Skip to content

Commit

Permalink
fix: Duration handling in to_dict() and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lefcha committed Jul 31, 2024
1 parent f43df98 commit 7efad3f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tests/data/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"timezone": "Europe/Moscow",
}

DEFAULT_DURATION_RESPONSE = {
"amount": 60,
"unit": "minute",
}

DEFAULT_TASK_RESPONSE = {
"id": "1234",
"assigner_id": "2971358",
Expand All @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,6 +23,7 @@
Comment,
CompletedItems,
Due,
Duration,
Item,
ItemCompletedInfo,
Label,
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand Down
10 changes: 7 additions & 3 deletions todoist_api_python/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -438,7 +442,7 @@ def from_dict(cls, obj: dict[str, Any]) -> CompletedItems:


@dataclass
class Duration(object):
class Duration:
amount: int
unit: str

Expand Down

0 comments on commit 7efad3f

Please sign in to comment.