diff --git a/tests/data/test_defaults.py b/tests/data/test_defaults.py index 9dad345..8d5fe85 100644 --- a/tests/data/test_defaults.py +++ b/tests/data/test_defaults.py @@ -16,6 +16,11 @@ "timezone": "Europe/Moscow", } +DEFAULT_DURATION_RESPONSE = { + "amount": 60, + "unit": "minute", +} + DEFAULT_TASK_RESPONSE = { "id": "1234", "assigner_id": "2971358", @@ -35,6 +40,7 @@ "created_at": "2019-01-02T21:00:30.00000Z", "url": "https://todoist.com/showTask?id=2995104339", "due": DEFAULT_DUE_RESPONSE, + "duration": DEFAULT_DURATION_RESPONSE, } DEFAULT_TASK_RESPONSE_2 = dict(DEFAULT_TASK_RESPONSE) diff --git a/tests/test_models.py b/tests/test_models.py index d6ad6b0..e5c6690 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -8,6 +8,7 @@ DEFAULT_COMMENT_RESPONSE, DEFAULT_COMPLETED_ITEMS_RESPONSE, DEFAULT_DUE_RESPONSE, + DEFAULT_DURATION_RESPONSE, DEFAULT_ITEM_COMPLETED_INFO_RESPONSE, DEFAULT_ITEM_RESPONSE, DEFAULT_LABEL_RESPONSE, @@ -22,6 +23,7 @@ Comment, CompletedItems, Due, + Duration, Item, ItemCompletedInfo, Label, @@ -80,6 +82,16 @@ def test_due_from_dict(): assert due.timezone == sample_data["timezone"] +def test_duration_from_dict(): + sample_data = dict(DEFAULT_DURATION_RESPONSE) + sample_data.update(unexpected_data) + + duration = Duration.from_dict(sample_data) + + assert duration.amount == sample_data["amount"] + assert duration.unit == sample_data["unit"] + + def test_task_from_dict(): sample_data = dict(DEFAULT_TASK_RESPONSE) sample_data.update(unexpected_data) @@ -102,6 +114,34 @@ def test_task_from_dict(): assert task.labels == sample_data["labels"] assert task.order == sample_data["order"] assert task.parent_id == sample_data["parent_id"] + assert task.duration == Duration.from_dict(sample_data["duration"]) + + +def test_task_to_dict(): + sample_data = dict(DEFAULT_TASK_RESPONSE) + sample_data.update(unexpected_data) + + task = Task.from_dict(sample_data).to_dict() + + assert task["comment_count"] == sample_data["comment_count"] + assert task["is_completed"] == sample_data["is_completed"] + assert task["content"] == sample_data["content"] + assert task["created_at"] == sample_data["created_at"] + assert task["creator_id"] == sample_data["creator_id"] + assert task["id"] == sample_data["id"] + assert task["project_id"] == sample_data["project_id"] + assert task["section_id"] == sample_data["section_id"] + assert task["priority"] == sample_data["priority"] + assert task["url"] == sample_data["url"] + assert task["assignee_id"] == sample_data["assignee_id"] + assert task["assigner_id"] == sample_data["assigner_id"] + for key in task["due"]: + assert task["due"][key] == sample_data["due"][key] + assert task["labels"] == sample_data["labels"] + assert task["order"] == sample_data["order"] + assert task["parent_id"] == sample_data["parent_id"] + for key in task["duration"]: + assert task["duration"][key] == sample_data["duration"][key] def test_collaborator_from_dict(): diff --git a/todoist_api_python/models.py b/todoist_api_python/models.py index 37e4729..ed1c9fa 100644 --- a/todoist_api_python/models.py +++ b/todoist_api_python/models.py @@ -163,15 +163,19 @@ def from_dict(cls, obj): project_id=obj["project_id"], section_id=obj["section_id"], url=obj["url"], - duration=duration + duration=duration, ) def to_dict(self): due: dict | None = None + duration: dict | None = None if self.due: due = self.due.to_dict() + if self.duration: + duration = self.duration.to_dict() + return { "assignee_id": self.assignee_id, "assigner_id": self.assigner_id, @@ -191,7 +195,7 @@ def to_dict(self): "section_id": self.section_id, "sync_id": self.sync_id, "url": self.url, - "duration": self.duration + "duration": duration, } @classmethod @@ -438,7 +442,7 @@ def from_dict(cls, obj: dict[str, Any]) -> CompletedItems: @dataclass -class Duration(object): +class Duration: amount: int unit: str