@@ -111,6 +111,8 @@ def test_relationship_test_uses_primary_model_owner_only(
111111 unique_id = str (uuid .uuid4 ()).replace ("-" , "_" )
112112 primary_model_name = f"model_primary_{ unique_id } "
113113 referenced_model_name = f"model_referenced_{ unique_id } "
114+ # Use explicit test name for reliable querying across dbt versions
115+ test_name = f"rel_primary_owner_{ unique_id } "
114116 primary_owner = "Alice"
115117 referenced_owner = "Bob"
116118
@@ -137,10 +139,9 @@ def test_relationship_test_uses_primary_model_owner_only(
137139 "tests" : [
138140 {
139141 "relationships" : {
140- "arguments" : {
141- "to" : f"ref('{ referenced_model_name } ')" ,
142- "field" : "id" ,
143- }
142+ "name" : test_name ,
143+ "to" : f"ref('{ referenced_model_name } ')" ,
144+ "field" : "id" ,
144145 }
145146 }
146147 ],
@@ -175,27 +176,17 @@ def test_relationship_test_uses_primary_model_owner_only(
175176 select = f"{ primary_model_name } { referenced_model_name } "
176177 )
177178
178- # Query by parent_model_unique_id and filter by test_original_name in Python
179- # This is more robust across dbt versions (fusion vs latest_official)
180- all_tests = dbt_project .read_table (
179+ # Query by explicit test name - more robust across dbt versions
180+ tests = dbt_project .read_table (
181181 "dbt_tests" ,
182- where = f"parent_model_unique_id LIKE '%{ primary_model_name } %'" ,
182+ where = f"name LIKE '%{ test_name } %'" ,
183183 raise_if_empty = False ,
184184 )
185185
186- # Filter for relationship tests
187- relationship_tests = [
188- t
189- for t in all_tests
190- if t .get ("test_original_name" ) == "relationships"
191- or "relationships" in (t .get ("short_name" ) or "" ).lower ()
192- or "relationships" in (t .get ("name" ) or "" ).lower ()
193- ]
194-
195186 assert (
196- len (relationship_tests ) == 1
197- ), f"Expected 1 relationship test, got { len (relationship_tests )} . All tests found: { [t .get ('name' ) for t in all_tests ]} "
198- test_row = relationship_tests [0 ]
187+ len (tests ) == 1
188+ ), f"Expected 1 relationship test with name containing ' { test_name } ' , got { len (tests )} . Tests found: { [t .get ('name' ) for t in tests ]} "
189+ test_row = tests [0 ]
199190 model_owners = _parse_model_owners (test_row .get ("model_owners" ))
200191
201192 assert model_owners == [
@@ -212,6 +203,8 @@ def test_relationship_test_no_owner_on_primary_model(dbt_project: DbtProject, tm
212203 unique_id = str (uuid .uuid4 ()).replace ("-" , "_" )
213204 primary_model_name = f"model_no_owner_{ unique_id } "
214205 referenced_model_name = f"model_with_owner_{ unique_id } "
206+ # Use explicit test name for reliable querying across dbt versions
207+ test_name = f"rel_no_owner_{ unique_id } "
215208 referenced_owner = "Bob"
216209
217210 primary_model_sql = """
@@ -236,10 +229,9 @@ def test_relationship_test_no_owner_on_primary_model(dbt_project: DbtProject, tm
236229 "tests" : [
237230 {
238231 "relationships" : {
239- "arguments" : {
240- "to" : f"ref('{ referenced_model_name } ')" ,
241- "field" : "id" ,
242- }
232+ "name" : test_name ,
233+ "to" : f"ref('{ referenced_model_name } ')" ,
234+ "field" : "id" ,
243235 }
244236 }
245237 ],
@@ -274,27 +266,17 @@ def test_relationship_test_no_owner_on_primary_model(dbt_project: DbtProject, tm
274266 select = f"{ primary_model_name } { referenced_model_name } "
275267 )
276268
277- # Query by parent_model_unique_id and filter by test_original_name in Python
278- # This is more robust across dbt versions (fusion vs latest_official)
279- all_tests = dbt_project .read_table (
269+ # Query by explicit test name - more robust across dbt versions
270+ tests = dbt_project .read_table (
280271 "dbt_tests" ,
281- where = f"parent_model_unique_id LIKE '%{ primary_model_name } %'" ,
272+ where = f"name LIKE '%{ test_name } %'" ,
282273 raise_if_empty = False ,
283274 )
284275
285- # Filter for relationship tests
286- relationship_tests = [
287- t
288- for t in all_tests
289- if t .get ("test_original_name" ) == "relationships"
290- or "relationships" in (t .get ("short_name" ) or "" ).lower ()
291- or "relationships" in (t .get ("name" ) or "" ).lower ()
292- ]
293-
294276 assert (
295- len (relationship_tests ) == 1
296- ), f"Expected 1 relationship test, got { len (relationship_tests )} . All tests found: { [t .get ('name' ) for t in all_tests ]} "
297- test_row = relationship_tests [0 ]
277+ len (tests ) == 1
278+ ), f"Expected 1 relationship test with name containing ' { test_name } ' , got { len (tests )} . Tests found: { [t .get ('name' ) for t in tests ]} "
279+ test_row = tests [0 ]
298280 model_owners = _parse_model_owners (test_row .get ("model_owners" ))
299281
300282 assert (
0 commit comments