11from __future__ import annotations
22
33from typing import TYPE_CHECKING
4+ from unittest .mock import call , patch
45
56import pytest
67
78from infrahub .core .initialization import create_branch
8- from infrahub .message_bus import messages
9+ from infrahub .graphql .models import RequestGraphQLQueryGroupUpdate
10+ from infrahub .workflows .catalogue import REQUEST_GRAPHQL_QUERY_GROUP_UPDATE
911
1012if TYPE_CHECKING :
1113 from fastapi .testclient import TestClient
@@ -32,70 +34,90 @@ async def test_query_endpoint_group_no_params(
3234 patch_services ,
3335):
3436 # Must execute in a with block to execute the startup/shutdown events
35- with client :
37+ with (
38+ client ,
39+ patch (
40+ "infrahub.services.adapters.workflow.local.WorkflowLocalExecution.submit_workflow"
41+ ) as mock_submit_workflow ,
42+ ):
3643 response = client .get (
3744 "/api/query/query01?update_group=true&subscribers=AAAAAA&subscribers=BBBBBB" , headers = admin_headers
3845 )
3946
40- assert "errors" not in response .json ()
41- assert response .status_code == 200
42- assert response .json ()["data" ] is not None
43- result = response .json ()["data" ]
47+ assert "errors" not in response .json ()
48+ assert response .status_code == 200
49+ assert response .json ()["data" ] is not None
50+ result = response .json ()["data" ]
4451
45- result_per_name = {result ["node" ]["name" ]["value" ]: result for result in result ["TestPerson" ]["edges" ]}
46- assert sorted (result_per_name .keys ()) == ["Jane" , "John" ]
47- assert len (result_per_name ["John" ]["node" ]["cars" ]["edges" ]) == 2
48- assert len (result_per_name ["Jane" ]["node" ]["cars" ]["edges" ]) == 1
52+ result_per_name = {result ["node" ]["name" ]["value" ]: result for result in result ["TestPerson" ]["edges" ]}
53+ assert sorted (result_per_name .keys ()) == ["Jane" , "John" ]
54+ assert len (result_per_name ["John" ]["node" ]["cars" ]["edges" ]) == 2
55+ assert len (result_per_name ["Jane" ]["node" ]["cars" ]["edges" ]) == 1
4956
50- q1 = car_person_data ["q1" ]
51- p1 = car_person_data ["p1" ]
52- p2 = car_person_data ["p2" ]
53- c1 = car_person_data ["c1" ]
54- c2 = car_person_data ["c2" ]
55- c3 = car_person_data ["c3" ]
57+ q1 = car_person_data ["q1" ]
58+ p1 = car_person_data ["p1" ]
59+ p2 = car_person_data ["p2" ]
60+ c1 = car_person_data ["c1" ]
61+ c2 = car_person_data ["c2" ]
62+ c3 = car_person_data ["c3" ]
5663
57- assert (
58- messages .RequestGraphQLQueryGroupUpdate (
64+ model = RequestGraphQLQueryGroupUpdate (
5965 query_id = q1 .id ,
6066 query_name = "query01" ,
6167 branch = "main" ,
6268 related_node_ids = sorted ([p1 .id , p2 .id , c1 .id , c2 .id , c3 .id ]),
6369 subscribers = sorted (["AAAAAA" , "BBBBBB" ]),
6470 params = {},
6571 )
66- in client .app .state .service .message_bus .messages
67- )
72+
73+ expected_calls = [
74+ call (
75+ workflow = REQUEST_GRAPHQL_QUERY_GROUP_UPDATE ,
76+ parameters = {"model" : model },
77+ ),
78+ ]
79+ mock_submit_workflow .assert_has_calls (expected_calls )
6880
6981
7082async def test_query_endpoint_group_params (
7183 db : InfrahubDatabase , client : TestClient , admin_headers , default_branch , create_test_admin , car_person_data
7284):
7385 # Must execute in a with block to execute the startup/shutdown events
74- with client :
86+ with (
87+ client ,
88+ patch (
89+ "infrahub.services.adapters.workflow.local.WorkflowLocalExecution.submit_workflow"
90+ ) as mock_submit_workflow ,
91+ ):
7592 response = client .get ("/api/query/query02?update_group=true&person=John" , headers = admin_headers )
7693
77- assert "errors" not in response .json ()
78- assert response .status_code == 200
79- assert response .json ()["data" ] is not None
80- result = response .json ()["data" ]
94+ assert "errors" not in response .json ()
95+ assert response .status_code == 200
96+ assert response .json ()["data" ] is not None
97+ result = response .json ()["data" ]
8198
82- result_per_name = {result ["node" ]["name" ]["value" ]: result for result in result ["TestPerson" ]["edges" ]}
83- assert sorted (result_per_name .keys ()) == ["John" ]
99+ result_per_name = {result ["node" ]["name" ]["value" ]: result for result in result ["TestPerson" ]["edges" ]}
100+ assert sorted (result_per_name .keys ()) == ["John" ]
84101
85- q2 = car_person_data ["q2" ]
86- p1 = car_person_data ["p1" ]
102+ q2 = car_person_data ["q2" ]
103+ p1 = car_person_data ["p1" ]
87104
88- assert (
89- messages .RequestGraphQLQueryGroupUpdate (
105+ model = RequestGraphQLQueryGroupUpdate (
90106 query_id = q2 .id ,
91107 query_name = "query02" ,
92108 branch = "main" ,
93109 related_node_ids = {p1 .id },
94110 subscribers = [],
95111 params = {"person" : "John" },
96112 )
97- in client .app .state .service .message_bus .messages
98- )
113+
114+ expected_calls = [
115+ call (
116+ workflow = REQUEST_GRAPHQL_QUERY_GROUP_UPDATE ,
117+ parameters = {"model" : model },
118+ ),
119+ ]
120+ mock_submit_workflow .assert_has_calls (expected_calls )
99121
100122
101123async def test_query_endpoint_get_default_branch (
0 commit comments