88from infrahub .core .timestamp import Timestamp
99from infrahub .database import InfrahubDatabase
1010from infrahub .graphql .initialization import prepare_graphql_params
11+ from infrahub .services import InfrahubServices , services
12+ from infrahub .services .adapters .workflow .local import WorkflowLocalExecution
13+ from tests .adapters .cache import MemoryCache
14+ from tests .adapters .message_bus import BusRecorder
1115from tests .helpers .graphql import graphql
1216
1317DIFF_UPDATE_MUTATION = """
14- mutation($branch: String!, $name: String, $from_time: DateTime, $to_time: DateTime) {
15- DiffUpdate(data: {branch: $branch, name: $name, from_time: $from_time, to_time: $to_time, wait_for_completion: true}) {
18+ mutation($branch: String!, $name: String, $from_time: DateTime, $to_time: DateTime, $wait_until_completion: Boolean = true) {
19+ DiffUpdate(
20+ data: {
21+ branch: $branch,
22+ name: $name,
23+ from_time: $from_time,
24+ to_time: $to_time
25+ },
26+ wait_until_completion: $wait_until_completion
27+ ) {
1628 ok
29+ task {
30+ id
31+ }
1732 }
1833 }
1934"""
2237class TestDiffUpdateMutation :
2338 diff_name = "CountDiffula"
2439
40+ @pytest .fixture
41+ def service_testing (self , db : InfrahubDatabase ):
42+ original = services .service
43+ service = InfrahubServices (
44+ database = db , message_bus = BusRecorder (), workflow = WorkflowLocalExecution (), cache = MemoryCache ()
45+ )
46+ services .service = service
47+ services .prepare (service = services .service )
48+ yield service
49+ services .service = original
50+ services .prepare (service = services .service )
51+
2552 @pytest .fixture
2653 async def diff_branch (self , db : InfrahubDatabase ) -> Branch :
2754 return await create_branch (db = db , branch_name = "branch" )
2855
2956 @pytest .fixture
3057 async def named_diff (
31- self , db : InfrahubDatabase , default_branch : Branch , criticality_schema , diff_branch : Branch
58+ self ,
59+ db : InfrahubDatabase ,
60+ default_branch : Branch ,
61+ prefect_test_fixture ,
62+ service_testing : InfrahubServices ,
63+ criticality_schema ,
64+ diff_branch : Branch ,
3265 ) -> EnrichedDiffRootMetadata :
3366 params = await prepare_graphql_params (
34- db = db , include_mutation = True , include_subscription = False , branch = default_branch
67+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
3568 )
3669 result = await graphql (
3770 schema = params .schema ,
@@ -53,11 +86,17 @@ async def named_diff(
5386 )[0 ]
5487
5588 async def test_create_diff_before_branched_from_fails (
56- self , db : InfrahubDatabase , default_branch : Branch , criticality_schema , diff_branch : Branch
89+ self ,
90+ db : InfrahubDatabase ,
91+ default_branch : Branch ,
92+ prefect_test_fixture ,
93+ service_testing : InfrahubServices ,
94+ criticality_schema ,
95+ diff_branch : Branch ,
5796 ):
5897 branched_from_timestamp = Timestamp (diff_branch .get_branched_from ())
5998 params = await prepare_graphql_params (
60- db = db , include_mutation = True , include_subscription = False , branch = default_branch
99+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
61100 )
62101 result = await graphql (
63102 schema = params .schema ,
@@ -74,11 +113,17 @@ async def test_create_diff_before_branched_from_fails(
74113 assert result .data ["DiffUpdate" ]["ok" ] is True
75114
76115 async def test_create_time_range_diff_without_name_fails (
77- self , db : InfrahubDatabase , default_branch : Branch , criticality_schema , diff_branch : Branch
116+ self ,
117+ db : InfrahubDatabase ,
118+ default_branch : Branch ,
119+ prefect_test_fixture ,
120+ service_testing : InfrahubServices ,
121+ criticality_schema ,
122+ diff_branch : Branch ,
78123 ):
79124 branched_from_timestamp = Timestamp (diff_branch .get_branched_from ())
80125 params = await prepare_graphql_params (
81- db = db , include_mutation = True , include_subscription = False , branch = default_branch
126+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
82127 )
83128 result = await graphql (
84129 schema = params .schema ,
@@ -99,12 +144,14 @@ async def test_create_diff_with_illegal_times_fails(
99144 self ,
100145 db : InfrahubDatabase ,
101146 default_branch : Branch ,
147+ prefect_test_fixture ,
148+ service_testing : InfrahubServices ,
102149 criticality_schema ,
103150 diff_branch : Branch ,
104151 named_diff : EnrichedDiffRootMetadata ,
105152 ):
106153 params = await prepare_graphql_params (
107- db = db , include_mutation = True , include_subscription = False , branch = default_branch
154+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
108155 )
109156 result = await graphql (
110157 schema = params .schema ,
@@ -140,13 +187,44 @@ async def test_create_named_diff_with_legal_times_succeeds(
140187 self ,
141188 db : InfrahubDatabase ,
142189 default_branch : Branch ,
190+ prefect_test_fixture ,
191+ service_testing : InfrahubServices ,
192+ criticality_schema ,
193+ diff_branch : Branch ,
194+ named_diff : EnrichedDiffRootMetadata ,
195+ ):
196+ branched_from_timestamp = Timestamp (diff_branch .get_branched_from ())
197+ params = await prepare_graphql_params (
198+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
199+ )
200+ result = await graphql (
201+ schema = params .schema ,
202+ source = DIFF_UPDATE_MUTATION ,
203+ context_value = params .context ,
204+ root_value = None ,
205+ variable_values = {
206+ "branch" : diff_branch .name ,
207+ "from_time" : branched_from_timestamp .to_string (),
208+ "to_time" : Timestamp ().to_string (),
209+ "name" : self .diff_name ,
210+ },
211+ )
212+ assert result .errors is None
213+ assert result .data ["DiffUpdate" ]["ok" ] is True
214+
215+ async def test_retrieve_task_id (
216+ self ,
217+ db : InfrahubDatabase ,
218+ default_branch : Branch ,
219+ prefect_test_fixture ,
220+ service_testing : InfrahubServices ,
143221 criticality_schema ,
144222 diff_branch : Branch ,
145223 named_diff : EnrichedDiffRootMetadata ,
146224 ):
147225 branched_from_timestamp = Timestamp (diff_branch .get_branched_from ())
148226 params = await prepare_graphql_params (
149- db = db , include_mutation = True , include_subscription = False , branch = default_branch
227+ db = db , include_mutation = True , include_subscription = False , branch = default_branch , service = service_testing
150228 )
151229 result = await graphql (
152230 schema = params .schema ,
@@ -158,7 +236,9 @@ async def test_create_named_diff_with_legal_times_succeeds(
158236 "from_time" : branched_from_timestamp .to_string (),
159237 "to_time" : Timestamp ().to_string (),
160238 "name" : self .diff_name ,
239+ "wait_until_completion" : False ,
161240 },
162241 )
163242 assert result .errors is None
164243 assert result .data ["DiffUpdate" ]["ok" ] is True
244+ assert result .data ["DiffUpdate" ]["task" ]["id" ] is not None
0 commit comments