33# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
55import datetime
6+ import json
67from unittest .mock import MagicMock
78
89import pytest
@@ -309,6 +310,7 @@ def test_get_task_url(root_url):
309310def test_get_task_definition (responses , root_url ):
310311 tid = "abc123"
311312 tc .get_taskcluster_client .cache_clear ()
313+ tc ._task_definitions_cache .cache .clear ()
312314
313315 responses .get (
314316 f"{ root_url } /api/queue/v1/task/{ tid } " ,
@@ -318,6 +320,40 @@ def test_get_task_definition(responses, root_url):
318320 assert result == {"payload" : "blah" }
319321
320322
323+ def test_get_task_definitions (responses , root_url ):
324+ tid = (
325+ "abc123" ,
326+ "def456" ,
327+ )
328+ tc .get_taskcluster_client .cache_clear ()
329+ tc ._task_definitions_cache .cache .clear ()
330+
331+ task_definitions = {
332+ "abc123" : {"payload" : "blah" },
333+ "def456" : {"payload" : "foobar" },
334+ }
335+
336+ def tasks_callback (request ):
337+ payload = json .loads (request .body )
338+ resp_body = {
339+ "tasks" : [
340+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
341+ for task_id in payload ["taskIds" ]
342+ ]
343+ }
344+ return (200 , [], json .dumps (resp_body ))
345+
346+ responses .add_callback (
347+ responses .POST ,
348+ f"{ root_url } /api/queue/v1/tasks" ,
349+ callback = tasks_callback ,
350+ )
351+ result = tc .get_task_definitions (tid )
352+ assert result == task_definitions
353+ result = tc .get_task_definition (tid [0 ])
354+ assert result == {"payload" : "blah" }
355+
356+
321357def test_cancel_task (responses , root_url ):
322358 tid = "abc123"
323359 tc .get_taskcluster_client .cache_clear ()
@@ -487,8 +523,7 @@ def test_list_task_group_incomplete_tasks(responses, root_url):
487523
488524
489525def test_get_ancestors (responses , root_url ):
490- tc .get_task_definition .cache_clear ()
491- tc ._get_deps .cache_clear ()
526+ tc ._task_definitions_cache .cache .clear ()
492527 tc .get_taskcluster_client .cache_clear ()
493528
494529 task_definitions = {
@@ -518,12 +553,20 @@ def test_get_ancestors(responses, root_url):
518553 },
519554 }
520555
521- # Mock API responses for each task definition
522- for task_id , definition in task_definitions .items ():
523- responses .get (
524- f"{ root_url } /api/queue/v1/task/{ task_id } " ,
525- json = definition ,
526- )
556+ # Mock API response for task definitions
557+ def tasks_callback (request ):
558+ payload = json .loads (request .body )
559+ resp_body = {
560+ "tasks" : [
561+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
562+ for task_id in payload ["taskIds" ]
563+ ]
564+ }
565+ return (200 , [], json .dumps (resp_body ))
566+
567+ responses .add_callback (
568+ responses .POST , f"{ root_url } /api/queue/v1/tasks" , callback = tasks_callback
569+ )
527570
528571 got = tc .get_ancestors (["bbb" , "fff" ])
529572 expected = {
@@ -536,8 +579,7 @@ def test_get_ancestors(responses, root_url):
536579
537580
538581def test_get_ancestors_string (responses , root_url ):
539- tc .get_task_definition .cache_clear ()
540- tc ._get_deps .cache_clear ()
582+ tc ._task_definitions_cache .cache .clear ()
541583 tc .get_taskcluster_client .cache_clear ()
542584
543585 task_definitions = {
@@ -567,12 +609,20 @@ def test_get_ancestors_string(responses, root_url):
567609 },
568610 }
569611
570- # Mock API responses for each task definition
571- for task_id , definition in task_definitions .items ():
572- responses .get (
573- f"{ root_url } /api/queue/v1/task/{ task_id } " ,
574- json = definition ,
575- )
612+ # Mock API response for task definitions
613+ def tasks_callback (request ):
614+ payload = json .loads (request .body )
615+ resp_body = {
616+ "tasks" : [
617+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
618+ for task_id in payload ["taskIds" ]
619+ ]
620+ }
621+ return (200 , [], json .dumps (resp_body ))
622+
623+ responses .add_callback (
624+ responses .POST , f"{ root_url } /api/queue/v1/tasks" , callback = tasks_callback
625+ )
576626
577627 got = tc .get_ancestors ("fff" )
578628 expected = {
0 commit comments