Skip to content

Commit

Permalink
fix and test property setters
Browse files Browse the repository at this point in the history
  • Loading branch information
thrau committed Oct 14, 2023
1 parent 5045e13 commit 916c9d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
10 changes: 5 additions & 5 deletions notion_objects/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ class MultiSelect(Property[List[str]]):
def get(self, field: str, obj: dict) -> List[str]:
return [s["name"] for s in obj["properties"][field].get("multi_select", [])]

def set(self, field: str, value: List[str], obj: dict):
obj[field] = {"multi_select": [{"name": v} for v in value]}
def set(self, field: str, value: Optional[List[str]], obj: dict):
obj[field] = {"multi_select": [{"name": v} for v in value] if value is not None else None}


class Status(Property[Optional[str]]):
Expand Down Expand Up @@ -488,7 +488,7 @@ def get_value(field: str, obj: dict) -> List[UserValue]:
]

@staticmethod
def set_value(field: str, value: List[Union[str, UserValue]], obj: dict):
def set_value(field: str, value: Optional[List[Union[str, UserValue]]], obj: dict):
if not value:
obj[field] = {"people": []}
return
Expand All @@ -512,8 +512,8 @@ def get(self, field: str, obj: dict) -> Optional[str]:

return people[0].name or people[0].id

def set(self, field: str, value: str, obj: dict):
PeopleProperty.set_value(field, [value], obj)
def set(self, field: str, value: Optional[str], obj: dict):
PeopleProperty.set_value(field, [value] if value is not None else None, obj)


class People(Property[List[str]]):
Expand Down
30 changes: 27 additions & 3 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import date, datetime, timedelta
from typing import Tuple

from dateutil.tz import tzutc

Expand Down Expand Up @@ -42,8 +43,8 @@ class SamplePage(Page, SampleObject):
class MyDateObject(NotionObject):
my_date: date = Date("Date")
my_date_time: datetime = DateTime("DateTime")
my_date_range: tuple[date, date] = DateRange("DateRange")
my_date_time_range: tuple[datetime, datetime] = DateTimeRange("DateTimeRange")
my_date_range: Tuple[date, date] = DateRange("DateRange")
my_date_time_range: Tuple[datetime, datetime] = DateTimeRange("DateTimeRange")


def test_access_properties():
Expand Down Expand Up @@ -75,8 +76,31 @@ def test_set_properties():

o = SampleObject(data[1])

o.name = "MyName"
o.status = "Closed"
print(o.__changes__)
o.my_select = "Foobar"
o.person = "d3d8f682-95a9-4513-b7fd-be52d7f6aa64"
o.Phone = "+123456789"
o.Tags = ["tag1", "tag2"]

assert o.__changes__ == {
"Name": {
"title": [{"plain_text": "MyName", "text": {"content": "MyName"}, "type": "text"}]
},
"Status": {"status": {"name": "Closed"}},
"My select": {"select": {"name": "Foobar"}},
"Person": {"people": [{"id": "d3d8f682-95a9-4513-b7fd-be52d7f6aa64", "object": "user"}]},
"Phone": {"phone_number": "+123456789"},
"Tags": {"multi_select": [{"name": "tag1"}, {"name": "tag2"}]},
}

# unset all attributes

o.status = None
o.my_select = None
o.person = None
o.Phone = None
o.Tags = None


def test_to_dict():
Expand Down

0 comments on commit 916c9d0

Please sign in to comment.