Skip to content

Commit 0b0ba9c

Browse files
committed
WIP: seed tests
1 parent 924c9d8 commit 0b0ba9c

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

tests/functional/adapter/test_seed.py

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

0 commit comments

Comments
 (0)