This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_resource.py
664 lines (529 loc) · 22 KB
/
test_resource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import urllib
from datetime import datetime, timezone
import pytest
from todoms.attributes import Content as ContentAttr
from todoms.attributes import Importance, Status
from todoms.client import ToDoClient
from todoms.fields.basic import Attribute
from todoms.filters import and_, eq
from todoms.recurrence import Recurrence, patterns, ranges
from todoms.resources import (
ContentField,
Resource,
ResourceAlreadyCreatedError,
Subtask,
Task,
TaskList,
TaskListNotSpecifiedError,
TaskNotSpecifiedError,
UnsupportedOperationError,
)
from .utils.constants import API_BASE
from .utils.helpers import match_body
@pytest.fixture
def simple_resource_class():
class SimpleResource(Resource):
ENDPOINT = "endpoint"
_id = Attribute("id")
name = Attribute("name")
return SimpleResource
TASK_LIST_EXAMPLE_DATA = {
# "@odata.context": "https://graph.microsoft.com/beta/$metadata#lists/$entity",
# "@odata.etag": "xxx",
"displayName": "list-name",
"isOwner": True,
"isShared": True,
"wellknownListName": "none",
"id": "id-1",
}
SUBTASK_EXAMPLE_DATA = {
"displayName": "Subtask-1",
"createdDateTime": "2022-12-09T14:03:33Z",
"isChecked": True,
"checkedDateTime": "2022-12-09T16:13:52Z",
"id": "sub-1",
}
TASK_EXAMPLE_DATA = {
# "@odata.etag": "W/\"SRqIGuHKgEaeKjdSMmaZRwADcFhrVA==\"",
"importance": "high",
"isReminderOn": True,
"reminderDateTime": {"dateTime": "2020-05-03T00:00:00.000000", "timeZone": "UTC"},
"status": "notStarted",
"title": "My new task",
"createdDateTime": "2020-01-01T18:00:00Z",
"lastModifiedDateTime": "2021-01-01T18:00:00Z",
"dueDateTime": {"dateTime": "2020-05-02T00:00:00.000000", "timeZone": "UTC"},
"id": "task-1",
"body": {"content": "task-body", "contentType": "html"},
"completedDateTime": {"dateTime": "2020-05-01T00:00:00.000000", "timeZone": "UTC"},
"recurrence": {
"pattern": {
"type": "absoluteYearly",
"interval": 1,
"month": 7,
"dayOfMonth": 5,
},
"range": {"type": "noEnd"},
},
"categories": ["category-1", "category-2"],
"hasAttachments": False,
"startDateTime": {"dateTime": "2020-03-02T00:00:00.000000", "timeZone": "UTC"},
"checklistItems": [SUBTASK_EXAMPLE_DATA],
}
@pytest.fixture
def task_list(client):
return TaskList.from_dict(TASK_LIST_EXAMPLE_DATA, client=client)
@pytest.fixture
def task(client, task_list):
t = Task.from_dict(TASK_EXAMPLE_DATA, client=client)
t.task_list = task_list
return t
@pytest.mark.parametrize(
"resource,endpoint",
[(TaskList, "todo/lists"), (Task, "tasks"), (Subtask, "checklistItems")],
)
def test_resource_has_proper_endpoint(resource, endpoint):
assert resource.ENDPOINT == endpoint
@pytest.mark.parametrize(
"resource,data,to_omit",
[
(TaskList, TASK_LIST_EXAMPLE_DATA, ["isShared"]),
(Task, TASK_EXAMPLE_DATA, ["checklistItems"]),
(Subtask, SUBTASK_EXAMPLE_DATA, ["createdDateTime"]),
],
)
def test_resource_is_proper_converted_back_to_dict(resource, data, to_omit, client):
obj = resource.from_dict(data, client=client)
expected = {k: v for k, v in data.items() if k not in to_omit}
assert expected == obj.to_dict()
class TestDefaultResource:
def test_default_resource_create_set_client(self, client, simple_resource_class):
resource = simple_resource_class.from_dict(
{"_id": "id-1", "name": "name-1"}, client=client
)
assert resource._client == client
assert resource.name == "name-1"
def test_default_resource_update_client_call_and_refresh_data_from_response(
self, client, requests_mock
):
class ComplexResource(Resource):
ENDPOINT = "fake"
_id = Attribute("id")
new = ContentField("old")
last_updated = Attribute("last_updated")
resource = ComplexResource(client=client, new=ContentAttr("data"), _id="id-1")
requests_mock.patch(
f"{API_BASE}/fake/id-1",
json={"last_updated": "2020-01-01T18:00:00Z"},
status_code=200,
additional_matcher=match_body(resource.to_dict()),
)
resource.update()
assert requests_mock.called is True
assert resource.last_updated == "2020-01-01T18:00:00Z"
def test_default_resource_refresh_clearing_old_data(self, client, requests_mock):
class ComplexResource(Resource):
ENDPOINT = "fake"
_id = Attribute("id")
new = Attribute("old")
to_clear = Attribute("to_clear")
last_updated = Attribute("last_updated", read_only=True)
resource = ComplexResource(
client=client, new="data", _id="id-1", to_clear="data"
)
requests_mock.get(
f"{API_BASE}/fake/id-1",
json={
"last_updated": "2020-01-01T18:00:00Z",
"id": "id-1",
"old": "new-data",
},
status_code=200,
)
resource.refresh()
assert resource.new == "new-data"
assert resource.last_updated == "2020-01-01T18:00:00Z"
assert resource.id == "id-1"
assert resource.to_clear is None
def test_default_resource_id(self, simple_resource_class):
resource = simple_resource_class.from_dict({"id": "id-1", "name": "name-1"})
assert resource.id == "id-1"
def test_resources_are_equal_when_have_equal_id(self, simple_resource_class):
resource_1 = simple_resource_class.from_dict({"id": "id-1"})
resource_2 = simple_resource_class.from_dict({"id": "id-1"})
assert resource_1 == resource_2
def test_resources_are_not_equal_when_have_different_id(
self, simple_resource_class
):
resource_1 = simple_resource_class.from_dict({"id": "id-1"})
resource_2 = simple_resource_class.from_dict({"id": "id-2"})
assert resource_1 != resource_2
def test_resources_are_not_equal_when_dont_have_id(self, simple_resource_class):
resource_1 = simple_resource_class.from_dict({"name": "name-1"})
resource_2 = simple_resource_class.from_dict({"name": "name-1"})
assert resource_1 != resource_2
def test_default_resource_managing_endpoint(self, simple_resource_class):
resource = simple_resource_class.from_dict({"id": "id-1", "name": "name-1"})
assert resource.managing_endpoint == "endpoint/id-1"
def test_default_resource_id_return_none_when_unset(self, simple_resource_class):
resource = simple_resource_class.from_dict({"name": "name-1"})
assert resource.id is None
def test_default_resource_create_fails_when_id_set(self, simple_resource_class):
resource = simple_resource_class.from_dict({"id": "id-1", "name": "name-1"})
with pytest.raises(ResourceAlreadyCreatedError):
resource.create()
def test_default_resource_create_calls_endpoint(
self, simple_resource_class, client, requests_mock
):
resource = simple_resource_class(client=client, name="new-resource")
requests_mock.post(
f"{API_BASE}/endpoint",
json={"id": "new-id", "name": "new-resource"},
status_code=201,
additional_matcher=match_body({"name": "new-resource"}),
)
resource.create()
assert requests_mock.called is True
assert resource.id == "new-id"
def test_default_resource_delete_calls_endpoint(
self, simple_resource_class, client, requests_mock
):
resource = simple_resource_class.from_dict(
{"id": "id-1", "name": "name-1"}, client=client
)
requests_mock.delete(f"{API_BASE}/endpoint/id-1", status_code=204)
resource.delete()
assert requests_mock.called is True
def test_default_handle_list_filter_when_empty(self, simple_resource_class):
assert simple_resource_class.handle_list_filters() == {}
def test_default_handle_list_filter_when_given(self, simple_resource_class):
expected = {
"$filter": (
"((key1 eq 'val1' and key2 eq 'val2') and status eq 'inProgress')"
)
}
assert (
simple_resource_class.handle_list_filters(
and_(key1=eq("val1"), key2=eq("val2")), status=eq(Status.IN_PROGRESS)
)
== expected
)
def test_default_handle_list_filter_when_parameter_none(
self, simple_resource_class: Resource
):
assert simple_resource_class.handle_list_filters(status=None) == {}
class TestTaskListResource:
def test_create_tasklist_object_from_data(
self,
):
task_list = TaskList.from_dict(TASK_LIST_EXAMPLE_DATA)
assert task_list.id == "id-1"
assert task_list.name == "list-name"
assert task_list.is_owner is True
assert task_list.is_shared is True
assert task_list.well_known_name == "none"
def test_tasklist_get_tasks_returns_default_not_completed_tasks(
self, client, requests_mock
):
qs = urllib.parse.urlencode({"$filter": "status ne 'completed'"})
requests_mock.get(
f"{API_BASE}/todo/lists/id-1/tasks?{qs}",
json={"value": [TASK_EXAMPLE_DATA]},
complete_qs=True,
)
task_list = TaskList.from_dict(TASK_LIST_EXAMPLE_DATA, client=client)
tasks = list(task_list.get_tasks())
assert len(tasks) == 1
assert tasks[0].id == "task-1"
assert tasks[0].task_list == task_list
assert tasks == list(task_list.open_tasks)
def test_tasklist_prop_all_tasks_returns_all(self, client, requests_mock):
requests_mock.get(
f"{API_BASE}/todo/lists/id-1/tasks/delta",
json={"value": [TASK_EXAMPLE_DATA]},
complete_qs=True,
)
task_list = TaskList.from_dict(TASK_LIST_EXAMPLE_DATA, client=client)
tasks = list(task_list.tasks)
assert len(tasks) == 1
assert tasks[0].id == "task-1"
assert tasks[0].task_list == task_list
def test_task_list_delete_themselves(self, requests_mock, client):
requests_mock.delete(f"{API_BASE}/todo/lists/id-1", status_code=204)
task_list = TaskList.from_dict(TASK_LIST_EXAMPLE_DATA, client=client)
task_list.delete()
assert requests_mock.called is True
def test_task_list_saves_task(self, requests_mock, client):
task_list = TaskList.from_dict(TASK_LIST_EXAMPLE_DATA, client=client)
new_task = Task(client=client, title="Test")
expected_body = {k: v for k, v in new_task.to_dict().items() if v is not None}
requests_mock.post(
f"{API_BASE}/todo/lists/id-1/tasks",
status_code=201,
json={"id": "new_id"},
additional_matcher=match_body(expected_body),
)
task_list.save_task(new_task)
assert new_task._task_list is task_list
assert new_task.id == "new_id"
assert requests_mock.called is True
class TestTaskResource:
def test_minimum_task(self):
task = Task(title="Title")
assert task.id is None
assert task.title == "Title"
assert task._task_list is None
# Default values
assert task.status == Status.NOT_STARTED
assert task.importance == Importance.NORMAL
assert task.is_reminder_on is False
assert task.body is None
assert task.recurrence is None
assert task.due_datetime is None
assert task.completed_datetime is None
assert task.last_modified_datetime is None
assert task.created_datetime is None
assert task.reminder_datetime is None
assert task.start_datetime is None
assert task.categories is None
assert task.has_attachments is False
def test_create_task_object_from_dict(self, client):
task = Task.from_dict(TASK_EXAMPLE_DATA, client=client)
assert task.id == "task-1"
assert task.body == ContentAttr("task-body")
assert task.status == Status.NOT_STARTED
assert task.title == "My new task"
assert task.importance == Importance.HIGH
assert task.is_reminder_on is True
assert task.has_attachments is False
assert task.task_list is None
assert task.categories == ["category-1", "category-2"]
assert task.last_modified_datetime == datetime(
2021, 1, 1, 18, tzinfo=timezone.utc
)
assert task.created_datetime == datetime(2020, 1, 1, 18, tzinfo=timezone.utc)
assert task.completed_datetime == datetime(2020, 5, 1, tzinfo=timezone.utc)
assert task.due_datetime == datetime(2020, 5, 2, tzinfo=timezone.utc)
assert task.reminder_datetime == datetime(2020, 5, 3, tzinfo=timezone.utc)
assert task.start_datetime == datetime(2020, 3, 2, tzinfo=timezone.utc)
assert isinstance(task.recurrence, Recurrence) is True
assert isinstance(task.recurrence.pattern, patterns.YearlyAbsolute)
assert isinstance(task.recurrence.range, ranges.NoEnd)
assert len(task.subtasks) == 1
assert task.subtasks[0].name == "Subtask-1"
assert task.subtasks[0].task is task
@staticmethod
def _default_task_body(title: str, id: str = None):
data = {
"title": title,
"hasAttachments": False,
"importance": "normal",
"isReminderOn": False,
"status": "notStarted",
}
if id:
data["id"] = id
return data
def test_create_task_with_subtasks(
self, client: ToDoClient, requests_mock, task_list: TaskList
):
task = Task(title="Task-1", task_list=task_list, client=client)
task.add_subtask("Sub-1")
requests_mock.post(
f"{API_BASE}/todo/lists/{task_list.id}/tasks",
status_code=201,
json={"id": "new-id", "title": "Task-1"},
additional_matcher=match_body(self._default_task_body("Task-1")),
)
requests_mock.post(
f"{API_BASE}/todo/lists/{task_list.id}/tasks/new-id/checklistItems",
status_code=201,
json={"id": "s-1", "displayName": "Sub-1"},
additional_matcher=match_body({"displayName": "Sub-1", "isChecked": False}),
)
task.create()
assert task.subtasks[0].id == "s-1"
def test_create_task_without_subtasks(
self, client: ToDoClient, requests_mock, task_list: TaskList
):
task = Task(title="Task-1", task_list=task_list, client=client)
requests_mock.post(
f"{API_BASE}/todo/lists/{task_list.id}/tasks",
status_code=201,
json={"id": "new-id", "title": "Task-1"},
additional_matcher=match_body(self._default_task_body("Task-1")),
)
task.create()
assert task.id == "new-id"
def test_updating_tasks_updates_and_creates_subtasks(
self, client: ToDoClient, requests_mock, task_list: TaskList
):
task = Task(title="Task-1", _id="id-1", task_list=task_list, client=client)
task.add_subtask(
Subtask(name="Existing", _id="sub-1", task=task, client=client)
)
task.add_subtask("new subtask")
requests_mock.patch(
f"{API_BASE}/todo/lists/{task_list.id}/tasks/id-1",
status_code=200,
json={"id": "id-1", "title": "Task-1"},
)
# The already created subtask should be updated
requests_mock.patch(
f"{API_BASE}/todo/lists/{task_list.id}/tasks/id-1/checklistItems/sub-1",
status_code=200,
json={"id": "sub-1", "displayName": "Existing"},
additional_matcher=match_body(
{
"displayName": "Existing",
"isChecked": False,
"id": "sub-1",
"checkedDateTime": None,
}
),
)
# The new subtask should be created
requests_mock.post(
f"{API_BASE}/todo/lists/{task_list.id}/tasks/id-1/checklistItems",
status_code=201,
json={"id": "sub-2", "displayName": "new subtask"},
additional_matcher=match_body(
{
"displayName": "new subtask",
"isChecked": False,
}
),
)
task.update()
assert task.subtasks[1].id == "sub-2"
def test_updating_task_without_subtasks(
self, client: ToDoClient, requests_mock, task_list: TaskList
):
task = Task(title="Task-1", _id="id-1", task_list=task_list, client=client)
requests_mock.patch(
f"{API_BASE}/todo/lists/{task_list.id}/tasks/id-1",
status_code=200,
json={"id": "new-id", "title": "Task-1"},
)
task.update()
def test_task_handle_filters_default_completed(self):
filters = Task.handle_list_filters()
assert filters == {"$filter": "status ne 'completed'"}
def test_task_handle_filters_select_status(self):
filters = Task.handle_list_filters(status="my-filter")
assert filters == {"$filter": "status my-filter"}
def test_task_delete_themselves(self, requests_mock, client, task_list):
requests_mock.delete(
f"{API_BASE}/todo/lists/id-1/tasks/task-1", status_code=204
)
task = Task.from_dict(TASK_EXAMPLE_DATA, client=client)
task.task_list = task_list
task.delete()
assert requests_mock.called is True
def test_task_create_raises_when_no_tasklist_id(self):
task = Task(title="Test")
with pytest.raises(TaskListNotSpecifiedError):
task.create()
def test_task_managing_endpoint_raises_when_no_tasklist_id(self):
task = Task(title="Test")
with pytest.raises(TaskListNotSpecifiedError):
task.managing_endpoint
def test_task_raises_when_try_to_change_list(self, task_list, client):
task = Task.from_dict(TASK_EXAMPLE_DATA, client=client)
task.task_list = task_list
# Allow assign once more the same
task.task_list = task_list
task_list_2 = TaskList.from_dict({"id": "id-2"})
with pytest.raises(UnsupportedOperationError):
task.task_list = task_list_2
def test_saving_subtask(self, task, requests_mock):
subtask = Subtask(name="To Do")
requests_mock.post(
f"{API_BASE}/todo/lists/id-1/tasks/task-1/checklistItems",
status_code=201,
json={"id": "new-id", "displayName": "To Do"},
additional_matcher=match_body({"displayName": "To Do", "isChecked": False}),
)
task.save_subtask(subtask)
assert subtask.task is task
assert subtask.client is task.client
assert subtask in task.subtasks
assert subtask.id == "new-id"
def test_adding_subtask_from_object(self, task):
subtask = Subtask(name="To Do")
task.add_subtask(subtask)
assert subtask.task is task
assert subtask.client is task.client
assert subtask in task.subtasks
assert subtask.id is None
def test_adding_subtask_from_string(self, task):
task.add_subtask("My test")
subtask = next(filter(lambda s: s.name == "My test", task.subtasks))
assert subtask.task is task
assert subtask.client is task.client
assert subtask.id is None
class TestSubtaskResource:
def test_create_from_dict(self):
subtask = Subtask.from_dict(SUBTASK_EXAMPLE_DATA)
assert subtask.name == "Subtask-1"
assert subtask.is_checked is True
assert subtask.created_datetime == datetime(
2022, 12, 9, 14, 3, 33, tzinfo=timezone.utc
)
assert subtask.id == "sub-1"
assert subtask.checked_datetime == datetime(
2022, 12, 9, 16, 13, 52, tzinfo=timezone.utc
)
def test_minimum_subtask(self):
subtask = Subtask(name="My sub-1")
assert subtask.name == "My sub-1"
assert subtask.is_checked is False
def test_delete_themselves(self, requests_mock, client, task):
requests_mock.delete(
f"{API_BASE}/todo/lists/id-1/tasks/task-1/checklistItems/sub-1",
status_code=204,
)
subtask = Subtask.from_dict(SUBTASK_EXAMPLE_DATA, client=client)
subtask.task = task
subtask.delete()
assert requests_mock.called is True
def test_create_raises_when_no_task(self):
subtask = Subtask(name="Test")
with pytest.raises(TaskNotSpecifiedError):
subtask.create()
def test_managing_endpoint_raises_when_no_tas(self):
subtask = Subtask(name="Test")
with pytest.raises(TaskNotSpecifiedError):
subtask.managing_endpoint
def test_raises_when_try_to_change_task(self, task):
subtask = Subtask.from_dict(SUBTASK_EXAMPLE_DATA)
subtask.task = task
# Allow assign once more the same
subtask.task = task
task_2 = Task.from_dict({"id": "id-2"})
with pytest.raises(UnsupportedOperationError):
subtask.task = task_2
def test_check_and_uncheck(self):
subtask = Subtask(name="Test")
subtask.check()
assert subtask.is_checked is True
assert isinstance(subtask.checked_datetime, datetime)
subtask.uncheck()
assert subtask.is_checked is False
assert subtask.checked_datetime is None
def test_deleting_removes_from_task(self, task, requests_mock):
subtask = Subtask(name="aa", _id="test-1")
task.add_subtask(subtask)
assert len(task.subtasks) == 2
assert subtask in task.subtasks
requests_mock.delete(
f"{API_BASE}/todo/lists/id-1/tasks/task-1/checklistItems/test-1",
status_code=204,
)
subtask.delete()
assert len(task.subtasks) == 1
assert subtask not in task.subtasks
def test_adding_subtask_to_empty_task(self, client):
task = Task(title="test", client=client)
task.add_subtask("Test 2")
assert len(task.subtasks) == 1