Coverage for tests\test_put_api_projects_constraints_comment.py: 100.00%
57 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_constraints_comment.py
5* Description: Unittests for `app` module on the `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` 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# ==============================================================================
15from urllib.parse import quote_plus, urlencode
17import pytest
19from tests.dummies_utils import create_dummy_projects
21# ==============================================================================
22# test_ko_not_found_1
23# ==============================================================================
26@pytest.mark.asyncio()
27async def test_ko_not_found_1(async_client):
28 """
29 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` route with not existing project.
31 Arguments:
32 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
33 """
34 # Assert HTTP client is created.
35 assert async_client
37 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` works.
38 response_put = await async_client.put(
39 url="/api/projects/UNKNOWN_PROJECT/constraints/UNKNOWN_CONSTRAINT/comment?"
40 + urlencode(
41 {"constraint_comment": "Changement de valeur !"},
42 quote_via=quote_plus,
43 )
44 )
45 assert response_put.status_code == 404
46 assert response_put.json() == {
47 "detail": "The project with id 'UNKNOWN_PROJECT' doesn't exist.",
48 }
51# ==============================================================================
52# test_ko_not_found_2
53# ==============================================================================
56@pytest.mark.asyncio()
57async def test_ko_not_found_2(async_client, tmp_path):
58 """
59 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` route with not existing project.
61 Arguments:
62 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
63 tmp_path: The temporary path given for this test, declared in `conftest.py`.
64 """
65 # Assert HTTP client is created.
66 assert async_client
68 # Create dummy projects.
69 create_dummy_projects(
70 tmp_path=tmp_path,
71 list_of_dummy_project_ids=[
72 "1a_SAMPLING_TODO",
73 ],
74 )
76 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` works.
77 response_put = await async_client.put(
78 url="/api/projects/1a_SAMPLING_TODO/constraints/UNKNOWN_CONSTRAINT/comment?"
79 + urlencode(
80 {"constraint_comment": "Changement de valeur !"},
81 quote_via=quote_plus,
82 )
83 )
84 assert response_put.status_code == 404
85 assert response_put.json() == {
86 "detail": "In project with id '1a_SAMPLING_TODO', the constraint with id 'UNKNOWN_CONSTRAINT' to annotate doesn't exist.",
87 }
90# ==============================================================================
91# test_ok_good_state_1
92# ==============================================================================
95@pytest.mark.asyncio()
96async def test_ok_good_state_1(async_client, tmp_path):
97 """
98 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` route with good state.
100 Arguments:
101 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
102 tmp_path: The temporary path given for this test, declared in `conftest.py`.
103 """
104 # Assert HTTP client is created.
105 assert async_client
107 # Create dummy projects.
108 create_dummy_projects(
109 tmp_path=tmp_path,
110 list_of_dummy_project_ids=[
111 "1m_CLUSTERING_TODO",
112 ],
113 )
115 # Get texts before test.
116 response_get_constraints_before = await async_client.get(
117 url="/api/projects/1m_CLUSTERING_TODO/constraints?without_hidden_constraints=false"
118 )
119 assert response_get_constraints_before.status_code == 200
120 assert response_get_constraints_before.json()["constraints"]["(0,4)"]["comment"] == ""
122 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` works.
123 response_put = await async_client.put(
124 url="/api/projects/1m_CLUSTERING_TODO/constraints/(0,4)/comment?"
125 + urlencode(
126 {"constraint_comment": "Changement de valeur !"},
127 quote_via=quote_plus,
128 )
129 )
130 assert response_put.status_code == 202
131 assert response_put.json() == {
132 "project_id": "1m_CLUSTERING_TODO",
133 "constraint_id": "(0,4)",
134 "constraint_comment": "Changement de valeur !",
135 "detail": "In project with id '1m_CLUSTERING_TODO', the constraint with id '(0,4)' has been commented.",
136 }
138 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
139 response_get_constraints_after = await async_client.get(
140 url="/api/projects/1m_CLUSTERING_TODO/constraints?without_hidden_constraints=false"
141 )
142 assert response_get_constraints_after.status_code == 200
143 for constraint_id in response_get_constraints_before.json()["constraints"].keys():
144 if constraint_id != "(0,4)":
145 assert (
146 response_get_constraints_after.json()["constraints"][constraint_id]
147 == response_get_constraints_before.json()["constraints"][constraint_id]
148 )
149 assert response_get_constraints_after.json()["constraints"]["(0,4)"]["comment"] == "Changement de valeur !"
151 # Assert route `GET /api/projects/{project_id}/status` is updated.
152 response_get_status = await async_client.get(url="/api/projects/1m_CLUSTERING_TODO/status")
153 assert response_get_status.status_code == 200
154 assert response_get_status.json()["status"]["iteration_id"] == 1
155 assert response_get_status.json()["status"]["state"] == "CLUSTERING_TODO"
158# ==============================================================================
159# test_ok_good_state_2
160# ==============================================================================
163@pytest.mark.asyncio()
164async def test_ok_good_state_2(async_client, tmp_path):
165 """
166 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` route with good state.
168 Arguments:
169 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
170 tmp_path: The temporary path given for this test, declared in `conftest.py`.
171 """
172 # Assert HTTP client is created.
173 assert async_client
175 # Create dummy projects.
176 create_dummy_projects(
177 tmp_path=tmp_path,
178 list_of_dummy_project_ids=[
179 "1p_ITERATION_END",
180 ],
181 )
183 # Get texts before test.
184 response_get_constraints_before = await async_client.get(
185 url="/api/projects/1p_ITERATION_END/constraints?without_hidden_constraints=false"
186 )
187 assert response_get_constraints_before.status_code == 200
188 assert (
189 response_get_constraints_before.json()["constraints"]["(4,7)"]["comment"]
190 == "Même intention (gestion carte virtuelle)."
191 )
193 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/comment` works.
194 response_put = await async_client.put(
195 url="/api/projects/1p_ITERATION_END/constraints/(4,7)/comment?"
196 + urlencode(
197 {"constraint_comment": "Changement de valeur !"},
198 quote_via=quote_plus,
199 )
200 )
201 assert response_put.status_code == 202
202 assert response_put.json() == {
203 "project_id": "1p_ITERATION_END",
204 "constraint_id": "(4,7)",
205 "constraint_comment": "Changement de valeur !",
206 "detail": "In project with id '1p_ITERATION_END', the constraint with id '(4,7)' has been commented.",
207 }
209 # Assert route `GET /api/projects/{project_id}/constraints` is updated.
210 response_get_constraints_after = await async_client.get(
211 url="/api/projects/1p_ITERATION_END/constraints?without_hidden_constraints=false"
212 )
213 assert response_get_constraints_after.status_code == 200
214 for constraint_id in response_get_constraints_before.json()["constraints"].keys():
215 if constraint_id != "(4,7)":
216 assert (
217 response_get_constraints_after.json()["constraints"][constraint_id]
218 == response_get_constraints_before.json()["constraints"][constraint_id]
219 )
220 assert response_get_constraints_after.json()["constraints"]["(4,7)"]["comment"] == "Changement de valeur !"
222 # Assert route `GET /api/projects/{project_id}/status` is updated.
223 response_get_status = await async_client.get(url="/api/projects/1p_ITERATION_END/status")
224 assert response_get_status.status_code == 200
225 assert response_get_status.json()["status"]["iteration_id"] == 1
226 assert response_get_status.json()["status"]["state"] == "ITERATION_END"