Coverage for tests\test_get_api_projects_modelization.py: 100.00%
30 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_modelization.py
5* Description: Unittests for `app` module on the `GET /api/projects/{project_id}/modelization` 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}/modelization` 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}/modelization` works.
38 response_get = await async_client.get(url="/api/projects/UNKNOWN_PROJECT/modelization")
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_ok_1
47# ==============================================================================
50@pytest.mark.asyncio()
51async def test_ok_1(async_client, tmp_path):
52 """
53 Test the `GET /api/projects/{project_id}/modelization` route with some projects.
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}/modelization` works.
71 response_get = await async_client.get(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/modelization")
72 assert response_get.status_code == 200
73 assert list(response_get.json().keys()) == ["project_id", "modelization"]
74 assert response_get.json() == {
75 "project_id": "0a_INITIALIZATION_WITHOUT_MODELIZATION",
76 "modelization": {
77 "0": {"MUST_LINK": ["0"], "CANNOT_LINK": [], "COMPONENT": 0},
78 "1": {"MUST_LINK": ["1"], "CANNOT_LINK": [], "COMPONENT": 1},
79 "2": {"MUST_LINK": ["2"], "CANNOT_LINK": [], "COMPONENT": 2},
80 "3": {"MUST_LINK": ["3"], "CANNOT_LINK": [], "COMPONENT": 3},
81 "4": {"MUST_LINK": ["4"], "CANNOT_LINK": [], "COMPONENT": 4},
82 "5": {"MUST_LINK": ["5"], "CANNOT_LINK": [], "COMPONENT": 5},
83 "6": {"MUST_LINK": ["6"], "CANNOT_LINK": [], "COMPONENT": 6},
84 "7": {"MUST_LINK": ["7"], "CANNOT_LINK": [], "COMPONENT": 7},
85 "8": {"MUST_LINK": ["8"], "CANNOT_LINK": [], "COMPONENT": 8},
86 "9": {"MUST_LINK": ["9"], "CANNOT_LINK": [], "COMPONENT": 9},
87 "10": {"MUST_LINK": ["10"], "CANNOT_LINK": [], "COMPONENT": 10},
88 "11": {"MUST_LINK": ["11"], "CANNOT_LINK": [], "COMPONENT": 11},
89 "12": {"MUST_LINK": ["12"], "CANNOT_LINK": [], "COMPONENT": 12},
90 "13": {"MUST_LINK": ["13"], "CANNOT_LINK": [], "COMPONENT": 13},
91 "14": {"MUST_LINK": ["14"], "CANNOT_LINK": [], "COMPONENT": 14},
92 "15": {"MUST_LINK": ["15"], "CANNOT_LINK": [], "COMPONENT": 15},
93 "16": {"MUST_LINK": ["16"], "CANNOT_LINK": [], "COMPONENT": 16},
94 "17": {"MUST_LINK": ["17"], "CANNOT_LINK": [], "COMPONENT": 17},
95 "18": {"MUST_LINK": ["18"], "CANNOT_LINK": [], "COMPONENT": 18},
96 "19": {"MUST_LINK": ["19"], "CANNOT_LINK": [], "COMPONENT": 19},
97 "20": {"MUST_LINK": ["20"], "CANNOT_LINK": [], "COMPONENT": 20},
98 "21": {"MUST_LINK": ["21"], "CANNOT_LINK": [], "COMPONENT": 21},
99 "22": {"MUST_LINK": ["22"], "CANNOT_LINK": [], "COMPONENT": 22},
100 "23": {"MUST_LINK": ["23"], "CANNOT_LINK": [], "COMPONENT": 23},
101 },
102 }
104 # Assert file content is the same.
105 with open(
106 tmp_path / "0a_INITIALIZATION_WITHOUT_MODELIZATION" / "modelization.json", "r"
107 ) as modelization_fileobject:
108 assert response_get.json()["modelization"] == json.load(modelization_fileobject)
111# ==============================================================================
112# test_ok_2
113# ==============================================================================
116@pytest.mark.asyncio()
117async def test_ok_2(async_client, tmp_path):
118 """
119 Test the `GET /api/projects/{project_id}/modelization` route with some projects.
121 Arguments:
122 async_client: Fixture providing an HTTP client, declared in `conftest.py`.
123 tmp_path: The temporary path given for this test, declared in `conftest.py`.
124 """
125 # Assert HTTP client is created.
126 assert async_client
128 # Create dummy projects.
129 create_dummy_projects(
130 tmp_path=tmp_path,
131 list_of_dummy_project_ids=[
132 "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
133 ],
134 )
136 # Assert route `GET /api/projects/{project_id}/modelization` works.
137 response_get = await async_client.get(url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/modelization")
138 assert response_get.status_code == 200
139 assert list(response_get.json().keys()) == ["project_id", "modelization"]
140 assert response_get.json() == {
141 "project_id": "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION",
142 "modelization": {
143 "0": {
144 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
145 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
146 "COMPONENT": 0,
147 },
148 "1": {
149 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
150 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
151 "COMPONENT": 0,
152 },
153 "2": {
154 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
155 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
156 "COMPONENT": 0,
157 },
158 "3": {
159 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
160 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
161 "COMPONENT": 0,
162 },
163 "4": {
164 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
165 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
166 "COMPONENT": 0,
167 },
168 "5": {"MUST_LINK": ["5"], "CANNOT_LINK": [], "COMPONENT": 1},
169 "6": {
170 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
171 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
172 "COMPONENT": 0,
173 },
174 "7": {
175 "MUST_LINK": ["1", "2", "3", "6", "0", "4", "7"],
176 "CANNOT_LINK": ["22", "20", "21", "17", "19", "18"],
177 "COMPONENT": 0,
178 },
179 "8": {
180 "MUST_LINK": ["8", "11", "10", "12", "9"],
181 "CANNOT_LINK": ["22", "18", "20", "21", "17", "19"],
182 "COMPONENT": 2,
183 },
184 "9": {
185 "MUST_LINK": ["8", "11", "10", "12", "9"],
186 "CANNOT_LINK": ["22", "18", "20", "21", "17", "19"],
187 "COMPONENT": 2,
188 },
189 "10": {
190 "MUST_LINK": ["8", "11", "10", "12", "9"],
191 "CANNOT_LINK": ["22", "18", "20", "21", "17", "19"],
192 "COMPONENT": 2,
193 },
194 "11": {
195 "MUST_LINK": ["8", "11", "10", "12", "9"],
196 "CANNOT_LINK": ["22", "18", "20", "21", "17", "19"],
197 "COMPONENT": 2,
198 },
199 "12": {
200 "MUST_LINK": ["8", "11", "10", "12", "9"],
201 "CANNOT_LINK": ["22", "18", "20", "21", "17", "19"],
202 "COMPONENT": 2,
203 },
204 "13": {"MUST_LINK": ["13"], "CANNOT_LINK": ["16"], "COMPONENT": 3},
205 "15": {"MUST_LINK": ["15"], "CANNOT_LINK": ["16"], "COMPONENT": 4},
206 "16": {"MUST_LINK": ["16"], "CANNOT_LINK": ["15", "13"], "COMPONENT": 5},
207 "17": {
208 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
209 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
210 "COMPONENT": 6,
211 },
212 "18": {
213 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
214 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
215 "COMPONENT": 6,
216 },
217 "19": {
218 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
219 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
220 "COMPONENT": 6,
221 },
222 "20": {
223 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
224 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
225 "COMPONENT": 6,
226 },
227 "21": {
228 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
229 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
230 "COMPONENT": 6,
231 },
232 "22": {
233 "MUST_LINK": ["18", "19", "17", "21", "20", "22"],
234 "CANNOT_LINK": ["8", "11", "10", "12", "9", "6", "1", "2", "3", "0", "4", "7"],
235 "COMPONENT": 6,
236 },
237 "23": {"MUST_LINK": ["23"], "CANNOT_LINK": [], "COMPONENT": 7},
238 },
239 }
241 # Assert file content is the same.
242 with open(
243 tmp_path / "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "modelization.json", "r"
244 ) as modelization_fileobject:
245 assert response_get.json()["modelization"] == json.load(modelization_fileobject)