-
Notifications
You must be signed in to change notification settings - Fork 175
/
test_constraints.py
202 lines (171 loc) · 5.54 KB
/
test_constraints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import pytest
from dbt.tests.adapter.constraints.test_constraints import (
BaseTableConstraintsColumnsEqual,
BaseViewConstraintsColumnsEqual,
BaseTableContractSqlHeader,
BaseIncrementalContractSqlHeader,
BaseIncrementalConstraintsColumnsEqual,
BaseConstraintsRuntimeDdlEnforcement,
BaseConstraintsRollback,
BaseIncrementalConstraintsRuntimeDdlEnforcement,
BaseIncrementalConstraintsRollback,
BaseModelConstraintsRuntimeEnforcement,
BaseConstraintQuotedColumn,
)
from dbt.tests.adapter.constraints.fixtures import (
model_contract_header_schema_yml,
)
my_model_contract_sql_header_sql = """
{{
config(
materialized = "table"
)
}}
{% call set_sql_header(config) %}
SET MY_VARIABLE='test';
{% endcall %}
SELECT $MY_VARIABLE as column_name
"""
my_model_incremental_contract_sql_header_sql = """
{{
config(
materialized = "incremental",
on_schema_change="append_new_columns"
)
}}
{% call set_sql_header(config) %}
SET MY_VARIABLE='test';
{% endcall %}
SELECT $MY_VARIABLE as column_name
"""
_expected_sql_snowflake = """
create or replace transient table <model_identifier> (
id integer not null primary key references <foreign_key_model_identifier> (id) unique,
color text,
date_day text
) as ( select
id,
color,
date_day from
(
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day
) as model_subq
);
"""
class SnowflakeColumnEqualSetup:
@pytest.fixture
def int_type(self):
return "FIXED"
@pytest.fixture
def schema_int_type(self):
return "INT"
@pytest.fixture
def data_types(self, int_type, schema_int_type, string_type):
# sql_column_value, schema_data_type, error_data_type
return [
["1", schema_int_type, int_type],
["'1'", string_type, string_type],
["cast('2019-01-01' as date)", "date", "DATE"],
["true", "boolean", "BOOLEAN"],
["'2013-11-03 00:00:00-07'::timestamptz", "timestamp_tz", "TIMESTAMP_TZ"],
["'2013-11-03 00:00:00-07'::timestamp", "timestamp", "TIMESTAMP_NTZ"],
["ARRAY_CONSTRUCT('a','b','c')", "array", "ARRAY"],
["ARRAY_CONSTRUCT(1,2,3)", "array", "ARRAY"],
["TO_GEOGRAPHY('POINT(-122.35 37.55)')", "geography", "GEOGRAPHY"],
["TO_GEOMETRY('POINT(1820.12 890.56)')", "geometry", "GEOMETRY"],
[
"""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""",
"variant",
"VARIANT",
],
]
class TestSnowflakeTableConstraintsColumnsEqual(
SnowflakeColumnEqualSetup, BaseTableConstraintsColumnsEqual
):
pass
class TestSnowflakeViewConstraintsColumnsEqual(
SnowflakeColumnEqualSetup, BaseViewConstraintsColumnsEqual
):
pass
class TestSnowflakeIncrementalConstraintsColumnsEqual(
SnowflakeColumnEqualSetup, BaseIncrementalConstraintsColumnsEqual
):
pass
class TestSnowflakeTableContractsSqlHeader(BaseTableContractSqlHeader):
@pytest.fixture(scope="class")
def models(self):
return {
"my_model_contract_sql_header.sql": my_model_contract_sql_header_sql,
"constraints_schema.yml": model_contract_header_schema_yml,
}
class TestSnowflakeIncrementalContractsSqlHeader(BaseIncrementalContractSqlHeader):
@pytest.fixture(scope="class")
def models(self):
return {
"my_model_contract_sql_header.sql": my_model_incremental_contract_sql_header_sql,
"constraints_schema.yml": model_contract_header_schema_yml,
}
class TestSnowflakeTableConstraintsDdlEnforcement(BaseConstraintsRuntimeDdlEnforcement):
@pytest.fixture(scope="class")
def expected_sql(self):
return _expected_sql_snowflake
class TestSnowflakeIncrementalConstraintsDdlEnforcement(
BaseIncrementalConstraintsRuntimeDdlEnforcement
):
@pytest.fixture(scope="class")
def expected_sql(self):
return _expected_sql_snowflake
class TestSnowflakeTableConstraintsRollback(BaseConstraintsRollback):
@pytest.fixture(scope="class")
def expected_error_messages(self):
return ["NULL result in a non-nullable column"]
class TestSnowflakeIncrementalConstraintsRollback(BaseIncrementalConstraintsRollback):
@pytest.fixture(scope="class")
def expected_error_messages(self):
return ["NULL result in a non-nullable column"]
class TestSnowflakeModelConstraintsRuntimeEnforcement(BaseModelConstraintsRuntimeEnforcement):
@pytest.fixture(scope="class")
def expected_sql(self):
return """
create or replace transient table <model_identifier> (
id integer not null,
color text,
date_day text,
primary key (id),
constraint strange_uniqueness_requirement unique (color, date_day),
foreign key (id) references <foreign_key_model_identifier> (id)
) as ( select
id,
color,
date_day from
(
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day
) as model_subq
);
"""
class TestSnowflakeConstraintQuotedColumn(BaseConstraintQuotedColumn):
@pytest.fixture(scope="class")
def expected_sql(self):
return """
create or replace transient table <model_identifier> (
id integer not null,
"from" text not null,
date_day text
) as (
select id, "from", date_day
from (
select
'blue' as "from",
1 as id,
'2019-01-01' as date_day
) as model_subq
);
"""