Coverage for tests\test_put_api_projects_texts_undelete.py: 100.00%
139 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-22 23:23 +0100
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-22 23:23 +0100
1# -*- coding: utf-8 -*-
3"""
4* Name: interactive-clustering-gui/tests/test_put_api_projects_texts_undelete.py
5* Description: Unittests for `app` module on the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route.
6* Author: Erwan Schild
7* Created: 22/02/2022
8* Licence: CeCILL (https://cecill.info/licences.fr.html)
9"""
11# ==============================================================================
12# IMPORT PYTHON DEPENDENCIES
13# ==============================================================================
15import pytest
17from tests.dummies_utils import create_dummy_projects
19# ==============================================================================
20# test_ko_not_found
21# ==============================================================================
24@pytest.mark.asyncio()
25async def test_ko_not_found_1(async_client):
26 """
27 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with not existing project.
29 Arguments:
30 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
31 """
32 # Assert HTTP client is created.
33 assert async_client
35 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
36 response_put = await async_client.put(url="/api/projects/UNKNOWN_PROJECT/texts/UNKNOWN_TEXT/undelete")
37 assert response_put.status_code == 404
38 assert response_put.json() == {
39 "detail": "The project with id 'UNKNOWN_PROJECT' doesn't exist.",
40 }
43# ==============================================================================
44# test_ko_not_found
45# ==============================================================================
48@pytest.mark.asyncio()
49async def test_ko_not_found_2(async_client, tmp_path):
50 """
51 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with not existing project.
53 Arguments:
54 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
55 tmp_path: The temporary path given for this test, declared in `conftest.py`.
56 """
57 # Assert HTTP client is created.
58 assert async_client
60 # Create dummy projects.
61 create_dummy_projects(
62 tmp_path=tmp_path,
63 list_of_dummy_project_ids=[
64 "1a_SAMPLING_TODO",
65 ],
66 )
68 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
69 response_put = await async_client.put(url="/api/projects/1a_SAMPLING_TODO/texts/UNKNOWN_TEXT/undelete")
70 assert response_put.status_code == 404
71 assert response_put.json() == {
72 "detail": "In project with id '1a_SAMPLING_TODO', the text with id 'UNKNOWN_TEXT' to undelete doesn't exist.",
73 }
76# ==============================================================================
77# test_ko_bad_state_1
78# ==============================================================================
81@pytest.mark.asyncio()
82async def test_ko_bad_state_1(async_client, tmp_path):
83 """
84 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with bad state.
86 Arguments:
87 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
88 tmp_path: The temporary path given for this test, declared in `conftest.py`.
89 """
90 # Assert HTTP client is created.
91 assert async_client
93 # Create dummy projects.
94 create_dummy_projects(
95 tmp_path=tmp_path,
96 list_of_dummy_project_ids=[
97 "1m_CLUSTERING_TODO",
98 ],
99 )
101 # Get texts before test.
102 response_get_texts_before = await async_client.get(
103 url="/api/projects/1m_CLUSTERING_TODO/texts?without_deleted_texts=false"
104 )
105 assert response_get_texts_before.status_code == 200
106 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
108 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
109 response_put = await async_client.put(url="/api/projects/1m_CLUSTERING_TODO/texts/14/undelete")
110 assert response_put.status_code == 403
111 assert response_put.json() == {
112 "detail": "The project with id '1m_CLUSTERING_TODO' doesn't allow modification during this state (state='CLUSTERING_TODO').",
113 }
115 # Assert route `GET /api/projects/{project_id}/texts` is still the same.
116 response_get_texts_after = await async_client.get(
117 url="/api/projects/1m_CLUSTERING_TODO/texts?without_deleted_texts=false"
118 )
119 assert response_get_texts_after.status_code == 200
120 assert response_get_texts_after.json() == response_get_texts_before.json()
121 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is True
124# ==============================================================================
125# test_ko_bad_state_2
126# ==============================================================================
129@pytest.mark.asyncio()
130async def test_ko_bad_state_2(async_client, tmp_path):
131 """
132 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with bad state.
134 Arguments:
135 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
136 tmp_path: The temporary path given for this test, declared in `conftest.py`.
137 """
138 # Assert HTTP client is created.
139 assert async_client
141 # Create dummy projects.
142 create_dummy_projects(
143 tmp_path=tmp_path,
144 list_of_dummy_project_ids=[
145 "1k_ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS",
146 ],
147 )
149 # Get texts before test.
150 response_get_texts_before = await async_client.get(
151 url="/api/projects/1k_ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false"
152 )
153 assert response_get_texts_before.status_code == 200
154 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
156 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
157 response_put = await async_client.put(
158 url="/api/projects/1k_ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS/texts/14/undelete"
159 )
160 assert response_put.status_code == 403
161 assert response_put.json() == {
162 "detail": "The project with id '1k_ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS' doesn't allow modification during this state (state='ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS').",
163 }
165 # Assert route `GET /api/projects/{project_id}/texts` is still the same.
166 response_get_texts_after = await async_client.get(
167 url="/api/projects/1k_ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false"
168 )
169 assert response_get_texts_after.status_code == 200
170 assert response_get_texts_after.json() == response_get_texts_before.json()
171 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is True
174# ==============================================================================
175# test_ok_good_state_1
176# ==============================================================================
179@pytest.mark.asyncio()
180async def test_ok_good_state_1(async_client, tmp_path):
181 """
182 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with good state.
184 Arguments:
185 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
186 tmp_path: The temporary path given for this test, declared in `conftest.py`.
187 """
188 # Assert HTTP client is created.
189 assert async_client
191 # Create dummy projects.
192 create_dummy_projects(
193 tmp_path=tmp_path,
194 list_of_dummy_project_ids=[
195 "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
196 ],
197 )
199 # Get texts before test.
200 response_get_texts_before = await async_client.get(
201 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false"
202 )
203 assert response_get_texts_before.status_code == 200
204 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
206 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
207 response_put = await async_client.put(
208 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/14/undelete"
209 )
210 assert response_put.status_code == 202
211 assert response_put.json() == {
212 "project_id": "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
213 "text_id": "14",
214 "detail": "In project with id '1l_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '14' has been undeleted. Several constraints have been unhidden.",
215 }
217 # Assert route `GET /api/projects/{project_id}/texts` is updated.
218 response_get_texts_after = await async_client.get(
219 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false"
220 )
221 assert response_get_texts_after.status_code == 200
222 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is False
224 # Assert route `GET /api/projects/{project_id}/status` is updated.
225 response_get_status = await async_client.get(url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/status")
226 assert response_get_status.status_code == 200
227 assert response_get_status.json()["status"]["iteration_id"] == 1
228 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS"
230 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
231 response_get_constraints = await async_client.get(
232 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints"
233 )
234 assert response_get_constraints.status_code == 200
235 for constraint_value in response_get_constraints.json()["constraints"].values():
236 data_id_1 = constraint_value["data"]["id_1"]
237 data_id_2 = constraint_value["data"]["id_2"]
238 assert constraint_value["is_hidden"] is (
239 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"]
240 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"]
241 )
244# ==============================================================================
245# test_ok_good_state_2
246# ==============================================================================
249@pytest.mark.asyncio()
250async def test_ok_good_state_2(async_client, tmp_path):
251 """
252 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with good state.
254 Arguments:
255 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
256 tmp_path: The temporary path given for this test, declared in `conftest.py`.
257 """
258 # Assert HTTP client is created.
259 assert async_client
261 # Create dummy projects.
262 create_dummy_projects(
263 tmp_path=tmp_path,
264 list_of_dummy_project_ids=[
265 "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS",
266 ],
267 )
269 # Get texts before test.
270 response_get_texts_before = await async_client.get(
271 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false"
272 )
273 assert response_get_texts_before.status_code == 200
274 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
276 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
277 response_put = await async_client.put(
278 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts/14/undelete"
279 )
280 assert response_put.status_code == 202
281 assert response_put.json() == {
282 "project_id": "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS",
283 "text_id": "14",
284 "detail": "In project with id '1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS', the text with id '14' has been undeleted. Several constraints have been unhidden.",
285 }
287 # Assert route `GET /api/projects/{project_id}/texts` is updated.
288 response_get_texts_after = await async_client.get(
289 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false"
290 )
291 assert response_get_texts_after.status_code == 200
292 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is False
294 # Assert route `GET /api/projects/{project_id}/status` is updated.
295 response_get_status = await async_client.get(
296 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/status"
297 )
298 assert response_get_status.status_code == 200
299 assert response_get_status.json()["status"]["iteration_id"] == 1
300 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS"
302 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
303 response_get_constraints = await async_client.get(
304 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/constraints"
305 )
306 assert response_get_constraints.status_code == 200
307 for constraint_value in response_get_constraints.json()["constraints"].values():
308 data_id_1 = constraint_value["data"]["id_1"]
309 data_id_2 = constraint_value["data"]["id_2"]
310 assert constraint_value["is_hidden"] is (
311 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"]
312 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"]
313 )
316# ==============================================================================
317# test_ok_good_state_3
318# ==============================================================================
321@pytest.mark.asyncio()
322async def test_ok_good_state_3(async_client, tmp_path):
323 """
324 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with good state.
326 Arguments:
327 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
328 tmp_path: The temporary path given for this test, declared in `conftest.py`.
329 """
330 # Assert HTTP client is created.
331 assert async_client
333 # Create dummy projects.
334 create_dummy_projects(
335 tmp_path=tmp_path,
336 list_of_dummy_project_ids=[
337 "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS",
338 ],
339 )
341 # Get texts before test.
342 response_get_texts_before = await async_client.get(
343 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false"
344 )
345 assert response_get_texts_before.status_code == 200
346 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
348 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
349 response_put = await async_client.put(
350 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts/14/undelete"
351 )
352 assert response_put.status_code == 202
353 assert response_put.json() == {
354 "project_id": "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS",
355 "text_id": "14",
356 "detail": "In project with id '1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS', the text with id '14' has been undeleted. Several constraints have been unhidden.",
357 }
359 # Assert route `GET /api/projects/{project_id}/texts` is updated.
360 response_get_texts_after = await async_client.get(
361 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false"
362 )
363 assert response_get_texts_after.status_code == 200
364 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is False
366 # Assert route `GET /api/projects/{project_id}/status` is updated.
367 response_get_status = await async_client.get(
368 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/status"
369 )
370 assert response_get_status.status_code == 200
371 assert response_get_status.json()["status"]["iteration_id"] == 1
372 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS"
374 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
375 response_get_constraints = await async_client.get(
376 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/constraints"
377 )
378 assert response_get_constraints.status_code == 200
379 for constraint_value in response_get_constraints.json()["constraints"].values():
380 data_id_1 = constraint_value["data"]["id_1"]
381 data_id_2 = constraint_value["data"]["id_2"]
382 assert constraint_value["is_hidden"] is (
383 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"]
384 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"]
385 )
388# ==============================================================================
389# test_ok_double_request
390# ==============================================================================
393@pytest.mark.asyncio()
394async def test_ok_double_request(async_client, tmp_path):
395 """
396 Test the `PUT /api/projects/{project_id}/texts/{text_id}/undelete` route with double request.
398 Arguments:
399 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
400 tmp_path: The temporary path given for this test, declared in `conftest.py`.
401 """
402 # Assert HTTP client is created.
403 assert async_client
405 # Create dummy projects.
406 create_dummy_projects(
407 tmp_path=tmp_path,
408 list_of_dummy_project_ids=[
409 "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
410 ],
411 )
413 # Get texts before test.
414 response_get_texts_before = await async_client.get(
415 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false"
416 )
417 assert response_get_texts_before.status_code == 200
418 assert response_get_texts_before.json()["texts"]["14"]["is_deleted"] is True
420 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
421 response_put1 = await async_client.put(
422 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/14/undelete"
423 )
424 assert response_put1.status_code == 202
425 assert response_put1.json() == {
426 "project_id": "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
427 "text_id": "14",
428 "detail": "In project with id '1l_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '14' has been undeleted. Several constraints have been unhidden.",
429 }
431 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/undelete` works.
432 response_put2 = await async_client.put(
433 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/14/undelete"
434 )
435 assert response_put2.status_code == 202
436 assert response_put2.json() == {
437 "project_id": "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
438 "text_id": "14",
439 "detail": "In project with id '1l_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '14' has been undeleted. Several constraints have been unhidden.",
440 }
442 # Assert route `GET /api/projects/{project_id}/texts` is updated.
443 response_get_texts_after = await async_client.get(
444 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false"
445 )
446 assert response_get_texts_after.status_code == 200
447 assert response_get_texts_after.json()["texts"]["14"]["is_deleted"] is False
449 # Assert route `GET /api/projects/{project_id}/status` is updated.
450 response_get_status = await async_client.get(url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/status")
451 assert response_get_status.status_code == 200
452 assert response_get_status.json()["status"]["iteration_id"] == 1
453 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS"
455 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
456 response_get_constraints = await async_client.get(
457 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints"
458 )
459 assert response_get_constraints.status_code == 200
460 for constraint_value in response_get_constraints.json()["constraints"].values():
461 data_id_1 = constraint_value["data"]["id_1"]
462 data_id_2 = constraint_value["data"]["id_2"]
463 assert constraint_value["is_hidden"] is (
464 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"]
465 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"]
466 )