Skip to content

Commit 88cdba9

Browse files
committed
Switch tests to declarative respx request lookups
Use params__eq, headers__contains, and json__eq via mock_route keyword arguments instead of runtime matcher callbacks. Set API test fixtures to deterministic request IDs so header assertions can be fully declarative and consistent across sync/async test suites.
1 parent 2b10d06 commit 88cdba9

11 files changed

Lines changed: 129 additions & 286 deletions

tests/conftest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
DEFAULT_LABELS_RESPONSE,
1616
DEFAULT_PROJECT_RESPONSE,
1717
DEFAULT_PROJECTS_RESPONSE,
18+
DEFAULT_REQUEST_ID,
1819
DEFAULT_SECTION_RESPONSE,
1920
DEFAULT_SECTIONS_RESPONSE,
2021
DEFAULT_TASK_META_RESPONSE,
@@ -42,13 +43,19 @@
4243

4344
@pytest.fixture
4445
def todoist_api() -> Iterator[TodoistAPI]:
45-
with TodoistAPI(DEFAULT_TOKEN) as api:
46+
with TodoistAPI(
47+
DEFAULT_TOKEN,
48+
request_id_fn=lambda: DEFAULT_REQUEST_ID,
49+
) as api:
4650
yield api
4751

4852

4953
@pytest_asyncio.fixture
5054
async def todoist_api_async() -> AsyncIterator[TodoistAPIAsync]:
51-
async with TodoistAPIAsync(DEFAULT_TOKEN) as api:
55+
async with TodoistAPIAsync(
56+
DEFAULT_TOKEN,
57+
request_id_fn=lambda: DEFAULT_REQUEST_ID,
58+
) as api:
5259
yield api
5360

5461

tests/test_api_comments.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
DEFAULT_API_URL,
99
PaginatedResults,
1010
)
11-
from tests.utils.test_utils import (
12-
auth_matcher,
13-
data_matcher,
14-
enumerate_async,
15-
mock_route,
16-
request_id_matcher,
17-
)
11+
from tests.utils.test_utils import api_headers, enumerate_async, mock_route
1812
from todoist_api_python.models import Attachment
1913

2014
if TYPE_CHECKING:
@@ -43,7 +37,7 @@ async def test_get_comment(
4337
url=endpoint,
4438
json=default_comment_response,
4539
status=200,
46-
matchers=[auth_matcher(), request_id_matcher()],
40+
headers=api_headers(),
4741
)
4842

4943
comment = todoist_api.get_comment(comment_id)
@@ -77,10 +71,7 @@ async def test_get_comments(
7771
json=page,
7872
status=200,
7973
params={"task_id": task_id} | ({"cursor": cursor} if cursor else {}),
80-
matchers=[
81-
auth_matcher(),
82-
request_id_matcher(),
83-
],
74+
headers=api_headers(),
8475
)
8576
cursor = page["next_cursor"]
8677

@@ -124,17 +115,12 @@ async def test_add_comment(
124115
url=f"{DEFAULT_API_URL}/comments",
125116
json=default_comment_response,
126117
status=200,
127-
matchers=[
128-
auth_matcher(),
129-
request_id_matcher(),
130-
data_matcher(
131-
{
132-
"content": content,
133-
"project_id": project_id,
134-
"attachment": attachment.to_dict(),
135-
}
136-
),
137-
],
118+
headers=api_headers(),
119+
request_json={
120+
"content": content,
121+
"project_id": project_id,
122+
"attachment": attachment.to_dict(),
123+
},
138124
)
139125

140126
new_comment = todoist_api.add_comment(
@@ -174,7 +160,8 @@ async def test_update_comment(
174160
url=f"{DEFAULT_API_URL}/comments/{default_comment.id}",
175161
json=updated_comment_dict,
176162
status=200,
177-
matchers=[auth_matcher(), request_id_matcher(), data_matcher(args)],
163+
headers=api_headers(),
164+
request_json=args,
178165
)
179166

180167
response = todoist_api.update_comment(comment_id=default_comment.id, **args)
@@ -204,7 +191,7 @@ async def test_delete_comment(
204191
method="DELETE",
205192
url=endpoint,
206193
status=204,
207-
matchers=[auth_matcher(), request_id_matcher()],
194+
headers=api_headers(),
208195
)
209196

210197
response = todoist_api.delete_comment(comment_id)

tests/test_api_completed_tasks.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
import pytest
1313

1414
from tests.data.test_defaults import DEFAULT_API_URL, PaginatedItems
15-
from tests.utils.test_utils import (
16-
auth_matcher,
17-
enumerate_async,
18-
mock_route,
19-
request_id_matcher,
20-
)
15+
from tests.utils.test_utils import api_headers, enumerate_async, mock_route
2116
from todoist_api_python._core.utils import format_datetime
2217

2318
if TYPE_CHECKING:
@@ -59,10 +54,7 @@ async def test_get_completed_tasks_by_due_date(
5954
json=page,
6055
status=200,
6156
params=params | ({"cursor": cursor} if cursor else {}),
62-
matchers=[
63-
auth_matcher(),
64-
request_id_matcher(),
65-
],
57+
headers=api_headers(),
6658
)
6759
cursor = page["next_cursor"]
6860

@@ -124,10 +116,7 @@ async def test_get_completed_tasks_by_completion_date(
124116
json=page,
125117
status=200,
126118
params=params | ({"cursor": cursor} if cursor else {}),
127-
matchers=[
128-
auth_matcher(),
129-
request_id_matcher(),
130-
],
119+
headers=api_headers(),
131120
)
132121
cursor = page["next_cursor"]
133122

tests/test_api_labels.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
import pytest
66

77
from tests.data.test_defaults import DEFAULT_API_URL, PaginatedResults
8-
from tests.utils.test_utils import (
9-
auth_matcher,
10-
data_matcher,
11-
enumerate_async,
12-
mock_route,
13-
request_id_matcher,
14-
)
8+
from tests.utils.test_utils import api_headers, enumerate_async, mock_route
159

1610
if TYPE_CHECKING:
1711
import respx
@@ -38,7 +32,7 @@ async def test_get_label(
3832
url=endpoint,
3933
json=default_label_response,
4034
status=200,
41-
matchers=[auth_matcher()],
35+
headers=api_headers(),
4236
)
4337

4438
label = todoist_api.get_label(label_id)
@@ -71,10 +65,7 @@ async def test_get_labels(
7165
json=page,
7266
status=200,
7367
params={"cursor": cursor} if cursor else {},
74-
matchers=[
75-
auth_matcher(),
76-
request_id_matcher(),
77-
],
68+
headers=api_headers(),
7869
)
7970
cursor = page["next_cursor"]
8071

@@ -115,10 +106,7 @@ async def test_search_labels(
115106
json=page,
116107
status=200,
117108
params={"query": query} | ({"cursor": cursor} if cursor else {}),
118-
matchers=[
119-
auth_matcher(),
120-
request_id_matcher(),
121-
],
109+
headers=api_headers(),
122110
)
123111
cursor = page["next_cursor"]
124112

@@ -155,11 +143,8 @@ async def test_add_label_minimal(
155143
url=f"{DEFAULT_API_URL}/labels",
156144
json=default_label_response,
157145
status=200,
158-
matchers=[
159-
auth_matcher(),
160-
request_id_matcher(),
161-
data_matcher({"name": label_name}),
162-
],
146+
headers=api_headers(),
147+
request_json={"name": label_name},
163148
)
164149

165150
new_label = todoist_api.add_label(name=label_name)
@@ -194,11 +179,8 @@ async def test_add_label_full(
194179
url=f"{DEFAULT_API_URL}/labels",
195180
json=default_label_response,
196181
status=200,
197-
matchers=[
198-
auth_matcher(),
199-
request_id_matcher(),
200-
data_matcher({"name": label_name} | args),
201-
],
182+
headers=api_headers(),
183+
request_json={"name": label_name} | args,
202184
)
203185

204186
new_label = todoist_api.add_label(name=label_name, **args)
@@ -230,7 +212,8 @@ async def test_update_label(
230212
url=f"{DEFAULT_API_URL}/labels/{default_label.id}",
231213
json=updated_label_dict,
232214
status=200,
233-
matchers=[auth_matcher(), request_id_matcher(), data_matcher(args)],
215+
headers=api_headers(),
216+
request_json=args,
234217
)
235218

236219
response = todoist_api.update_label(label_id=default_label.id, **args)
@@ -258,7 +241,7 @@ async def test_delete_label(
258241
method="DELETE",
259242
url=endpoint,
260243
status=204,
261-
matchers=[auth_matcher(), request_id_matcher()],
244+
headers=api_headers(),
262245
)
263246

264247
response = todoist_api.delete_label(label_id)

tests/test_api_projects.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
import pytest
66

77
from tests.data.test_defaults import DEFAULT_API_URL, PaginatedResults
8-
from tests.utils.test_utils import (
9-
auth_matcher,
10-
data_matcher,
11-
enumerate_async,
12-
mock_route,
13-
request_id_matcher,
14-
)
8+
from tests.utils.test_utils import api_headers, enumerate_async, mock_route
159

1610
if TYPE_CHECKING:
1711
import respx
@@ -38,7 +32,7 @@ async def test_get_project(
3832
url=endpoint,
3933
json=default_project_response,
4034
status=200,
41-
matchers=[auth_matcher()],
35+
headers=api_headers(),
4236
)
4337

4438
project = todoist_api.get_project(project_id)
@@ -71,10 +65,7 @@ async def test_get_projects(
7165
json=page,
7266
status=200,
7367
params={"cursor": cursor} if cursor else {},
74-
matchers=[
75-
auth_matcher(),
76-
request_id_matcher(),
77-
],
68+
headers=api_headers(),
7869
)
7970
cursor = page["next_cursor"]
8071

@@ -115,10 +106,7 @@ async def test_search_projects(
115106
json=page,
116107
status=200,
117108
params={"query": query} | ({"cursor": cursor} if cursor else {}),
118-
matchers=[
119-
auth_matcher(),
120-
request_id_matcher(),
121-
],
109+
headers=api_headers(),
122110
)
123111
cursor = page["next_cursor"]
124112

@@ -155,11 +143,8 @@ async def test_add_project_minimal(
155143
url=f"{DEFAULT_API_URL}/projects",
156144
json=default_project_response,
157145
status=200,
158-
matchers=[
159-
auth_matcher(),
160-
request_id_matcher(),
161-
data_matcher({"name": project_name}),
162-
],
146+
headers=api_headers(),
147+
request_json={"name": project_name},
163148
)
164149

165150
new_project = todoist_api.add_project(name=project_name)
@@ -196,11 +181,8 @@ async def test_add_project_full(
196181
url=f"{DEFAULT_API_URL}/projects",
197182
json=default_project_response,
198183
status=200,
199-
matchers=[
200-
auth_matcher(),
201-
request_id_matcher(),
202-
data_matcher({"name": project_name} | args),
203-
],
184+
headers=api_headers(),
185+
request_json={"name": project_name} | args,
204186
)
205187

206188
new_project = todoist_api.add_project(name=project_name, **args)
@@ -234,7 +216,8 @@ async def test_update_project(
234216
url=f"{DEFAULT_API_URL}/projects/{default_project.id}",
235217
json=updated_project_dict,
236218
status=200,
237-
matchers=[auth_matcher(), request_id_matcher(), data_matcher(args)],
219+
headers=api_headers(),
220+
request_json=args,
238221
)
239222

240223
response = todoist_api.update_project(project_id=default_project.id, **args)
@@ -269,7 +252,7 @@ async def test_archive_project(
269252
url=endpoint,
270253
json=archived_project_dict,
271254
status=200,
272-
matchers=[auth_matcher(), request_id_matcher()],
255+
headers=api_headers(),
273256
)
274257

275258
project = todoist_api.archive_project(project_id)
@@ -302,7 +285,7 @@ async def test_unarchive_project(
302285
url=endpoint,
303286
json=unarchived_project_dict,
304287
status=200,
305-
matchers=[auth_matcher(), request_id_matcher()],
288+
headers=api_headers(),
306289
)
307290

308291
project = todoist_api.unarchive_project(project_id)
@@ -330,7 +313,7 @@ async def test_delete_project(
330313
method="DELETE",
331314
url=endpoint,
332315
status=204,
333-
matchers=[auth_matcher(), request_id_matcher()],
316+
headers=api_headers(),
334317
)
335318

336319
response = todoist_api.delete_project(project_id)
@@ -364,10 +347,7 @@ async def test_get_collaborators(
364347
json=page,
365348
status=200,
366349
params={"cursor": cursor} if cursor else {},
367-
matchers=[
368-
auth_matcher(),
369-
request_id_matcher(),
370-
],
350+
headers=api_headers(),
371351
)
372352
cursor = page["next_cursor"]
373353

0 commit comments

Comments
 (0)