Skip to content

Commit 6fe8cf5

Browse files
committed
added tests for with statements in custom tests
1 parent 09b577b commit 6fe8cf5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import pytest
2+
from dbt.tests.util import run_dbt
3+
4+
sample_model = """
5+
SELECT
6+
1 as ID,
7+
'a' as data
8+
9+
UNION ALL
10+
11+
SELECT
12+
2 as ID,
13+
'b' as data
14+
15+
UNION ALL
16+
17+
SELECT
18+
2 as ID,
19+
'c' as data
20+
"""
21+
22+
pass_model_yml = """
23+
version: 2
24+
models:
25+
- name: sample_model
26+
data_tests:
27+
- with_statement_pass:
28+
field: ID
29+
"""
30+
31+
fail_model_yml = """
32+
version: 2
33+
models:
34+
- name: sample_model
35+
data_tests:
36+
- with_statement_fail:
37+
field: ID
38+
"""
39+
40+
with_test_fail_sql = """
41+
{% test with_statement_fail(model, field) %}
42+
43+
with test_sample AS (
44+
SELECT {{ field }} FROM {{ model }}
45+
GROUP BY {{ field }}
46+
HAVING COUNT(*) > 1
47+
)
48+
SELECT * FROM test_sample
49+
50+
{% endtest %}
51+
"""
52+
53+
with_test_pass_sql = """
54+
{% test with_statement_pass(model, field) %}
55+
56+
with test_sample AS (
57+
SELECT {{ field }} FROM {{ model }}
58+
GROUP BY {{ field }}
59+
HAVING COUNT(*) > 2
60+
)
61+
SELECT * FROM test_sample
62+
63+
{% endtest %}
64+
"""
65+
66+
67+
class BaseSQLTestWith:
68+
@pytest.fixture(scope="class")
69+
def project_config_update(self):
70+
return {
71+
"config-version": 2,
72+
"macro-paths": ["macros"],
73+
}
74+
75+
@pytest.fixture(scope="class")
76+
def macros(self):
77+
return {
78+
"with_statement_pass.sql": with_test_pass_sql,
79+
"with_statement_fail.sql": with_test_fail_sql,
80+
}
81+
82+
@pytest.fixture(scope="class")
83+
def models(self):
84+
return {
85+
"sample_model.sql": sample_model,
86+
"schema.yml": pass_model_yml,
87+
}
88+
89+
90+
class TestSQLTestWithPass(BaseSQLTestWith):
91+
@pytest.fixture(scope="class")
92+
def models(self):
93+
return {
94+
"sample_model.sql": sample_model,
95+
"schema.yml": pass_model_yml,
96+
}
97+
98+
def test_sql_test_contains_with(self, project):
99+
run_dbt(["run"])
100+
run_dbt(["test"])
101+
102+
103+
class TestSQLTestWithFail(BaseSQLTestWith):
104+
@pytest.fixture(scope="class")
105+
def models(self):
106+
return {
107+
"sample_model.sql": sample_model,
108+
"schema.yml": fail_model_yml,
109+
}
110+
111+
def test_sql_test_contains_with(self, project):
112+
run_dbt(["run"])
113+
run_dbt(["test"], expect_pass=False)

0 commit comments

Comments
 (0)