3
3
from .utils import (
4
4
cli , db ,
5
5
assert_api_get_response , assert_api_post_response , compare_partial ,
6
- add_flow , add_run , add_step , add_task , update_objects_with_run_tags
6
+ add_flow , add_run , add_step , add_task , add_metadata , update_objects_with_run_tags
7
7
)
8
8
import pytest
9
9
@@ -185,6 +185,99 @@ async def test_tasks_get(cli, db):
185
185
# getting tasks for non-existent step should return empty list
186
186
await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/nonexistent/tasks" .format (** _first_task ), status = 200 , data = [])
187
187
188
+ async def test_filtered_tasks_get (cli , db ):
189
+ # create a flow, run and step for the test
190
+ _flow = (await add_flow (db , "TestFlow" , "test_user-1" , ["a_tag" , "b_tag" ], ["runtime:test" ])).body
191
+ _run = (await add_run (db , flow_id = _flow ["flow_id" ])).body
192
+ _step = (await add_step (db , flow_id = _run ["flow_id" ], run_number = _run ["run_number" ], step_name = "first_step" )).body
193
+
194
+ # add tasks to the step
195
+ _first_task = (await add_task (db , flow_id = _step ["flow_id" ], run_number = _step ["run_number" ], step_name = _step ["step_name" ])).body
196
+ _second_task = (await add_task (db , flow_id = _step ["flow_id" ], run_number = _step ["run_number" ], step_name = _step ["step_name" ])).body
197
+ _third_task = (await add_task (db , flow_id = _step ["flow_id" ], run_number = _step ["run_number" ], step_name = _step ["step_name" ])).body
198
+
199
+ # add metadata to filter on
200
+ (await add_metadata (db , flow_id = _first_task ["flow_id" ], run_number = _first_task ["run_number" ], step_name = _first_task ["step_name" ], task_id = _first_task ["task_id" ], metadata = {"field_name" :"field_a" , "value" : "value_a" }))
201
+ (await add_metadata (db , flow_id = _first_task ["flow_id" ], run_number = _first_task ["run_number" ], step_name = _first_task ["step_name" ], task_id = _first_task ["task_id" ], metadata = {"field_name" :"field_b" , "value" : "value_b" }))
202
+
203
+ (await add_metadata (db , flow_id = _second_task ["flow_id" ], run_number = _second_task ["run_number" ], step_name = _second_task ["step_name" ], task_id = _second_task ["task_id" ], metadata = {"field_name" : "field_a" , "value" : "not_value_a" }))
204
+ (await add_metadata (db , flow_id = _second_task ["flow_id" ], run_number = _second_task ["run_number" ], step_name = _second_task ["step_name" ], task_id = _second_task ["task_id" ], metadata = {"field_name" : "field_b" , "value" : "value_b" }))
205
+
206
+ # filtering with a shared key should return all relevant tasks
207
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a" .format (** _first_task ),
208
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
209
+
210
+ # filtering with a shared value should return all relevant tasks
211
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?pattern=value_b" .format (** _first_task ),
212
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
213
+
214
+ # filtering with a regexp should return all relevant tasks
215
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?pattern=value_.*" .format (** _first_task ),
216
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
217
+
218
+ # filtering with a shared key&value should return all relevant tasks
219
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_b&pattern=value_b" .format (** _first_task ),
220
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
221
+
222
+ # filtering with a shared value should return all relevant tasks
223
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a&pattern=not_value_a" .format (** _first_task ),
224
+ data = [task_pathspec (_second_task )])
225
+
226
+ # filtering with a mixed key&value should not return results
227
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a&pattern=value_b" .format (** _first_task ),
228
+ data = [])
229
+
230
+ # not providing filters should return all
231
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks" .format (** _first_task ), data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
232
+
233
+
234
+ async def test_filtered_tasks_mixed_ids_get (cli , db ):
235
+ # create a flow, run and step for the test
236
+ _flow = (await add_flow (db , "TestFlow" , "test_user-1" , ["a_tag" , "b_tag" ], ["runtime:test" ])).body
237
+ _run = (await add_run (db , flow_id = _flow ["flow_id" ])).body
238
+ _step = (await add_step (db , flow_id = _run ["flow_id" ], run_number = _run ["run_number" ], step_name = "first_step" )).body
239
+
240
+ # add tasks to the step
241
+ _first_task = (await add_task (db , flow_id = _step ["flow_id" ], run_number = _step ["run_number" ], step_name = _step ["step_name" ], task_name = "first-task-1" )).body
242
+ # we need to refetch the task as the return does not contain the internal task ID we need for further record creation.
243
+ _first_task = (await db .task_table_postgres .get_task (flow_id = _step ["flow_id" ], run_id = _step ["run_number" ], step_name = _step ["step_name" ], task_id = "first-task-1" , expanded = True )).body
244
+ _second_task = (await add_task (db , flow_id = _step ["flow_id" ], run_number = _step ["run_number" ], step_name = _step ["step_name" ])).body
245
+
246
+ # add metadata to filter on
247
+ (await add_metadata (db , flow_id = _first_task ["flow_id" ], run_number = _first_task ["run_number" ], step_name = _first_task ["step_name" ], task_id = _first_task ['task_id' ], task_name = _first_task ["task_name" ], metadata = {"field_name" :"field_a" , "value" : "value_a" }))
248
+ (await add_metadata (db , flow_id = _first_task ["flow_id" ], run_number = _first_task ["run_number" ], step_name = _first_task ["step_name" ], task_id = _first_task ['task_id' ], task_name = _first_task ["task_name" ], metadata = {"field_name" :"field_b" , "value" : "value_b" }))
249
+
250
+ (await add_metadata (db , flow_id = _second_task ["flow_id" ], run_number = _second_task ["run_number" ], step_name = _second_task ["step_name" ], task_id = _second_task ["task_id" ], metadata = {"field_name" : "field_a" , "value" : "not_value_a" }))
251
+ (await add_metadata (db , flow_id = _second_task ["flow_id" ], run_number = _second_task ["run_number" ], step_name = _second_task ["step_name" ], task_id = _second_task ["task_id" ], metadata = {"field_name" : "field_b" , "value" : "value_b" }))
252
+
253
+ # filtering with a shared key should return all relevant tasks
254
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a" .format (** _first_task ),
255
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
256
+
257
+ # filtering with a shared value should return all relevant tasks
258
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?pattern=value_b" .format (** _first_task ),
259
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
260
+
261
+ # # filtering with a regexp should return all relevant tasks
262
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?pattern=value_.*" .format (** _first_task ),
263
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
264
+
265
+ # filtering with a shared key&value should return all relevant tasks
266
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_b&pattern=value_b" .format (** _first_task ),
267
+ data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
268
+
269
+ # filtering with a shared value should return all relevant tasks
270
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a&pattern=not_value_a" .format (** _first_task ),
271
+ data = [task_pathspec (_second_task )])
272
+
273
+ # filtering with a mixed key&value should not return results
274
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks?metadata_field_name=field_a&pattern=value_b" .format (** _first_task ),
275
+ data = [])
276
+
277
+ # not providing filters should return all
278
+ await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/filtered_tasks" .format (** _first_task ), data = [task_pathspec (_first_task ), task_pathspec (_second_task )])
279
+
280
+
188
281
189
282
async def test_task_get (cli , db ):
190
283
# create flow, run and step for test
@@ -206,3 +299,9 @@ async def test_task_get(cli, db):
206
299
await assert_api_get_response (cli , "/flows/{flow_id}/runs/1234/steps/{step_name}/tasks/{task_id}" .format (** _task ), status = 404 )
207
300
await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/nonexistent_step/tasks/{task_id}" .format (** _task ), status = 404 )
208
301
await assert_api_get_response (cli , "/flows/{flow_id}/runs/{run_number}/steps/{step_name}/tasks/1234" .format (** _task ), status = 404 )
302
+
303
+
304
+ # Helpers
305
+
306
+ def task_pathspec (task_dict ):
307
+ return f"{ task_dict ['flow_id' ]} /{ task_dict ['run_number' ]} /{ task_dict ['step_name' ]} /{ task_dict .get ('task_name' , task_dict ['task_id' ])} "
0 commit comments