Skip to content

Commit e8e69c7

Browse files
committed
added tests for commented with statements
1 parent 6fe8cf5 commit e8e69c7

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% macro sqlserver__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
2+
3+
-- Create target schema if it does not
4+
USE [{{ target.database }}];
5+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ target.schema }}')
6+
BEGIN
7+
EXEC('CREATE SCHEMA [{{ target.schema }}]')
8+
END
9+
10+
{% set with_statement_pattern = 'with .+ as\s*\(' %}
11+
{% set re = modules.re %}
12+
{% set is_match = re.search(with_statement_pattern, main_sql, re.IGNORECASE) %}
13+
14+
{% if is_match %}
15+
{% set testview %}
16+
[{{ target.schema }}.testview_{{ range(1300, 19000) | random }}]
17+
{% endset %}
18+
19+
{% set sql = main_sql.replace("'", "''")%}
20+
EXEC('create view {{testview}} as {{ sql }};')
21+
select
22+
{{ "top (" ~ limit ~ ')' if limit != none }}
23+
{{ fail_calc }} as failures,
24+
case when {{ fail_calc }} {{ warn_if }}
25+
then 'true' else 'false' end as should_warn,
26+
case when {{ fail_calc }} {{ error_if }}
27+
then 'true' else 'false' end as should_error
28+
from (
29+
select * from {{testview}}
30+
) dbt_internal_test;
31+
32+
EXEC('drop view {{testview}};')
33+
34+
{% else -%}
35+
select
36+
{{ "top (" ~ limit ~ ')' if limit != none }}
37+
{{ fail_calc }} as failures,
38+
case when {{ fail_calc }} {{ warn_if }}
39+
then 'true' else 'false' end as should_warn,
40+
case when {{ fail_calc }} {{ error_if }}
41+
then 'true' else 'false' end as should_error
42+
from (
43+
{{ main_sql }}
44+
) dbt_internal_test
45+
{%- endif -%}
46+
{%- endmacro %}

tests/functional/adapter/mssql/test_test_with.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
field: ID
3838
"""
3939

40+
comments_model_yml = """
41+
version: 2
42+
models:
43+
- name: sample_model
44+
data_tests:
45+
- with_statement_comments:
46+
field: ID
47+
"""
48+
4049
with_test_fail_sql = """
4150
{% test with_statement_fail(model, field) %}
4251
@@ -63,6 +72,18 @@
6372
{% endtest %}
6473
"""
6574

75+
with_test_with_comments_sql = """
76+
{% test with_statement_comments(model, field) %}
77+
-- comments
78+
with test_sample AS (
79+
SELECT {{ field }} FROM {{ model }}
80+
GROUP BY {{ field }}
81+
HAVING COUNT(*) > 2
82+
)
83+
SELECT * FROM test_sample
84+
{% endtest %}
85+
"""
86+
6687

6788
class BaseSQLTestWith:
6889
@pytest.fixture(scope="class")
@@ -77,6 +98,7 @@ def macros(self):
7798
return {
7899
"with_statement_pass.sql": with_test_pass_sql,
79100
"with_statement_fail.sql": with_test_fail_sql,
101+
"with_statement_comments.sql": with_test_with_comments_sql,
80102
}
81103

82104
@pytest.fixture(scope="class")
@@ -111,3 +133,16 @@ def models(self):
111133
def test_sql_test_contains_with(self, project):
112134
run_dbt(["run"])
113135
run_dbt(["test"], expect_pass=False)
136+
137+
138+
class TestSQLTestWithComment(BaseSQLTestWith):
139+
@pytest.fixture(scope="class")
140+
def models(self):
141+
return {
142+
"sample_model.sql": sample_model,
143+
"schema.yml": comments_model_yml,
144+
}
145+
146+
def test_sql_test_contains_with(self, project):
147+
run_dbt(["run"])
148+
run_dbt(["test"])

0 commit comments

Comments
 (0)