1414from sqlmesh .utils .yaml import load as yaml_load
1515
1616if t .TYPE_CHECKING :
17- from sqlmesh .core .config . loader import C
17+ from sqlmesh .core .loader import Loader
1818
1919
2020class ModelTestMetadata (PydanticModel ):
@@ -31,7 +31,8 @@ def __hash__(self) -> int:
3131
3232
3333def load_model_test_file (
34- path : pathlib .Path , variables : dict [str , t .Any ] | None = None
34+ path : pathlib .Path ,
35+ get_variables : t .Callable [[t .Optional [str ]], t .Dict [str , str ]],
3536) -> dict [str , ModelTestMetadata ]:
3637 """Load a single model test file.
3738
@@ -42,7 +43,7 @@ def load_model_test_file(
4243 A list of ModelTestMetadata named tuples.
4344 """
4445 model_test_metadata = {}
45- contents = yaml_load (path , variables = variables )
46+ contents = yaml_load (path , get_variables = get_variables )
4647
4748 for test_name , value in contents .items ():
4849 model_test_metadata [test_name ] = ModelTestMetadata (
@@ -53,8 +54,8 @@ def load_model_test_file(
5354
5455def discover_model_tests (
5556 path : pathlib .Path ,
57+ get_variables : t .Callable [[t .Optional [str ]], t .Dict [str , str ]],
5658 ignore_patterns : list [str ] | None = None ,
57- variables : dict [str , t .Any ] | None = None ,
5859) -> Iterator [ModelTestMetadata ]:
5960 """Discover model tests.
6061
@@ -78,7 +79,7 @@ def discover_model_tests(
7879 break
7980 else :
8081 for model_test_metadata in load_model_test_file (
81- yaml_file , variables = variables
82+ yaml_file , get_variables = get_variables
8283 ).values ():
8384 yield model_test_metadata
8485
@@ -103,34 +104,16 @@ def filter_tests_by_patterns(
103104 )
104105
105106
106- def get_all_model_tests (
107- * paths : pathlib .Path ,
108- patterns : list [str ] | None = None ,
109- ignore_patterns : list [str ] | None = None ,
110- variables : dict [str , t .Any ] | None = None ,
111- ) -> list [ModelTestMetadata ]:
112- model_test_metadatas = [
113- meta
114- for path in paths
115- for meta in discover_model_tests (pathlib .Path (path ), ignore_patterns , variables = variables )
116- ]
117- if patterns :
118- model_test_metadatas = filter_tests_by_patterns (model_test_metadatas , patterns )
119- return model_test_metadatas
120-
121-
122107def load_model_tests (
123- configs : dict [ pathlib . Path , C ],
108+ loaders : list [ Loader ],
124109 tests : t .Optional [t .List [str ]] = None ,
125110 patterns : list [str ] | None = None ,
126- variables : dict [str , t .Any ] | None = None ,
127111) -> list [ModelTestMetadata ]:
128112 """Load model tests into a list of ModelTestMetadata which will be propagated to the test runner.
129113
130114 Args:
131115 tests: A list of tests to load; If not specified, all tests are loaded
132- patterns: A list of patterns to match against.
133- variables: A dictionary of variables to use when loading the tests.
116+ patterns: A list of patterns that'll be used to filter tests by file name.
134117 configs: A dictionary of configs to use when loading all the tests.
135118 """
136119 test_meta = []
@@ -139,24 +122,27 @@ def load_model_tests(
139122 for test in tests :
140123 filename , test_name = test .split ("::" , maxsplit = 1 ) if "::" in test else (test , "" )
141124
142- test_file = load_model_test_file (pathlib .Path (filename ), variables = variables )
125+ test_file = load_model_test_file (
126+ pathlib .Path (filename ), get_variables = loaders [0 ]._get_variables
127+ )
143128 if test_name :
144129 test_meta .append (test_file [test_name ])
145130 else :
146131 test_meta .extend (test_file .values ())
147-
148- if patterns :
149- test_meta = filter_tests_by_patterns (test_meta , patterns )
150-
151132 else :
152- for path , config in configs . items () :
133+ for loader in loaders :
153134 test_meta .extend (
154- get_all_model_tests (
155- path / c .TESTS ,
156- patterns = patterns ,
157- ignore_patterns = config .ignore_patterns ,
158- variables = variables ,
159- )
135+ [
136+ meta
137+ for meta in discover_model_tests (
138+ pathlib .Path (loader .config_path / c .TESTS ),
139+ ignore_patterns = loader .config .ignore_patterns , # type: ignore
140+ get_variables = loader ._get_variables ,
141+ )
142+ ]
160143 )
161144
145+ if patterns :
146+ test_meta = filter_tests_by_patterns (test_meta , patterns )
147+
162148 return test_meta
0 commit comments