Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

560 test code reporting error when test code uses ctes with line breaks after with keyword #561

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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