-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_project.py
More file actions
419 lines (325 loc) · 15.4 KB
/
test_project.py
File metadata and controls
419 lines (325 loc) · 15.4 KB
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
from __future__ import annotations
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Dict, Union
from unittest.mock import Mock, patch
import pytest
from httpx import Response as HttpxResponse
from pydantic import ValidationError
from toggl_python.schemas.base import BulkEditOperation, BulkEditOperations, BulkEditResponse
from toggl_python.schemas.project import BulkEditProjectsFieldNames, ProjectResponse
from tests.conftest import fake
from tests.factories.base import bulk_edit_response_factory, datetime_repr_factory
from tests.factories.project import project_request_factory, project_response_factory
from tests.responses.project_get import PROJECT_RESPONSE
if TYPE_CHECKING:
from respx import MockRouter
from toggl_python.entities.user import CurrentUser
from toggl_python.entities.workspace import Workspace
def test_create_project(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
full_request_body = project_request_factory()
random_param = fake.random_element(full_request_body.keys())
request_body = {random_param: full_request_body[random_param]}
response = project_response_factory()
mocked_route = response_mock.post(
f"/workspaces/{workspace_id}/projects", json=request_body
).mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = ProjectResponse.model_validate(response)
result = authed_workspace.create_project(workspace_id, **request_body)
assert mocked_route.called is True
assert result == expected_result
def test_create_project_all_params(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
request_body = project_request_factory()
response = project_response_factory(workspace_id, end_date=request_body["end_date"])
mocked_route = response_mock.post(
f"/workspaces/{workspace_id}/projects", json=request_body
).mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = ProjectResponse.model_validate(response)
result = authed_workspace.create_project(workspace_id, **request_body)
assert mocked_route.called is True
assert result == expected_result
def test_create_project_empty_request_body(
response_mock: MockRouter, authed_workspace: Workspace
) -> None:
workspace_id = fake.random_int()
response = project_response_factory(workspace_id)
mocked_route = response_mock.post(f"/workspaces/{workspace_id}/projects").mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = ProjectResponse.model_validate(response)
result = authed_workspace.create_project(workspace_id)
assert mocked_route.called is True
assert result == expected_result
def test_get_project_by_id(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = 123
project_id = 123
mocked_route = response_mock.get(f"/workspaces/{workspace_id}/projects/{project_id}").mock(
return_value=HttpxResponse(status_code=200, json=PROJECT_RESPONSE),
)
expected_result = ProjectResponse.model_validate(PROJECT_RESPONSE)
result = authed_workspace.get_project(workspace_id=workspace_id, project_id=project_id)
assert mocked_route.called is True
assert result == expected_result
def test_get_projects__without_query_params(
response_mock: MockRouter, authed_workspace: Workspace
) -> None:
workspace_id = 123
mocked_route = response_mock.get(f"/workspaces/{workspace_id}/projects").mock(
return_value=HttpxResponse(status_code=200, json=[PROJECT_RESPONSE]),
)
expected_result = [ProjectResponse.model_validate(PROJECT_RESPONSE)]
result = authed_workspace.get_projects(workspace_id=workspace_id)
assert mocked_route.called is True
assert result == expected_result
@patch("toggl_python.schemas.base.datetime")
def test_get_projects__too_old_since_value(
mocked_datetime: Mock, authed_workspace: Workspace
) -> None:
error_message = "Since cannot be older than 3 months"
since = datetime(2020, 1, 1, tzinfo=timezone.utc)
mocked_datetime.now.return_value = datetime(2020, 4, 1, tzinfo=timezone.utc)
with pytest.raises(ValidationError, match=error_message):
_ = authed_workspace.get_projects(workspace_id=123, since=since)
@patch("toggl_python.schemas.base.datetime")
@pytest.mark.parametrize(
argnames="query_params",
argvalues=(
{"active": False},
{"since": int(datetime(2024, 5, 10, tzinfo=timezone.utc).timestamp())},
{"billable": True},
{"user_ids": [1234567]},
{"client_ids": [209327532]},
{"group_ids": [214327]},
{"statuses": "active"},
{"name": "random project name"},
{"page": 1},
{"per_page": 10},
{"sort_field": "billable"},
{"sort_order": "DESC"},
{"only_templates": True},
{"only_me": True},
),
)
def test_get_projects__with_query_params(
mocked_datetime: Mock,
query_params: Dict[str, Union[str, int]],
response_mock: MockRouter,
authed_workspace: Workspace,
) -> None:
mocked_datetime.now.return_value = datetime(2024, 7, 20, tzinfo=timezone.utc)
workspace_id = 123
mocked_route = response_mock.get(
f"/workspaces/{workspace_id}/projects", params=query_params
).mock(
return_value=HttpxResponse(status_code=200, json=[PROJECT_RESPONSE]),
)
expected_result = [ProjectResponse.model_validate(PROJECT_RESPONSE)]
result = authed_workspace.get_projects(workspace_id=workspace_id, **query_params)
assert mocked_route.called is True
assert result == expected_result
def test_me_get_projects__without_query_params(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
mocked_route = response_mock.get("me/projects").mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_projects()
assert mocked_route.called is True
assert result == expected_result
def test_me_get_projects__include_archived(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
include_archived = fake.boolean()
mocked_route = response_mock.get(
"me/projects", params={"include_archived": include_archived}
).mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_projects(include_archived=include_archived)
assert mocked_route.called is True
assert result == expected_result
def test_me_get_projects__since(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
since = int(fake.past_datetime(start_date="-3m").timestamp())
mocked_route = response_mock.get("me/projects", params={"since": since}).mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_projects(since=since)
assert mocked_route.called is True
assert result == expected_result
def test_me_get_projects__too_old_since_value(authed_current_user: CurrentUser) -> None:
error_message = "Since cannot be older than 3 months"
since = int(fake.date_time_between(end_date="-3m4d").timestamp())
with pytest.raises(ValidationError, match=error_message):
_ = authed_current_user.get_projects(since=since)
def test_me_get_paginated_projects__without_query_params(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
mocked_route = response_mock.get("me/projects/paginated").mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_paginated_projects()
assert mocked_route.called is True
assert result == expected_result
def test_me_get_paginated_projects__since_query_param(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
since = int(fake.past_datetime(start_date="-3m").timestamp())
mocked_route = response_mock.get("me/projects/paginated", params={"since": since}).mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_paginated_projects(since=since)
assert mocked_route.called is True
assert result == expected_result
def test_me_get_paginated_projects__too_old_since_value(authed_current_user: CurrentUser) -> None:
error_message = "Since cannot be older than 3 months"
since = int(fake.date_time_between(end_date="-3m4d").timestamp())
with pytest.raises(ValidationError, match=error_message):
_ = authed_current_user.get_paginated_projects(since=since)
def test_me_get_paginated_projects__with_query_params(
response_mock: MockRouter, authed_current_user: CurrentUser
) -> None:
response = project_response_factory()
query_params = {
"start_project_id": fake.random_int(),
"per_page": fake.random_int(min=50, max=200),
}
mocked_route = response_mock.get("me/projects/paginated", params=query_params).mock(
return_value=HttpxResponse(status_code=200, json=[response]),
)
expected_result = [ProjectResponse.model_validate(response)]
result = authed_current_user.get_paginated_projects(**query_params)
assert mocked_route.called is True
assert result == expected_result
def test_update_project(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_id = fake.random_int()
full_request_body = project_request_factory()
random_param = fake.random_element(full_request_body.keys())
request_body = {random_param: full_request_body[random_param]}
response = project_response_factory()
mocked_route = response_mock.put(
f"/workspaces/{workspace_id}/projects/{project_id}", json=request_body
).mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = ProjectResponse.model_validate(response)
result = authed_workspace.update_project(workspace_id, project_id, **request_body)
assert mocked_route.called is True
assert result == expected_result
def test_update_project_all_params(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_id = fake.random_int()
request_body = project_request_factory()
response = project_response_factory(workspace_id, end_date=request_body["end_date"])
mocked_route = response_mock.put(
f"/workspaces/{workspace_id}/projects/{project_id}", json=request_body
).mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = ProjectResponse.model_validate(response)
result = authed_workspace.update_project(workspace_id, project_id, **request_body)
assert mocked_route.called is True
assert result == expected_result
def test_update_project__both_client_id_and_client_name(authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_id = fake.random_int()
error_message = "Both client_id and client_name provided"
with pytest.raises(ValidationError, match=error_message):
_ = authed_workspace.update_project(
workspace_id, project_id, client_id=fake.random_int(), client_name=fake.name()
)
def test_update_project__invalid_timeframe(authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_id = fake.random_int()
start_date = fake.past_date()
end_date = fake.date_between(end_date=start_date).isoformat()
error_message = "Project timeframe is not valid"
with pytest.raises(ValidationError, match=error_message):
_ = authed_workspace.update_project(
workspace_id,
project_id,
start_date=start_date.isoformat(),
end_date=end_date,
)
def test_bulk_edit_projects__too_much_ids(authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_ids = [fake.random_int() for _ in range(101)]
error_message = "List should have at most 100 items after validation"
with pytest.raises(ValueError, match=error_message):
_ = authed_workspace.bulk_edit_projects(workspace_id, project_ids, operations=[])
def test_bulk_edit_projects__empty_projects_ids(authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
error_message = "List should have at least 1 item after validation"
with pytest.raises(ValueError, match=error_message):
_ = authed_workspace.bulk_edit_projects(workspace_id, project_ids=[], operations=[])
def test_bulk_edit_projects__empty_operations(authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_ids = [fake.random_int()]
error_message = "List should have at least 1 item after validation"
with pytest.raises(ValueError, match=error_message):
_ = authed_workspace.bulk_edit_projects(workspace_id, project_ids, operations=[])
@pytest.mark.parametrize(
argnames=("operation"), argvalues=[item.value for item in BulkEditOperations]
)
@pytest.mark.parametrize(
argnames=("field_name", "field_value"),
argvalues=[
(BulkEditProjectsFieldNames.auto_estimates.value, fake.boolean()),
(BulkEditProjectsFieldNames.end_date.value, datetime_repr_factory()),
(BulkEditProjectsFieldNames.estimated_hours.value, fake.random_int()),
(BulkEditProjectsFieldNames.is_private.value, fake.boolean()),
(BulkEditProjectsFieldNames.project_name.value, fake.uuid4()),
(BulkEditProjectsFieldNames.start_date.value, fake.date()),
],
)
def test_bulk_edit_time_entries__ok(
field_name: BulkEditProjectsFieldNames,
field_value: Union[str, int],
operation: BulkEditOperations,
response_mock: MockRouter,
authed_workspace: Workspace,
) -> None:
workspace_id = fake.random_int()
project_ids = [fake.random_int(), fake.random_int()]
project_ids_repr = ",".join(str(item) for item in project_ids)
edit_operation = BulkEditOperation(
operation=operation, field_name=field_name, field_value=field_value
)
response = bulk_edit_response_factory()
mocked_route = response_mock.patch(
f"/workspaces/{workspace_id}/projects/{project_ids_repr}"
).mock(
return_value=HttpxResponse(status_code=200, json=response),
)
expected_result = BulkEditResponse.model_validate(response)
result = authed_workspace.bulk_edit_projects(
workspace_id, project_ids, operations=[edit_operation]
)
assert mocked_route.called is True
assert result == expected_result
def test_delete_project(response_mock: MockRouter, authed_workspace: Workspace) -> None:
workspace_id = fake.random_int()
project_id = fake.random_int()
mocked_route = response_mock.delete(f"/workspaces/{workspace_id}/projects/{project_id}").mock(
return_value=HttpxResponse(status_code=200),
)
result = authed_workspace.delete_project(workspace_id, project_id)
assert mocked_route.called is True
assert result is True