1+ from pathlib import Path
2+
3+ import pytest
4+
15from sqlmesh .dbt .test import TestConfig
26
37
@@ -8,3 +12,131 @@ def test_multiline_test_kwarg() -> None:
812 test_kwargs = {"test_field" : "foo\n bar\n " },
913 )
1014 assert test ._kwargs () == 'test_field="foo\n bar"'
15+
16+
17+ @pytest .mark .xdist_group ("dbt_manifest" )
18+ def test_tests_get_unique_names (tmp_path : Path , create_empty_project ) -> None :
19+ from sqlmesh .utils .yaml import YAML
20+ from sqlmesh .core .context import Context
21+
22+ yaml = YAML ()
23+ project_dir , model_dir = create_empty_project (project_name = "local" )
24+
25+ model_file = model_dir / "my_model.sql"
26+ with open (model_file , "w" , encoding = "utf-8" ) as f :
27+ f .write ("SELECT 1 as id, 'value1' as status" )
28+
29+ # Create schema.yml with:
30+ # 1. Same test on model and source, both with/without custom test name
31+ # 2. Same test on same model with different args, both with/without custom test name
32+ # 3. Versioned model with tests (both built-in and custom named)
33+ schema_yaml = {
34+ "version" : 2 ,
35+ "sources" : [
36+ {
37+ "name" : "raw" ,
38+ "tables" : [
39+ {
40+ "name" : "my_source" ,
41+ "columns" : [
42+ {
43+ "name" : "id" ,
44+ "data_tests" : [
45+ {"not_null" : {"name" : "custom_notnull_name" }},
46+ {"not_null" : {}},
47+ ],
48+ }
49+ ],
50+ }
51+ ],
52+ }
53+ ],
54+ "models" : [
55+ {
56+ "name" : "my_model" ,
57+ "columns" : [
58+ {
59+ "name" : "id" ,
60+ "data_tests" : [
61+ {"not_null" : {"name" : "custom_notnull_name" }},
62+ {"not_null" : {}},
63+ ],
64+ },
65+ {
66+ "name" : "status" ,
67+ "data_tests" : [
68+ {"accepted_values" : {"values" : ["value1" , "value2" ]}},
69+ {"accepted_values" : {"values" : ["value1" , "value2" , "value3" ]}},
70+ {
71+ "accepted_values" : {
72+ "name" : "custom_accepted_values_name" ,
73+ "values" : ["value1" , "value2" ],
74+ }
75+ },
76+ {
77+ "accepted_values" : {
78+ "name" : "custom_accepted_values_name" ,
79+ "values" : ["value1" , "value2" , "value3" ],
80+ }
81+ },
82+ ],
83+ },
84+ ],
85+ },
86+ {
87+ "name" : "versioned_model" ,
88+ "columns" : [
89+ {
90+ "name" : "id" ,
91+ "data_tests" : [
92+ {"not_null" : {}},
93+ {"not_null" : {"name" : "custom_versioned_notnull" }},
94+ ],
95+ },
96+ {
97+ "name" : "amount" ,
98+ "data_tests" : [
99+ {"accepted_values" : {"values" : ["low" , "high" ]}},
100+ ],
101+ },
102+ ],
103+ "versions" : [
104+ {"v" : 1 },
105+ {"v" : 2 },
106+ ],
107+ },
108+ ],
109+ }
110+
111+ schema_file = model_dir / "schema.yml"
112+ with open (schema_file , "w" , encoding = "utf-8" ) as f :
113+ yaml .dump (schema_yaml , f )
114+
115+ # Create versioned model files
116+ versioned_model_v1_file = model_dir / "versioned_model_v1.sql"
117+ with open (versioned_model_v1_file , "w" , encoding = "utf-8" ) as f :
118+ f .write ("SELECT 1 as id, 'low' as amount" )
119+
120+ versioned_model_v2_file = model_dir / "versioned_model_v2.sql"
121+ with open (versioned_model_v2_file , "w" , encoding = "utf-8" ) as f :
122+ f .write ("SELECT 1 as id, 'low' as amount" )
123+
124+ context = Context (paths = project_dir )
125+
126+ all_audit_names = list (context ._audits .keys ()) + list (context ._standalone_audits .keys ())
127+ assert sorted (all_audit_names ) == [
128+ "local.accepted_values_my_model_status__value1__value2" ,
129+ "local.accepted_values_my_model_status__value1__value2__value3" ,
130+ "local.accepted_values_versioned_model_v1_amount__low__high" ,
131+ "local.accepted_values_versioned_model_v2_amount__low__high" ,
132+ "local.custom_accepted_values_name_my_model_status__value1__value2" ,
133+ "local.custom_accepted_values_name_my_model_status__value1__value2__value3" ,
134+ "local.custom_notnull_name_my_model_id" ,
135+ "local.custom_versioned_notnull_versioned_model_v1_id" ,
136+ "local.custom_versioned_notnull_versioned_model_v2_id" ,
137+ "local.not_null_my_model_id" ,
138+ "local.not_null_versioned_model_v1_id" ,
139+ "local.not_null_versioned_model_v2_id" ,
140+ "local.source_custom_notnull_name_raw_my_source_id" ,
141+ "local.source_not_null_raw_my_source_id" ,
142+ ]
0 commit comments