Skip to content

Commit

Permalink
Merge pull request #561 from dbt-msft/560-test-code-reporting-error-w…
Browse files Browse the repository at this point in the history
…hen-test-code-uses-ctes-with-line-breaks-after-with-keyword

560 test code reporting error when test code uses ctes with line breaks after with keyword
  • Loading branch information
cody-scott authored Sep 18, 2024
2 parents be032aa + b8c462b commit ec30afe
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 45 deletions.
9 changes: 0 additions & 9 deletions .devcontainer/devcontainer-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:1-3.10-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
"ghcr.io/devcontainers/features/docker-in-docker:2.12.0": {}
},
"forwardPorts": [1433],
"postStartCommand": "/bin/bash ./.devcontainer/setup_odbc.sh & /bin/bash ./.devcontainer/setup_env.sh",
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ env.bak/
venv.bak/
.mise.toml

**devcontainer-lock.json**
devcontainer-lock.json
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### v1.8.4

- Minor fix to tests with CTEs which do not start on first line. https://github.com/dbt-msft/dbt-sqlserver/issues/560

### v1.8.3

- Minor fix in no lock behaviour https://github.com/dbt-msft/dbt-sqlserver/pull/557
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/sqlserver/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.8.3"
version = "1.8.4"
49 changes: 16 additions & 33 deletions dbt/include/sqlserver/macros/materializations/tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,23 @@
EXEC('CREATE SCHEMA [{{ target.schema }}]')
END

{% set with_statement_pattern = 'with .+ as\s*\(' %}
{% set re = modules.re %}
{% set is_match = re.search(with_statement_pattern, main_sql, re.IGNORECASE) %}
{% set testview %}
[{{ target.schema }}.testview_{{ range(1300, 19000) | random }}]
{% endset %}

{% if is_match %}
{% set testview %}
[{{ target.schema }}.testview_{{ range(1300, 19000) | random }}]
{% endset %}
{% set sql = main_sql.replace("'", "''")%}
EXEC('create view {{testview}} as {{ sql }};')
select
{{ "top (" ~ limit ~ ')' if limit != none }}
{{ fail_calc }} as failures,
case when {{ fail_calc }} {{ warn_if }}
then 'true' else 'false' end as should_warn,
case when {{ fail_calc }} {{ error_if }}
then 'true' else 'false' end as should_error
from (
select * from {{testview}}
) dbt_internal_test;

{% set sql = main_sql.replace("'", "''")%}
EXEC('create view {{testview}} as {{ sql }};')
select
{{ "top (" ~ limit ~ ')' if limit != none }}
{{ fail_calc }} as failures,
case when {{ fail_calc }} {{ warn_if }}
then 'true' else 'false' end as should_warn,
case when {{ fail_calc }} {{ error_if }}
then 'true' else 'false' end as should_error
from (
select * from {{testview}}
) dbt_internal_test;
EXEC('drop view {{testview}};')

EXEC('drop view {{testview}};')

{% else -%}
select
{{ "top (" ~ limit ~ ')' if limit != none }}
{{ fail_calc }} as failures,
case when {{ fail_calc }} {{ warn_if }}
then 'true' else 'false' end as should_warn,
case when {{ fail_calc }} {{ error_if }}
then 'true' else 'false' end as should_error
from (
{{ main_sql }}
) dbt_internal_test
{%- endif -%}
{%- endmacro %}
15 changes: 15 additions & 0 deletions tests/functional/adapter/mssql/test_test_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@
{% endtest %}
"""

with_test_multiline = """
{% test with_test_multiline(model, field) %}
-- comments
with
test_sample AS (
SELECT {{ field }} FROM {{ model }}
GROUP BY {{ field }}
HAVING COUNT(*) > 2
)
SELECT * FROM test_sample
{% endtest %}
"""


class BaseSQLTestWith:
@pytest.fixture(scope="class")
Expand All @@ -99,6 +113,7 @@ def macros(self):
"with_statement_pass.sql": with_test_pass_sql,
"with_statement_fail.sql": with_test_fail_sql,
"with_statement_comments.sql": with_test_with_comments_sql,
"with_test_multiline.sql": with_test_multiline,
}

@pytest.fixture(scope="class")
Expand Down

0 comments on commit ec30afe

Please sign in to comment.