|
28 | 28 | pytest_simcore_core_services_selection = ["rabbit"] |
29 | 29 |
|
30 | 30 |
|
| 31 | +async def _list_functions_and_validate( |
| 32 | + client: TestClient, |
| 33 | + expected_status: HTTPStatus, |
| 34 | + expected_count: int | None = None, |
| 35 | + params: dict[str, Any] | None = None, |
| 36 | + expected_uid_in_results: str | None = None, |
| 37 | + expected_uid_at_index: tuple[str, int] | None = None, |
| 38 | +) -> list[RegisteredFunctionGet] | None: |
| 39 | + """Helper function to list functions and validate the response.""" |
| 40 | + url = client.app.router["list_functions"].url_for() |
| 41 | + response = await client.get(url, params=params or {}) |
| 42 | + data, error = await assert_status(response, expected_status) |
| 43 | + |
| 44 | + if error: |
| 45 | + return None |
| 46 | + |
| 47 | + retrieved_functions = TypeAdapter(list[RegisteredFunctionGet]).validate_python(data) |
| 48 | + |
| 49 | + if expected_count is not None: |
| 50 | + assert len(retrieved_functions) == expected_count |
| 51 | + |
| 52 | + if expected_uid_in_results is not None: |
| 53 | + assert expected_uid_in_results in [f"{f.uid}" for f in retrieved_functions] |
| 54 | + |
| 55 | + if expected_uid_at_index is not None: |
| 56 | + expected_uid, index = expected_uid_at_index |
| 57 | + assert f"{retrieved_functions[index].uid}" == expected_uid |
| 58 | + |
| 59 | + return retrieved_functions |
| 60 | + |
| 61 | + |
31 | 62 | @pytest.fixture(params=[FunctionClass.PROJECT, FunctionClass.SOLVER]) |
32 | 63 | def mocked_function(request) -> dict[str, Any]: |
33 | 64 | function_dict = { |
@@ -127,55 +158,42 @@ async def test_function_workflow( |
127 | 158 | assert retrieved_function.uid == returned_function.uid |
128 | 159 |
|
129 | 160 | # List existing functions (default) |
130 | | - url = client.app.router["list_functions"].url_for() |
131 | | - response = await client.get(url) |
132 | | - data, error = await assert_status(response, expected_list) |
133 | | - if not error: |
134 | | - retrieved_functions = TypeAdapter(list[RegisteredFunctionGet]).validate_python( |
135 | | - data |
136 | | - ) |
137 | | - assert len(retrieved_functions) == 2 |
138 | | - assert returned_function_uid in [f.uid for f in retrieved_functions] |
139 | | - assert ( |
140 | | - retrieved_functions[1].uid == returned_function_uid |
141 | | - ) # ordered by modified_at by default |
| 161 | + await _list_functions_and_validate( |
| 162 | + client, |
| 163 | + expected_list, |
| 164 | + expected_count=2, |
| 165 | + expected_uid_in_results=str(returned_function_uid), |
| 166 | + expected_uid_at_index=( |
| 167 | + str(returned_function_uid), |
| 168 | + 1, |
| 169 | + ), # ordered by modified_at by default |
| 170 | + ) |
142 | 171 |
|
143 | 172 | # List existing functions (ordered by created_at ascending) |
144 | | - url = client.app.router["list_functions"].url_for() |
145 | | - response = await client.get( |
146 | | - url, |
| 173 | + await _list_functions_and_validate( |
| 174 | + client, |
| 175 | + expected_list, |
| 176 | + expected_count=2, |
147 | 177 | params={"order_by": json_dumps({"field": "created_at", "direction": "asc"})}, |
| 178 | + expected_uid_in_results=str(returned_function_uid), |
| 179 | + expected_uid_at_index=(str(returned_function_uid), 0), |
148 | 180 | ) |
149 | | - data, error = await assert_status(response, expected_list) |
150 | | - if not error: |
151 | | - retrieved_functions = TypeAdapter(list[RegisteredFunctionGet]).validate_python( |
152 | | - data |
153 | | - ) |
154 | | - assert len(retrieved_functions) == 2 |
155 | | - assert returned_function_uid in [f.uid for f in retrieved_functions] |
156 | | - assert retrieved_functions[0].uid == returned_function_uid |
157 | 181 |
|
158 | 182 | # List existing functions (searching for not existing) |
159 | | - url = client.app.router["list_functions"].url_for() |
160 | | - response = await client.get( |
161 | | - url, params={"search": "you_can_not_find_me_because_I_do_not_exist"} |
| 183 | + await _list_functions_and_validate( |
| 184 | + client, |
| 185 | + expected_list, |
| 186 | + expected_count=0, |
| 187 | + params={"search": "you_can_not_find_me_because_I_do_not_exist"}, |
162 | 188 | ) |
163 | | - data, error = await assert_status(response, expected_list) |
164 | | - if not error: |
165 | | - retrieved_functions = TypeAdapter(list[RegisteredFunctionGet]).validate_python( |
166 | | - data |
167 | | - ) |
168 | | - assert len(retrieved_functions) == 0 |
169 | 189 |
|
170 | 190 | # List existing functions (searching for duplicate) |
171 | | - url = client.app.router["list_functions"].url_for() |
172 | | - response = await client.get(url, params={"search": "duplicate"}) |
173 | | - data, error = await assert_status(response, expected_list) |
174 | | - if not error: |
175 | | - retrieved_functions = TypeAdapter(list[RegisteredFunctionGet]).validate_python( |
176 | | - data |
177 | | - ) |
178 | | - assert len(retrieved_functions) == 1 |
| 191 | + await _list_functions_and_validate( |
| 192 | + client, |
| 193 | + expected_list, |
| 194 | + expected_count=1, |
| 195 | + params={"search": "duplicate"}, |
| 196 | + ) |
179 | 197 |
|
180 | 198 | # List existing functions (searching by title) |
181 | 199 | url = client.app.router["list_functions"].url_for() |
|
0 commit comments