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,37 @@ 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 = ("abc123" , "def456" ,)
325+ tc .get_taskcluster_client .cache_clear ()
326+ tc ._task_definitions_cache .cache .clear ()
327+
328+ task_definitions = {
329+ "abc123" : {"payload" : "blah" },
330+ "def456" : {"payload" : "foobar" },
331+ }
332+
333+ def tasks_callback (request ):
334+ payload = json .loads (request .body )
335+ resp_body = {
336+ "tasks" : [
337+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
338+ for task_id in payload ["taskIds" ]
339+ ]
340+ }
341+ return (200 , [], json .dumps (resp_body ))
342+
343+ responses .add_callback (
344+ responses .POST ,
345+ f"{ root_url } /api/queue/v1/tasks" ,
346+ callback = tasks_callback ,
347+ )
348+ result = tc .get_task_definitions (tid )
349+ assert result == task_definitions
350+ result = tc .get_task_definition (tid [0 ])
351+ assert result == {"payload" : "blah" }
352+
353+
321354def test_cancel_task (responses , root_url ):
322355 tid = "abc123"
323356 tc .get_taskcluster_client .cache_clear ()
@@ -487,8 +520,7 @@ def test_list_task_group_incomplete_tasks(responses, root_url):
487520
488521
489522def test_get_ancestors (responses , root_url ):
490- tc .get_task_definition .cache_clear ()
491- tc ._get_deps .cache_clear ()
523+ tc ._task_definitions_cache .cache .clear ()
492524 tc .get_taskcluster_client .cache_clear ()
493525
494526 task_definitions = {
@@ -518,12 +550,20 @@ def test_get_ancestors(responses, root_url):
518550 },
519551 }
520552
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- )
553+ # Mock API response for task definitions
554+ def tasks_callback (request ):
555+ payload = json .loads (request .body )
556+ resp_body = {
557+ "tasks" : [
558+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
559+ for task_id in payload ["taskIds" ]
560+ ]
561+ }
562+ return (200 , [], json .dumps (resp_body ))
563+
564+ responses .add_callback (
565+ responses .POST , f"{ root_url } /api/queue/v1/tasks" , callback = tasks_callback
566+ )
527567
528568 got = tc .get_ancestors (["bbb" , "fff" ])
529569 expected = {
@@ -536,8 +576,7 @@ def test_get_ancestors(responses, root_url):
536576
537577
538578def test_get_ancestors_string (responses , root_url ):
539- tc .get_task_definition .cache_clear ()
540- tc ._get_deps .cache_clear ()
579+ tc ._task_definitions_cache .cache .clear ()
541580 tc .get_taskcluster_client .cache_clear ()
542581
543582 task_definitions = {
@@ -567,12 +606,20 @@ def test_get_ancestors_string(responses, root_url):
567606 },
568607 }
569608
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- )
609+ # Mock API response for task definitions
610+ def tasks_callback (request ):
611+ payload = json .loads (request .body )
612+ resp_body = {
613+ "tasks" : [
614+ {"taskId" : task_id , "task" : task_definitions [task_id ]}
615+ for task_id in payload ["taskIds" ]
616+ ]
617+ }
618+ return (200 , [], json .dumps (resp_body ))
619+
620+ responses .add_callback (
621+ responses .POST , f"{ root_url } /api/queue/v1/tasks" , callback = tasks_callback
622+ )
576623
577624 got = tc .get_ancestors ("fff" )
578625 expected = {
0 commit comments