-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtest_selectors.py
More file actions
331 lines (295 loc) · 10.7 KB
/
test_selectors.py
File metadata and controls
331 lines (295 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import typing as t
import pytest
from sqlmesh_dbt import selectors
from sqlmesh.core.selector import DbtSelector
from sqlmesh.core.context import Context
from pathlib import Path
@pytest.mark.parametrize(
"dbt_select,expected",
[
([], None),
(["main.model_a"], "main.model_a"),
(["main.model_a main.model_b"], "main.model_a | main.model_b"),
(["main.model_a", "main.model_b"], "main.model_a | main.model_b"),
(["(main.model_a & ^main.model_b)"], "(main.model_a & ^main.model_b)"),
(
["(+main.model_a & ^main.model_b)", "main.model_c"],
"(+main.model_a & ^main.model_b) | main.model_c",
),
],
)
def test_selection(dbt_select: t.List[str], expected: t.Optional[str]):
assert selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=[]) == expected
@pytest.mark.parametrize(
"dbt_exclude,expected",
[
([], None),
(["main.model_a"], "^(main.model_a)"),
(["(main.model_a & main.model_b)"], "^(main.model_a & main.model_b)"),
(["main.model_a,main.model_b"], "^(main.model_a & main.model_b)"),
(["main.model_a +main.model_b"], "^(main.model_a | +main.model_b)"),
(
["(+main.model_a & ^main.model_b)", "main.model_c"],
"^((+main.model_a & ^main.model_b) | main.model_c)",
),
],
)
def test_exclusion(dbt_exclude: t.List[str], expected: t.Optional[str]):
assert selectors.to_sqlmesh(dbt_select=[], dbt_exclude=dbt_exclude) == expected
@pytest.mark.parametrize(
"dbt_select,dbt_exclude,expected",
[
([], [], None),
(["+main.model_a"], ["raw.src_data"], "+main.model_a & ^(raw.src_data)"),
(
["+main.model_a", "main.*b+"],
["raw.src_data"],
"(+main.model_a | main.*b+) & ^(raw.src_data)",
),
(
["+main.model_a", "main.*b+"],
["raw.src_data", "tag:disabled"],
"(+main.model_a | main.*b+) & ^(raw.src_data | tag:disabled)",
),
],
)
def test_selection_and_exclusion(
dbt_select: t.List[str], dbt_exclude: t.List[str], expected: t.Optional[str]
):
assert selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=dbt_exclude) == expected
@pytest.mark.parametrize(
"expression,expected",
[
("", ([], [])),
("model_a", (["model_a"], [])),
("model_a model_b", (["model_a", "model_b"], [])),
("model_a,model_b", ([], ["model_a", "model_b"])),
("model_a model_b,model_c", (["model_a"], ["model_b", "model_c"])),
("model_a,model_b model_c", (["model_c"], ["model_a", "model_b"])),
],
)
def test_split_unions_and_intersections(
expression: str, expected: t.Tuple[t.List[str], t.List[str]]
):
assert selectors._split_unions_and_intersections(expression) == expected
@pytest.mark.parametrize(
"dbt_select,expected",
[
(["aging"], set()),
(
["staging"],
{
'"jaffle_shop"."main"."stg_customers"',
'"jaffle_shop"."main"."stg_orders"',
'"jaffle_shop"."main"."stg_payments"',
},
),
(["staging.stg_customers"], {'"jaffle_shop"."main"."stg_customers"'}),
(["stg_customers.staging"], set()),
(
["+customers"],
{
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."stg_customers"',
'"jaffle_shop"."main"."stg_orders"',
'"jaffle_shop"."main"."stg_payments"',
'"jaffle_shop"."main"."raw_customers"',
'"jaffle_shop"."main"."raw_orders"',
'"jaffle_shop"."main"."raw_payments"',
},
),
(["customers+"], {'"jaffle_shop"."main"."customers"'}),
(
["customers+", "stg_orders"],
{'"jaffle_shop"."main"."customers"', '"jaffle_shop"."main"."stg_orders"'},
),
(["*.staging.stg_c*"], {'"jaffle_shop"."main"."stg_customers"'}),
(["tag:agg"], {'"jaffle_shop"."main"."agg_orders"'}),
(
["staging.stg_customers", "tag:agg"],
{
'"jaffle_shop"."main"."stg_customers"',
'"jaffle_shop"."main"."agg_orders"',
},
),
(
["+tag:agg"],
{
'"jaffle_shop"."main"."agg_orders"',
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."stg_orders"',
'"jaffle_shop"."main"."stg_payments"',
'"jaffle_shop"."main"."raw_orders"',
'"jaffle_shop"."main"."raw_payments"',
},
),
(
["tag:agg+"],
{
'"jaffle_shop"."main"."agg_orders"',
},
),
(
["tag:b*"],
set(),
),
(
["tag:a*"],
{
'"jaffle_shop"."main"."agg_orders"',
},
),
],
)
def test_select_by_dbt_names(
jaffle_shop_duckdb: Path,
jaffle_shop_duckdb_context: Context,
dbt_select: t.List[str],
expected: t.Set[str],
):
(jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text("""
{{ config(tags=["agg"]) }}
select order_date, count(*) as num_orders from {{ ref('orders') }}
""")
ctx = jaffle_shop_duckdb_context
ctx.load()
assert '"jaffle_shop"."main"."agg_orders"' in ctx.models
assert ctx.get_model('"jaffle_shop"."main"."agg_orders"').tags == ["agg"]
selector = ctx._new_selector()
assert isinstance(selector, DbtSelector)
sqlmesh_selector = selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=[])
assert sqlmesh_selector
assert selector.expand_model_selections([sqlmesh_selector]) == expected
@pytest.mark.parametrize(
"dbt_exclude,expected",
[
(["jaffle_shop"], set()),
(
["staging"],
{
'"jaffle_shop"."main"."agg_orders"',
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."raw_customers"',
'"jaffle_shop"."main"."raw_orders"',
'"jaffle_shop"."main"."raw_payments"',
},
),
(["+customers"], {'"jaffle_shop"."main"."orders"', '"jaffle_shop"."main"."agg_orders"'}),
(
["+tag:agg"],
{
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."stg_customers"',
'"jaffle_shop"."main"."raw_customers"',
},
),
],
)
def test_exclude_by_dbt_names(
jaffle_shop_duckdb: Path,
jaffle_shop_duckdb_context: Context,
dbt_exclude: t.List[str],
expected: t.Set[str],
):
(jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text("""
{{ config(tags=["agg"]) }}
select order_date, count(*) as num_orders from {{ ref('orders') }}
""")
ctx = jaffle_shop_duckdb_context
ctx.load()
assert '"jaffle_shop"."main"."agg_orders"' in ctx.models
assert ctx.get_model('"jaffle_shop"."main"."agg_orders"').tags == ["agg"]
selector = ctx._new_selector()
assert isinstance(selector, DbtSelector)
sqlmesh_selector = selectors.to_sqlmesh(dbt_select=[], dbt_exclude=dbt_exclude)
assert sqlmesh_selector
assert selector.expand_model_selections([sqlmesh_selector]) == expected
@pytest.mark.parametrize(
"dbt_select,dbt_exclude,expected",
[
(["jaffle_shop"], ["jaffle_shop"], set()),
(
["staging"],
["stg_customers"],
{
'"jaffle_shop"."main"."stg_orders"',
'"jaffle_shop"."main"."stg_payments"',
},
),
(
["staging.stg_customers", "tag:agg"],
["tag:agg"],
{
'"jaffle_shop"."main"."stg_customers"',
},
),
],
)
def test_selection_and_exclusion_by_dbt_names(
jaffle_shop_duckdb: Path,
jaffle_shop_duckdb_context: Context,
dbt_select: t.List[str],
dbt_exclude: t.List[str],
expected: t.Set[str],
):
(jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text("""
{{ config(tags=["agg"]) }}
select order_date, count(*) as num_orders from {{ ref('orders') }}
""")
ctx = jaffle_shop_duckdb_context
ctx.load()
assert '"jaffle_shop"."main"."agg_orders"' in ctx.models
selector = ctx._new_selector()
assert isinstance(selector, DbtSelector)
sqlmesh_selector = selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=dbt_exclude)
assert sqlmesh_selector
assert selector.expand_model_selections([sqlmesh_selector]) == expected
@pytest.mark.parametrize(
"input_args,expected",
[
(
dict(select=["jaffle_shop"], models=["jaffle_shop"]),
'"models" and "select" are mutually exclusive',
),
(
dict(models=["jaffle_shop"], resource_type="test"),
'"models" and "resource_type" are mutually exclusive',
),
(
dict(select=["jaffle_shop"], resource_type="test"),
(["resource_type:test,jaffle_shop"], []),
),
(dict(resource_type="model"), (["resource_type:model"], [])),
(dict(models=["stg_customers"]), (["resource_type:model,stg_customers"], [])),
(
dict(models=["stg_customers"], exclude=["orders"]),
(["resource_type:model,stg_customers"], ["orders"]),
),
],
)
def test_consolidate(input_args: t.Dict[str, t.Any], expected: t.Union[t.Tuple[str, str], str]):
all_input_args: t.Dict[str, t.Any] = dict(select=[], exclude=[], models=[], resource_type=None)
all_input_args.update(input_args)
def _do_assert():
assert selectors.consolidate(**all_input_args) == expected
if isinstance(expected, str):
with pytest.raises(ValueError, match=expected):
_do_assert()
else:
_do_assert()
def test_models_by_dbt_names(jaffle_shop_duckdb_context: Context):
ctx = jaffle_shop_duckdb_context
selector = ctx._new_selector()
assert isinstance(selector, DbtSelector)
selector_expr = selectors.to_sqlmesh(
*selectors.consolidate(select=[], exclude=[], models=["jaffle_shop"], resource_type=None)
)
assert selector_expr
assert selector.expand_model_selections([selector_expr]) == {
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."stg_customers"',
'"jaffle_shop"."main"."stg_orders"',
'"jaffle_shop"."main"."stg_payments"',
}