Coverage for tests\test_get_api_projects_settings.py: 100.00%
65 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_get_api_projects_settings.py
5* Description: Unittests for `app` module on the `GET /api/projects/{project_id}/settings` 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 json
17import pytest
19from tests.dummies_utils import create_dummy_projects
21# ==============================================================================
22# test_ko_not_found
23# ==============================================================================
26@pytest.mark.asyncio()
27async def test_ko_not_found(async_client):
28 """
29 Test the `GET /api/projects/{project_id}/settings` 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 `GET /api/projects/{project_id}/settings` works.
38 response_get = await async_client.get(url="/api/projects/UNKNOWN_PROJECT/settings")
39 assert response_get.status_code == 404
40 assert response_get.json() == {
41 "detail": "The project with id 'UNKNOWN_PROJECT' doesn't exist.",
42 }
45# ==============================================================================
46# test_ko_bad_iteration_id
47# ==============================================================================
50@pytest.mark.asyncio()
51async def test_ko_bad_iteration_id(async_client, tmp_path):
52 """
53 Test the `GET /api/projects/{project_id}/settings` route with not existing iteration id.
55 Arguments:
56 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
57 tmp_path: The temporary path given for this test, declared in `conftest.py`.
58 """
59 # Assert HTTP client is created.
60 assert async_client
62 # Create dummy projects.
63 create_dummy_projects(
64 tmp_path=tmp_path,
65 list_of_dummy_project_ids=[
66 "0a_INITIALIZATION_WITHOUT_MODELIZATION",
67 ],
68 )
70 # Assert route `GET /api/projects/{project_id}/settings` works.
71 response_get = await async_client.get(
72 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/settings?iteration_id=99"
73 )
74 assert response_get.status_code == 404
75 assert response_get.json() == {
76 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' has no iteration with id '99'.",
77 }
80# ==============================================================================
81# test_ko_bad_settings_name
82# ==============================================================================
85@pytest.mark.asyncio()
86async def test_ko_bad_settings_name(async_client, tmp_path):
87 """
88 Test the `GET /api/projects/{project_id}/settings` route with bad settings name.
90 Arguments:
91 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
92 tmp_path: The temporary path given for this test, declared in `conftest.py`.
93 """
94 # Assert HTTP client is created.
95 assert async_client
97 # Create dummy projects.
98 create_dummy_projects(
99 tmp_path=tmp_path,
100 list_of_dummy_project_ids=[
101 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
102 ],
103 )
105 # Assert route `GET /api/projects/{project_id}/settings` works.
106 response_get = await async_client.get(
107 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/settings?settings_names=UNKNOWN_SETTINGS"
108 )
109 assert response_get.status_code == 422
112# ==============================================================================
113# test_ok_default
114# ==============================================================================
117@pytest.mark.asyncio()
118async def test_ok_default(async_client, tmp_path):
119 """
120 Test the `GET /api/projects/{project_id}/settings` route with some projects.
122 Arguments:
123 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
124 tmp_path: The temporary path given for this test, declared in `conftest.py`.
125 """
126 # Assert HTTP client is created.
127 assert async_client
129 # Create dummy projects.
130 create_dummy_projects(
131 tmp_path=tmp_path,
132 list_of_dummy_project_ids=[
133 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
134 ],
135 )
137 # Assert route `GET /api/projects/{project_id}/settings` works.
138 response_get = await async_client.get(url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/settings")
139 assert response_get.status_code == 200
140 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "parameters", "settings"]
141 assert response_get.json() == {
142 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
143 "iteration_id": 2,
144 "parameters": {"settings_names": ["preprocessing", "vectorization", "sampling", "clustering"]},
145 "settings": {
146 "sampling": {
147 "algorithm": "custom",
148 "random_seed": 42,
149 "nb_to_select": 999,
150 "init_kargs": {
151 "clusters_restriction": "same_cluster",
152 "distance_restriction": "closest_neighbors",
153 "without_inferred_constraints": True,
154 },
155 },
156 "preprocessing": {
157 "apply_stopwords_deletion": False,
158 "apply_parsing_filter": False,
159 "apply_lemmatization": False,
160 "spacy_language_model": "fr_core_news_md",
161 },
162 "vectorization": {"vectorizer_type": "tfidf", "spacy_language_model": None, "random_seed": 42},
163 "clustering": {
164 "algorithm": "kmeans",
165 "random_seed": 42,
166 "nb_clusters": 3,
167 "init_kargs": {"model": "COP", "max_iteration": 150, "tolerance": 0.0001},
168 },
169 },
170 }
172 # Assert file content is the same.
173 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "settings.json", "r") as settings_fileobject:
174 assert response_get.json()["settings"] == json.load(settings_fileobject)["2"]
177# ==============================================================================
178# test_ok_with_iteration_id_1
179# ==============================================================================
182@pytest.mark.asyncio()
183async def test_ok_with_iteration_id_1(async_client, tmp_path):
184 """
185 Test the `GET /api/projects/{project_id}/settings` route with some projects.
187 Arguments:
188 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
189 tmp_path: The temporary path given for this test, declared in `conftest.py`.
190 """
191 # Assert HTTP client is created.
192 assert async_client
194 # Create dummy projects.
195 create_dummy_projects(
196 tmp_path=tmp_path,
197 list_of_dummy_project_ids=[
198 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
199 ],
200 )
202 # Assert route `GET /api/projects/{project_id}/settings` works.
203 response_get = await async_client.get(
204 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/settings?iteration_id=2"
205 )
206 assert response_get.status_code == 200
207 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "parameters", "settings"]
208 assert response_get.json() == {
209 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
210 "iteration_id": 2,
211 "parameters": {"settings_names": ["preprocessing", "vectorization", "sampling", "clustering"]},
212 "settings": {
213 "sampling": {
214 "algorithm": "custom",
215 "random_seed": 42,
216 "nb_to_select": 999,
217 "init_kargs": {
218 "clusters_restriction": "same_cluster",
219 "distance_restriction": "closest_neighbors",
220 "without_inferred_constraints": True,
221 },
222 },
223 "preprocessing": {
224 "apply_stopwords_deletion": False,
225 "apply_parsing_filter": False,
226 "apply_lemmatization": False,
227 "spacy_language_model": "fr_core_news_md",
228 },
229 "vectorization": {"vectorizer_type": "tfidf", "spacy_language_model": None, "random_seed": 42},
230 "clustering": {
231 "algorithm": "kmeans",
232 "random_seed": 42,
233 "nb_clusters": 3,
234 "init_kargs": {"model": "COP", "max_iteration": 150, "tolerance": 0.0001},
235 },
236 },
237 }
239 # Assert file content is the same.
240 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "settings.json", "r") as settings_fileobject:
241 assert response_get.json()["settings"] == json.load(settings_fileobject)["2"]
244# ==============================================================================
245# test_ok_with_iteration_id_2
246# ==============================================================================
249@pytest.mark.asyncio()
250async def test_ok_with_iteration_id_2(async_client, tmp_path):
251 """
252 Test the `GET /api/projects/{project_id}/settings` route with some projects.
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 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
266 ],
267 )
269 # Assert route `GET /api/projects/{project_id}/settings` works.
270 response_get = await async_client.get(
271 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/settings?iteration_id=0"
272 )
273 assert response_get.status_code == 200
274 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "parameters", "settings"]
275 assert response_get.json() == {
276 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
277 "iteration_id": 0,
278 "parameters": {"settings_names": ["preprocessing", "vectorization", "sampling", "clustering"]},
279 "settings": {
280 "preprocessing": {
281 "apply_stopwords_deletion": False,
282 "apply_parsing_filter": False,
283 "apply_lemmatization": False,
284 "spacy_language_model": "fr_core_news_md",
285 },
286 "vectorization": {"vectorizer_type": "tfidf", "spacy_language_model": None, "random_seed": 42},
287 "clustering": {
288 "algorithm": "kmeans",
289 "random_seed": 42,
290 "nb_clusters": 3,
291 "init_kargs": {"model": "COP", "max_iteration": 150, "tolerance": 0.0001},
292 },
293 },
294 }
296 # Assert file content is the same.
297 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "settings.json", "r") as settings_fileobject:
298 assert response_get.json()["settings"] == json.load(settings_fileobject)["0"]
301# ==============================================================================
302# test_ok_with_settings_name
303# ==============================================================================
306@pytest.mark.asyncio()
307async def test_ok_with_settings_name(async_client, tmp_path):
308 """
309 Test the `GET /api/projects/{project_id}/settings` route with some projects.
311 Arguments:
312 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
313 tmp_path: The temporary path given for this test, declared in `conftest.py`.
314 """
315 # Assert HTTP client is created.
316 assert async_client
318 # Create dummy projects.
319 create_dummy_projects(
320 tmp_path=tmp_path,
321 list_of_dummy_project_ids=[
322 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
323 ],
324 )
326 # Assert route `GET /api/projects/{project_id}/settings` works.
327 response_get = await async_client.get(
328 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/settings?settings_names=preprocessing&settings_names=vectorization"
329 )
330 assert response_get.status_code == 200
331 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "parameters", "settings"]
332 assert response_get.json() == {
333 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION",
334 "iteration_id": 2,
335 "parameters": {"settings_names": ["preprocessing", "vectorization"]},
336 "settings": {
337 "preprocessing": {
338 "apply_stopwords_deletion": False,
339 "apply_parsing_filter": False,
340 "apply_lemmatization": False,
341 "spacy_language_model": "fr_core_news_md",
342 },
343 "vectorization": {"vectorizer_type": "tfidf", "spacy_language_model": None, "random_seed": 42},
344 },
345 }
347 # Assert file content is the same.
348 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "settings.json", "r") as settings_fileobject:
349 project_settings = json.load(settings_fileobject)["2"]
350 assert response_get.json()["settings"]["preprocessing"] == project_settings["preprocessing"]
351 assert response_get.json()["settings"]["vectorization"] == project_settings["vectorization"]