-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathtest_schema_loader.py
More file actions
372 lines (293 loc) · 12.1 KB
/
test_schema_loader.py
File metadata and controls
372 lines (293 loc) · 12.1 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import pytest
import typing as t
from pathlib import Path
from unittest.mock import patch
import pandas as pd # noqa: TID253
from pytest_mock.plugin import MockerFixture
from sqlglot import exp, parse_one
from sqlmesh.core import constants as c
from sqlmesh.core.config import Config, DuckDBConnectionConfig, GatewayConfig
from sqlmesh.core.context import Context
from sqlmesh.core.dialect import parse
from sqlmesh.core.model import SqlModel, create_external_model, load_sql_based_model
from sqlmesh.core.model.definition import ExternalModel
from sqlmesh.core.schema_loader import create_external_models_file
from sqlmesh.core.snapshot import SnapshotChangeCategory
from sqlmesh.utils import yaml
from sqlmesh.utils.errors import SQLMeshError
def test_create_external_models(tmp_path, assert_exp_eq):
config = Config(gateways=GatewayConfig(connection=DuckDBConnectionConfig()))
context = Context(paths=[tmp_path], config=config)
# `fruits` is used by DuckDB in the upcoming select query
fruits = pd.DataFrame(
[
{"id": 1, "name": "apple"},
{"id": 2, "name": "banana"},
]
)
cursor = context.engine_adapter.cursor
cursor.execute("CREATE SCHEMA sushi")
cursor.execute("CREATE TABLE sushi.raw_fruits AS SELECT * FROM fruits")
model = load_sql_based_model(
parse(
"""
MODEL (
name sushi.fruits,
kind FULL,
);
SELECT name FROM sushi.raw_fruits
""",
),
default_catalog="memory",
)
context.upsert_model(model)
context.create_external_models()
assert context.models['"memory"."sushi"."fruits"'].columns_to_types == {
"name": exp.DataType.build("UNKNOWN")
}
context.load()
model = load_sql_based_model(
parse(
"""
MODEL (
name sushi.fruits,
kind FULL,
);
SELECT * FROM sushi.raw_fruits
""",
),
default_catalog="memory",
)
context.upsert_model(model)
raw_fruits = context.models['"memory"."sushi"."raw_fruits"']
assert raw_fruits.kind.is_symbolic
assert raw_fruits.kind.is_external
assert raw_fruits.columns_to_types == {
"id": exp.DataType.build("BIGINT"),
"name": exp.DataType.build("TEXT"),
}
snapshot = context.get_snapshot("sushi.raw_fruits", raise_if_missing=True)
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
fruits = context.models['"memory"."sushi"."fruits"']
assert not fruits.kind.is_symbolic
assert not fruits.kind.is_external
assert fruits.columns_to_types == {
"id": exp.DataType.build("BIGINT"),
"name": exp.DataType.build("TEXT"),
}
assert_exp_eq(
fruits.render_query(snapshots=context.snapshots),
"""
SELECT
"raw_fruits"."id" AS "id",
"raw_fruits"."name" AS "name"
FROM "memory"."sushi"."raw_fruits" AS "raw_fruits"
""",
)
# rerunning create external models should refetch existing external models
context.create_external_models()
context.load()
assert context.models['"memory"."sushi"."raw_fruits"']
# there was no explicit gateway, so it should not be written to the external models schema
assert context.models['"memory"."sushi"."raw_fruits"'].gateway is None
def test_gateway_specific_external_models(tmp_path: Path):
gateways = {
"dev": GatewayConfig(connection=DuckDBConnectionConfig()),
"prod": GatewayConfig(connection=DuckDBConnectionConfig()),
}
config = Config(gateways=gateways, default_gateway="dev")
dev_context = Context(paths=[tmp_path], config=config, gateway="dev")
dev_context.engine_adapter.execute("create schema landing")
dev_context.engine_adapter.execute("create table landing.dev_source as select 1")
dev_context.engine_adapter.execute("create schema lake")
prod_context = Context(paths=[tmp_path], config=config, gateway="prod")
prod_context.engine_adapter.execute("create schema landing")
prod_context.engine_adapter.execute("create table landing.prod_source as select 1")
prod_context.engine_adapter.execute("create schema lake")
def _create_model(gateway: str):
return load_sql_based_model(
parse(
"""
MODEL (
name lake.table,
kind FULL,
);
SELECT * FROM landing.@{gateway}_source
""",
),
variables={"gateway": gateway},
default_catalog="memory",
)
dev_context.upsert_model(_create_model("dev"))
prod_context.upsert_model(_create_model("prod"))
dev_context.create_external_models()
dev_context.load()
dev_models = dev_context.models
assert len(dev_models) == 1
dev_model = t.cast(ExternalModel, dev_models['"memory"."landing"."dev_source"'])
assert dev_model.gateway == "dev"
prod_context.create_external_models()
prod_context.load()
prod_models = prod_context.models
prod_model = t.cast(ExternalModel, prod_models['"memory"."landing"."prod_source"'])
assert prod_model.gateway == "prod"
# each context can only see models for its own gateway
# check that models from both gateways present in the file, to show that prod_context.create_external_models() didnt clobber the dev ones
contents = yaml.load(tmp_path / c.EXTERNAL_MODELS_YAML)
assert len(contents) == 2
assert len([c for c in contents if c["name"] == '"memory"."landing"."dev_source"']) == 1
assert len([c for c in contents if c["name"] == '"memory"."landing"."prod_source"']) == 1
def test_gateway_specific_external_models_mixed_with_others(tmp_path: Path):
def _init_db(ctx: Context):
ctx.engine_adapter.execute("create schema landing")
ctx.engine_adapter.execute("create table landing.source_table as select 1")
ctx.engine_adapter.execute("create schema lake")
gateways = {
"dev": GatewayConfig(connection=DuckDBConnectionConfig()),
"prod": GatewayConfig(connection=DuckDBConnectionConfig()),
}
config = Config(gateways=gateways, default_gateway="dev")
model_dir = tmp_path / c.MODELS
model_dir.mkdir()
with open(model_dir / "table.sql", "w", encoding="utf8") as fd:
fd.write(
"""
MODEL (
name lake.table,
kind FULL,
);
SELECT * FROM landing.source_table
""",
)
ctx = Context(paths=[tmp_path], config=config) # note: No explicitly defined gateway
assert ctx.gateway is None
assert ctx.selected_gateway == "dev"
_init_db(ctx)
ctx.load()
assert len(ctx.models) == 1
assert '"memory"."lake"."table"' in ctx.models
ctx.create_external_models()
# no gateway was specifically chosen; external models should be created against the default gateway
external_models_filename = tmp_path / c.EXTERNAL_MODELS_YAML
contents = yaml.load(external_models_filename)
assert len(contents) == 1
assert contents[0]["gateway"] == "dev"
ctx.load()
assert len(ctx.models) == 2
assert '"memory"."landing"."source_table"' in ctx.models
assert '"memory"."lake"."table"' in ctx.models
# explicitly set --gateway prod
prod_ctx = Context(paths=[tmp_path], config=config, gateway="prod")
assert prod_ctx.gateway == "prod"
assert prod_ctx.selected_gateway == "prod"
_init_db(prod_ctx)
prod_ctx.create_external_models()
# there should now be 2 external models with the same name - one with a gateway=dev and one with gateway=prod
contents = yaml.load(external_models_filename)
assert len(contents) == 2
assert sorted([contents[0]["gateway"], contents[1]["gateway"]]) == ["dev", "prod"]
assert contents[0]["name"] == contents[1]["name"]
# check that this doesnt present a problem on load
prod_ctx.load()
external_models = [m for _, m in prod_ctx.models.items() if type(m) == ExternalModel]
assert len(external_models) == 1
assert external_models[0].name == '"memory"."landing"."source_table"'
assert external_models[0].gateway == "prod"
def test_gateway_specific_external_models_default_gateway(tmp_path: Path):
model_0 = {"name": "db.model0", "columns": {"a": "int"}}
model_1 = {"name": "db.model1", "gateway": "dev", "columns": {"a": "int"}}
model_2 = {"name": "db.model2", "gateway": "prod", "columns": {"a": "int"}}
with open(tmp_path / c.EXTERNAL_MODELS_YAML, "w", encoding="utf8") as fd:
yaml.dump([model_0, model_1, model_2], fd)
gateways = {
"dev": GatewayConfig(connection=DuckDBConnectionConfig()),
"prod": GatewayConfig(connection=DuckDBConnectionConfig()),
}
config = Config(gateways=gateways, default_gateway="prod")
ctx = Context(paths=[tmp_path], config=config)
ctx.load()
assert len(ctx.models) == 2
model_names = list(ctx.models.keys())
assert '"memory"."db"."model0"' in model_names
assert '"memory"."db"."model2"' in model_names
def test_create_external_models_no_duplicates(tmp_path: Path):
config = Config(gateways={"": GatewayConfig(connection=DuckDBConnectionConfig())})
model_dir = tmp_path / c.MODELS
model_dir.mkdir()
with open(model_dir / "table.sql", "w", encoding="utf8") as fd:
fd.write(
"""
MODEL (
name lake.table,
kind FULL,
);
SELECT * FROM landing.source_table
""",
)
ctx = Context(paths=[tmp_path], config=config)
assert ctx.gateway is None
ctx.engine_adapter.execute("create schema landing")
ctx.engine_adapter.execute("create table landing.source_table as select 1")
ctx.engine_adapter.execute("create schema lake")
def _load_external_models():
return yaml.load(tmp_path / c.EXTERNAL_MODELS_YAML)
ctx.create_external_models()
assert len(_load_external_models()) == 1
# check no duplicates when writing the same models
# (since the file gets mutated and not replaced)
ctx.create_external_models()
assert len(_load_external_models()) == 1
def test_no_internal_model_conversion(tmp_path: Path, mocker: MockerFixture):
engine_adapter_mock = mocker.Mock()
engine_adapter_mock.columns.return_value = {
"b": exp.DataType.build("text"),
"a": exp.DataType.build("bigint"),
}
state_reader_mock = mocker.Mock()
state_reader_mock.nodes_exist.return_value = {'"model_b"'}
model_a = SqlModel(name="a", query=parse_one("select * FROM model_b, tbl_c"))
model_b = SqlModel(name="b", query=parse_one("select * FROM `tbl-d`", read="bigquery"))
filename = tmp_path / c.EXTERNAL_MODELS_YAML
create_external_models_file(
filename,
{ # type: ignore
"a": model_a,
"b": model_b,
},
engine_adapter_mock,
state_reader_mock,
"bigquery",
)
schema = yaml.load(filename)
assert len(schema) == 2
assert schema[0]["name"] == "`tbl-d`"
assert list(schema[0]["columns"]) == ["b", "a"]
assert schema[1]["name"] == "`tbl_c`"
assert list(schema[1]["columns"]) == ["b", "a"]
for row in schema:
create_external_model(**row, dialect="bigquery")
def test_missing_table(tmp_path: Path):
config = Config(gateways=GatewayConfig(connection=DuckDBConnectionConfig()))
context = Context(paths=[str(tmp_path.absolute())], config=config)
model = SqlModel(name="a", query=parse_one("select * FROM tbl_source"))
filename = tmp_path / c.EXTERNAL_MODELS_YAML
with patch.object(context.console, "log_warning") as mock_logger:
create_external_models_file(
filename,
{"a": model}, # type: ignore
context.engine_adapter,
context.state_reader,
"",
)
assert """Unable to get schema for '"tbl_source"'""" in mock_logger.call_args[0][0]
schema = yaml.load(filename)
assert len(schema) == 0
with pytest.raises(SQLMeshError, match=r"""Unable to get schema for '"tbl_source"'.*"""):
create_external_models_file(
filename,
{"a": model}, # type: ignore
context.engine_adapter,
context.state_reader,
"",
strict=True,
)