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 99985bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 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
25 changes: 24 additions & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,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 99985bc

Please sign in to comment.