Skip to content

Commit 230f842

Browse files
authored
Merge pull request #366 from dbt-msft/seed-tests
add seed tests and add cascade to drop relation
2 parents 48fc41e + 39e7f1d commit 230f842

File tree

2 files changed

+206
-11
lines changed

2 files changed

+206
-11
lines changed

dbt/include/sqlserver/macros/adapters/relation.sql

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@
1313
{% endmacro %}
1414

1515
{% macro sqlserver__drop_relation_script(relation) -%}
16-
{% if relation.type == 'view' -%}
17-
{% set object_id_type = 'V' %}
18-
{% elif relation.type == 'table'%}
19-
{% set object_id_type = 'U' %}
20-
{%- else -%} invalid target name
21-
{% endif %}
22-
USE [{{ relation.database }}];
23-
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
24-
begin
25-
drop {{ relation.type }} {{ relation.include(database=False) }}
26-
end
16+
{% call statement('find_references', fetch_result=true) %}
17+
USE [{{ relation.database }}];
18+
SELECT referencing_schema_name, referencing_entity_name
19+
FROM sys.dm_sql_referencing_entities ('{{ relation.include(database=false) }}', 'object')
20+
{% endcall %}
21+
{% set references = load_result('find_references')['data'] %}
22+
{% for reference in references -%}
23+
-- dropping referenced view {{ reference[0] }}.{{ reference[1] }}
24+
{{ sqlserver__drop_relation_script(relation.incorporate(
25+
type="view",
26+
path={"schema": reference[0], "identifier": reference[1]})) }}
27+
{% endfor %}
28+
{% if relation.type == 'view' -%}
29+
{% set object_id_type = 'V' %}
30+
{% elif relation.type == 'table'%}
31+
{% set object_id_type = 'U' %}
32+
{%- else -%}
33+
{{ exceptions.raise_not_implemented('Invalid relation being dropped: ' ~ relation) }}
34+
{% endif %}
35+
USE [{{ relation.database }}];
36+
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
37+
begin
38+
drop {{ relation.type }} {{ relation.include(database=False) }}
39+
end
2740
{% endmacro %}
2841

2942
{% macro sqlserver__rename_relation(from_relation, to_relation) -%}

tests/functional/adapter/test_seed.py

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import pytest
2+
from dbt.tests.adapter.simple_seed.seeds import seeds__expected_sql
3+
from dbt.tests.adapter.simple_seed.test_seed import TestBasicSeedTests as BaseBasicSeedTests
4+
from dbt.tests.adapter.simple_seed.test_seed import (
5+
TestSeedConfigFullRefreshOff as BaseSeedConfigFullRefreshOff,
6+
)
7+
from dbt.tests.adapter.simple_seed.test_seed import (
8+
TestSeedConfigFullRefreshOn as BaseSeedConfigFullRefreshOn,
9+
)
10+
from dbt.tests.adapter.simple_seed.test_seed import TestSeedCustomSchema as BaseSeedCustomSchema
11+
from dbt.tests.adapter.simple_seed.test_seed import TestSeedParsing as BaseSeedParsing
12+
from dbt.tests.adapter.simple_seed.test_seed import (
13+
TestSeedSpecificFormats as BaseSeedSpecificFormats,
14+
)
15+
from dbt.tests.adapter.simple_seed.test_seed import (
16+
TestSimpleSeedEnabledViaConfig as BaseSimpleSeedEnabledViaConfig,
17+
)
18+
from dbt.tests.adapter.simple_seed.test_seed_type_override import (
19+
BaseSimpleSeedColumnOverride,
20+
seeds__disabled_in_config_csv,
21+
seeds__enabled_in_config_csv,
22+
)
23+
from dbt.tests.util import get_connection
24+
25+
from dbt.adapters.sqlserver import SQLServerAdapter
26+
27+
fixed_setup_sql = seeds__expected_sql.replace("TIMESTAMP WITHOUT TIME ZONE", "DATETIME").replace(
28+
"TEXT", "VARCHAR(255)"
29+
)
30+
31+
seeds__tricky_csv = """
32+
seed_id,seed_id_str,a_bool,looks_like_a_bool,a_date,looks_like_a_date,relative,weekday
33+
1,1,1,1,2019-01-01 12:32:30,2019-01-01 12:32:30,tomorrow,Saturday
34+
2,2,1,1,2019-01-01 12:32:31,2019-01-01 12:32:31,today,Sunday
35+
3,3,1,1,2019-01-01 12:32:32,2019-01-01 12:32:32,yesterday,Monday
36+
4,4,0,0,2019-01-01 01:32:32,2019-01-01 01:32:32,tomorrow,Saturday
37+
5,5,0,0,2019-01-01 01:32:32,2019-01-01 01:32:32,today,Sunday
38+
6,6,0,0,2019-01-01 01:32:32,2019-01-01 01:32:32,yesterday,Monday
39+
""".lstrip()
40+
41+
macros__schema_test = """
42+
{% test column_type(model, column_name, type) %}
43+
44+
{% set cols = adapter.get_columns_in_relation(model) %}
45+
46+
{% set col_types = {} %}
47+
{% for col in cols %}
48+
{% do col_types.update({col.name: col.data_type}) %}
49+
{% endfor %}
50+
51+
{% set col_type = col_types.get(column_name) %}
52+
{% set col_type = 'text' if col_type and 'character varying' in col_type else col_type %}
53+
54+
{% set validation_message = 'Got a column type of ' ~ col_type ~ ', expected ' ~ type %}
55+
56+
{% set val = 0 if col_type == type else 1 %}
57+
{% if val == 1 and execute %}
58+
{{ log(validation_message, info=True) }}
59+
{% endif %}
60+
61+
select '{{ validation_message }}' as validation_error
62+
from (select 1 as empty) as nothing
63+
where {{ val }} = 1
64+
65+
{% endtest %}
66+
67+
"""
68+
69+
properties__schema_yml = """
70+
version: 2
71+
seeds:
72+
- name: seed_enabled
73+
columns:
74+
- name: birthday
75+
tests:
76+
- column_type:
77+
type: date
78+
- name: seed_id
79+
tests:
80+
- column_type:
81+
type: text
82+
83+
- name: seed_tricky
84+
columns:
85+
- name: seed_id
86+
tests:
87+
- column_type:
88+
type: int
89+
- name: seed_id_str
90+
tests:
91+
- column_type:
92+
type: text
93+
- name: a_bool
94+
tests:
95+
- column_type:
96+
type: int
97+
- name: looks_like_a_bool
98+
tests:
99+
- column_type:
100+
type: text
101+
- name: a_date
102+
tests:
103+
- column_type:
104+
type: datetime
105+
- name: looks_like_a_date
106+
tests:
107+
- column_type:
108+
type: text
109+
- name: relative
110+
tests:
111+
- column_type:
112+
type: text
113+
- name: weekday
114+
tests:
115+
- column_type:
116+
type: text
117+
"""
118+
119+
120+
class TestSimpleSeedColumnOverrideSQLServer(BaseSimpleSeedColumnOverride):
121+
@pytest.fixture(scope="class")
122+
def seeds(self):
123+
return {
124+
"seed_enabled.csv": seeds__enabled_in_config_csv,
125+
"seed_disabled.csv": seeds__disabled_in_config_csv,
126+
"seed_tricky.csv": seeds__tricky_csv,
127+
}
128+
129+
@pytest.fixture(scope="class")
130+
def macros(self):
131+
return {"schema_test.sql": macros__schema_test}
132+
133+
@pytest.fixture(scope="class")
134+
def models(self):
135+
return {
136+
"schema.yml": properties__schema_yml,
137+
}
138+
139+
140+
class TestBasicSeedTestsSQLServer(BaseBasicSeedTests):
141+
@pytest.fixture(scope="class", autouse=True)
142+
def setUp(self, project):
143+
project.run_sql(fixed_setup_sql)
144+
145+
146+
class TestSeedConfigFullRefreshOnSQLServer(BaseSeedConfigFullRefreshOn):
147+
@pytest.fixture(scope="class", autouse=True)
148+
def setUp(self, project):
149+
project.run_sql(fixed_setup_sql)
150+
151+
152+
class TestSeedConfigFullRefreshOffSQLServer(BaseSeedConfigFullRefreshOff):
153+
@pytest.fixture(scope="class", autouse=True)
154+
def setUp(self, project):
155+
project.run_sql(fixed_setup_sql)
156+
157+
158+
class TestSeedCustomSchemaSQLServer(BaseSeedCustomSchema):
159+
@pytest.fixture(scope="class", autouse=True)
160+
def setUp(self, project):
161+
project.run_sql(fixed_setup_sql)
162+
163+
164+
class TestSimpleSeedEnabledViaConfigSQLServer(BaseSimpleSeedEnabledViaConfig):
165+
@pytest.fixture(scope="function")
166+
def clear_test_schema(self, project):
167+
yield
168+
adapter = project.adapter
169+
assert isinstance(project.adapter, SQLServerAdapter)
170+
with get_connection(project.adapter):
171+
rel = adapter.Relation.create(database=project.database, schema=project.test_schema)
172+
adapter.drop_schema(rel)
173+
174+
175+
class TestSeedParsingSQLServer(BaseSeedParsing):
176+
@pytest.fixture(scope="class", autouse=True)
177+
def setUp(self, project):
178+
project.run_sql(fixed_setup_sql)
179+
180+
181+
class TestSeedSpecificFormatsSQLServer(BaseSeedSpecificFormats):
182+
pass

0 commit comments

Comments
 (0)