Skip to content

Commit

Permalink
adding drop relation to create_table_as to support test_store_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed May 27, 2024
1 parent 7bde13e commit 180ecc0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
{%- else %}
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
{% endif %}
{% do adapter.drop_relation(tmp_relation)%}
{% endmacro %}
10 changes: 8 additions & 2 deletions dbt/include/fabric/macros/materializations/tests/helpers.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{% macro fabric__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}

-- Create target schema if it does not
USE [{{ target.database }}];
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ target.schema }}')
BEGIN
EXEC('CREATE SCHEMA [{{ target.schema }}]')
END

{% if main_sql.strip().lower().startswith('with') %}
{% set testview %}
{{ config.get('schema') }}.testview_{{ range(1300, 19000) | random }}
{{ 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 }}
Expand Down
56 changes: 18 additions & 38 deletions tests/functional/adapter/test_store_test_failures.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import pytest
from dbt.tests.adapter.store_test_failures_tests import basic
from dbt.tests.adapter.store_test_failures_tests.fixtures import (
models__file_model_but_with_a_no_good_very_long_name,
models__fine_model,
models__problematic_model,
properties__schema_yml,
seeds__expected_accepted_values,
seeds__expected_failing_test,
seeds__expected_not_null_problematic_model_id,
seeds__expected_unique_problematic_model_id,
seeds__people,
tests__failing_test,
)
from dbt.tests.adapter.store_test_failures_tests import basic, fixtures
from dbt.tests.util import check_relations_equal, run_dbt

# from dbt.tests.adapter.store_test_failures_tests.test_store_test_failures import (
# TestStoreTestFailures,
# )
# used to rename test audit schema to help test schema meet max char limit
# the default is _dbt_test__audit but this runs over the postgres 63 schema name char limit
# without which idempotency conditions will not hold (i.e. dbt can't drop the schema properly)
TEST_AUDIT_SCHEMA_SUFFIX = "dbt_test__aud"

tests__passing_test = """
select * from {{ ref('fine_model') }}
where 1=2
"""

# used to rename test audit schema to help test schema meet max char limit
# the default is _dbt_test__audit but this runs over the postgres 63 schema name char limit
# without which idempotency conditions will not hold (i.e. dbt can't drop the schema properly)
TEST_AUDIT_SCHEMA_SUFFIX = "dbt_test__aud"


class StoreTestFailuresBase:
@pytest.fixture(scope="function", autouse=True)
Expand All @@ -39,30 +23,30 @@ def setUp(self, project):
@pytest.fixture(scope="class")
def seeds(self):
return {
"people.csv": seeds__people,
"expected_accepted_values.csv": seeds__expected_accepted_values,
"expected_failing_test.csv": seeds__expected_failing_test,
"expected_not_null_problematic_model_id.csv": seeds__expected_not_null_problematic_model_id,
"expected_unique_problematic_model_id.csv": seeds__expected_unique_problematic_model_id,
"people.csv": fixtures.seeds__people,
"expected_accepted_values.csv": fixtures.seeds__expected_accepted_values,
"expected_failing_test.csv": fixtures.seeds__expected_failing_test,
"expected_not_null_problematic_model_id.csv": fixtures.seeds__expected_not_null_problematic_model_id,
"expected_unique_problematic_model_id.csv": fixtures.seeds__expected_unique_problematic_model_id,
}

@pytest.fixture(scope="class")
def tests(self):
return {
"failing_test.sql": tests__failing_test,
"failing_test.sql": fixtures.tests__failing_test,
"passing_test.sql": tests__passing_test,
}

@pytest.fixture(scope="class")
def properties(self):
return {"schema.yml": properties__schema_yml}
return {"schema.yml": fixtures.properties__schema_yml}

@pytest.fixture(scope="class")
def models(self):
return {
"fine_model.sql": models__fine_model,
"fine_model_but_with_a_no_good_very_long_name.sql": models__file_model_but_with_a_no_good_very_long_name,
"problematic_model.sql": models__problematic_model,
"fine_model.sql": fixtures.models__fine_model,
"fine_model_but_with_a_no_good_very_long_name.sql": fixtures.models__file_model_but_with_a_no_good_very_long_name,
"problematic_model.sql": fixtures.models__problematic_model,
}

@pytest.fixture(scope="class")
Expand All @@ -72,7 +56,7 @@ def project_config_update(self):
"quote_columns": False,
"test": self.column_type_overrides(),
},
"tests": {"+schema": TEST_AUDIT_SCHEMA_SUFFIX},
"data_tests": {"+schema": TEST_AUDIT_SCHEMA_SUFFIX},
}

def column_type_overrides(self):
Expand Down Expand Up @@ -137,7 +121,7 @@ def run_tests_store_failures_and_assert(self, project):
)


class TestStoreTestFailures(StoreTestFailuresBase):
class BaseStoreTestFailures(StoreTestFailuresBase):
@pytest.fixture(scope="function")
def clean_up(self, project):
yield
Expand Down Expand Up @@ -171,11 +155,7 @@ def test__store_and_assert(self, project, clean_up):
self.run_tests_store_failures_and_assert(project)


class TestFabricStoreTestFailures(TestStoreTestFailures):
pass


class TestStoreTestFailuresAsInteractions(basic.StoreTestFailuresAsInteractions):
class TestStoreTestFailures(BaseStoreTestFailures):
pass


Expand Down

0 comments on commit 180ecc0

Please sign in to comment.