Skip to content

Commit 7efad3f

Browse files
committed
fix: Duration handling in to_dict() and tests
1 parent f43df98 commit 7efad3f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

tests/data/test_defaults.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"timezone": "Europe/Moscow",
1717
}
1818

19+
DEFAULT_DURATION_RESPONSE = {
20+
"amount": 60,
21+
"unit": "minute",
22+
}
23+
1924
DEFAULT_TASK_RESPONSE = {
2025
"id": "1234",
2126
"assigner_id": "2971358",
@@ -35,6 +40,7 @@
3540
"created_at": "2019-01-02T21:00:30.00000Z",
3641
"url": "https://todoist.com/showTask?id=2995104339",
3742
"due": DEFAULT_DUE_RESPONSE,
43+
"duration": DEFAULT_DURATION_RESPONSE,
3844
}
3945

4046
DEFAULT_TASK_RESPONSE_2 = dict(DEFAULT_TASK_RESPONSE)

tests/test_models.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
DEFAULT_COMMENT_RESPONSE,
99
DEFAULT_COMPLETED_ITEMS_RESPONSE,
1010
DEFAULT_DUE_RESPONSE,
11+
DEFAULT_DURATION_RESPONSE,
1112
DEFAULT_ITEM_COMPLETED_INFO_RESPONSE,
1213
DEFAULT_ITEM_RESPONSE,
1314
DEFAULT_LABEL_RESPONSE,
@@ -22,6 +23,7 @@
2223
Comment,
2324
CompletedItems,
2425
Due,
26+
Duration,
2527
Item,
2628
ItemCompletedInfo,
2729
Label,
@@ -80,6 +82,16 @@ def test_due_from_dict():
8082
assert due.timezone == sample_data["timezone"]
8183

8284

85+
def test_duration_from_dict():
86+
sample_data = dict(DEFAULT_DURATION_RESPONSE)
87+
sample_data.update(unexpected_data)
88+
89+
duration = Duration.from_dict(sample_data)
90+
91+
assert duration.amount == sample_data["amount"]
92+
assert duration.unit == sample_data["unit"]
93+
94+
8395
def test_task_from_dict():
8496
sample_data = dict(DEFAULT_TASK_RESPONSE)
8597
sample_data.update(unexpected_data)
@@ -102,6 +114,34 @@ def test_task_from_dict():
102114
assert task.labels == sample_data["labels"]
103115
assert task.order == sample_data["order"]
104116
assert task.parent_id == sample_data["parent_id"]
117+
assert task.duration == Duration.from_dict(sample_data["duration"])
118+
119+
120+
def test_task_to_dict():
121+
sample_data = dict(DEFAULT_TASK_RESPONSE)
122+
sample_data.update(unexpected_data)
123+
124+
task = Task.from_dict(sample_data).to_dict()
125+
126+
assert task["comment_count"] == sample_data["comment_count"]
127+
assert task["is_completed"] == sample_data["is_completed"]
128+
assert task["content"] == sample_data["content"]
129+
assert task["created_at"] == sample_data["created_at"]
130+
assert task["creator_id"] == sample_data["creator_id"]
131+
assert task["id"] == sample_data["id"]
132+
assert task["project_id"] == sample_data["project_id"]
133+
assert task["section_id"] == sample_data["section_id"]
134+
assert task["priority"] == sample_data["priority"]
135+
assert task["url"] == sample_data["url"]
136+
assert task["assignee_id"] == sample_data["assignee_id"]
137+
assert task["assigner_id"] == sample_data["assigner_id"]
138+
for key in task["due"]:
139+
assert task["due"][key] == sample_data["due"][key]
140+
assert task["labels"] == sample_data["labels"]
141+
assert task["order"] == sample_data["order"]
142+
assert task["parent_id"] == sample_data["parent_id"]
143+
for key in task["duration"]:
144+
assert task["duration"][key] == sample_data["duration"][key]
105145

106146

107147
def test_collaborator_from_dict():

todoist_api_python/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,19 @@ def from_dict(cls, obj):
163163
project_id=obj["project_id"],
164164
section_id=obj["section_id"],
165165
url=obj["url"],
166-
duration=duration
166+
duration=duration,
167167
)
168168

169169
def to_dict(self):
170170
due: dict | None = None
171+
duration: dict | None = None
171172

172173
if self.due:
173174
due = self.due.to_dict()
174175

176+
if self.duration:
177+
duration = self.duration.to_dict()
178+
175179
return {
176180
"assignee_id": self.assignee_id,
177181
"assigner_id": self.assigner_id,
@@ -191,7 +195,7 @@ def to_dict(self):
191195
"section_id": self.section_id,
192196
"sync_id": self.sync_id,
193197
"url": self.url,
194-
"duration": self.duration
198+
"duration": duration,
195199
}
196200

197201
@classmethod
@@ -438,7 +442,7 @@ def from_dict(cls, obj: dict[str, Any]) -> CompletedItems:
438442

439443

440444
@dataclass
441-
class Duration(object):
445+
class Duration:
442446
amount: int
443447
unit: str
444448

0 commit comments

Comments
 (0)