-
Notifications
You must be signed in to change notification settings - Fork 98
/
test_materialized_view.py
193 lines (160 loc) · 5.56 KB
/
test_materialized_view.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
"""
test materialized view creation. This is ClickHouse specific, which has a significantly different implementation
of materialized views from PostgreSQL or Oracle
"""
import json
import pytest
from dbt.tests.util import check_relation_types, run_dbt
from dbt.adapters.clickhouse.query import quote_identifier
PEOPLE_SEED_CSV = """
id,name,age,department
1231,Dade,33,engineering
6666,Ksenia,48,engineering
8888,Kate,50,engineering
""".lstrip()
# This model is parameterized, in a way, by the "run_type" dbt project variable
# This is to be able to switch between different model definitions within
# the same test run and allow us to test the evolution of a materialized view
MV_MODEL = """
{{ config(
materialized='materialized_view',
engine='MergeTree()',
order_by='(id)',
schema='custom_schema',
) }}
{% if var('run_type', '') == '' %}
select
id,
name,
case
when name like 'Dade' then 'crash_override'
when name like 'Kate' then 'acid burn'
else 'N/A'
end as hacker_alias
from {{ source('raw', 'people') }}
where department = 'engineering'
{% else %}
select
id,
name,
case
-- Dade wasn't always known as 'crash override'!
when name like 'Dade' and age = 11 then 'zero cool'
when name like 'Dade' and age != 11 then 'crash override'
when name like 'Kate' then 'acid burn'
else 'N/A'
end as hacker_alias
from {{ source('raw', 'people') }}
where department = 'engineering'
{% endif %}
"""
SEED_SCHEMA_YML = """
version: 2
sources:
- name: raw
schema: "{{ target.schema }}"
tables:
- name: people
"""
class TestBasicMV:
@pytest.fixture(scope="class")
def seeds(self):
"""
we need a base table to pull from
"""
return {
"people.csv": PEOPLE_SEED_CSV,
"schema.yml": SEED_SCHEMA_YML,
}
@pytest.fixture(scope="class")
def models(self):
return {
"hackers.sql": MV_MODEL,
}
def test_create(self, project):
"""
1. create a base table via dbt seed
2. create a model as a materialized view, selecting from the table created in (1)
3. insert data into the base table and make sure it's there in the target table created in (2)
"""
schema = quote_identifier(project.test_schema + "_custom_schema")
results = run_dbt(["seed"])
assert len(results) == 1
columns = project.run_sql("DESCRIBE TABLE people", fetch="all")
assert columns[0][1] == "Int32"
# create the model
results = run_dbt()
assert len(results) == 1
columns = project.run_sql(f"DESCRIBE TABLE {schema}.hackers", fetch="all")
assert columns[0][1] == "Int32"
columns = project.run_sql(f"DESCRIBE {schema}.hackers_mv", fetch="all")
assert columns[0][1] == "Int32"
check_relation_types(
project.adapter,
{
"hackers_mv": "view",
"hackers": "table",
},
)
# insert some data and make sure it reaches the target table
project.run_sql(
f"""
insert into {quote_identifier(project.test_schema)}.people ("id", "name", "age", "department")
values (1232,'Dade',16,'engineering'), (9999,'eugene',40,'malware');
"""
)
result = project.run_sql(f"select count(*) from {schema}.hackers", fetch="all")
assert result[0][0] == 4
class TestUpdateMV:
@pytest.fixture(scope="class")
def seeds(self):
"""
we need a base table to pull from
"""
return {
"people.csv": PEOPLE_SEED_CSV,
"schema.yml": SEED_SCHEMA_YML,
}
@pytest.fixture(scope="class")
def models(self):
return {
"hackers.sql": MV_MODEL,
}
def test_update_incremental(self, project):
schema = quote_identifier(project.test_schema + "_custom_schema")
# create our initial materialized view
run_dbt(["seed"])
run_dbt()
# re-run dbt but this time with the new MV SQL
run_vars = {"run_type": "extended_schema"}
run_dbt(["run", "--vars", json.dumps(run_vars)])
project.run_sql(
f"""
insert into {quote_identifier(project.test_schema)}.people ("id", "name", "age", "department")
values (1232,'Dade',11,'engineering'), (9999,'eugene',40,'malware');
"""
)
# assert that we now have both of Dade's aliases in our hackers table
result = project.run_sql(
f"select distinct hacker_alias from {schema}.hackers where name = 'Dade'", fetch="all"
)
assert len(result) == 2
def test_update_full_refresh(self, project):
schema = quote_identifier(project.test_schema + "_custom_schema")
# create our initial materialized view
run_dbt(["seed"])
run_dbt()
# re-run dbt but this time with the new MV SQL
run_vars = {"run_type": "extended_schema"}
run_dbt(["run", "--full-refresh", "--vars", json.dumps(run_vars)])
project.run_sql(
f"""
insert into {quote_identifier(project.test_schema)}.people ("id", "name", "age", "department")
values (1232,'Dade',11,'engineering'), (9999,'eugene',40,'malware');
"""
)
# assert that we now have both of Dade's aliases in our hackers table
result = project.run_sql(
f"select distinct hacker_alias from {schema}.hackers where name = 'Dade'", fetch="all"
)
assert len(result) == 2