-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathtest_model.py
More file actions
151 lines (139 loc) · 5.65 KB
/
test_model.py
File metadata and controls
151 lines (139 loc) · 5.65 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
import pytest
from pathlib import Path
from sqlmesh import Context
from sqlmesh.dbt.common import Dependencies
from sqlmesh.dbt.context import DbtContext
from sqlmesh.dbt.model import ModelConfig
from sqlmesh.dbt.target import PostgresConfig
from sqlmesh.dbt.test import TestConfig
from sqlmesh.utils.errors import ConfigError
from sqlmesh.utils.yaml import YAML
pytestmark = pytest.mark.dbt
def test_model_test_circular_references() -> None:
upstream_model = ModelConfig(name="upstream")
downstream_model = ModelConfig(name="downstream", dependencies=Dependencies(refs={"upstream"}))
context = DbtContext(_refs={"upstream": upstream_model, "downstream": downstream_model})
# Test and downstream model references
downstream_test = TestConfig(
name="downstream_with_upstream",
sql="",
dependencies=Dependencies(refs={"upstream", "downstream"}),
)
upstream_test = TestConfig(
name="upstream_with_downstream",
sql="",
dependencies=Dependencies(refs={"upstream", "downstream"}),
)
downstream_model.tests = [downstream_test]
downstream_model.check_for_circular_test_refs(context)
downstream_model.tests = []
upstream_model.tests = [upstream_test]
with pytest.raises(ConfigError, match="downstream model"):
upstream_model.check_for_circular_test_refs(context)
downstream_model.tests = [downstream_test]
with pytest.raises(ConfigError, match="downstream model"):
upstream_model.check_for_circular_test_refs(context)
downstream_model.check_for_circular_test_refs(context)
# Test only references
downstream_model.dependencies = Dependencies()
with pytest.raises(ConfigError, match="between tests"):
upstream_model.check_for_circular_test_refs(context)
with pytest.raises(ConfigError, match="between tests"):
downstream_model.check_for_circular_test_refs(context)
@pytest.mark.slow
def test_load_invalid_ref_audit_constraints(
tmp_path: Path, caplog, dbt_dummy_postgres_config: PostgresConfig
) -> None:
yaml = YAML()
dbt_project_dir = tmp_path / "dbt"
dbt_project_dir.mkdir()
dbt_model_dir = dbt_project_dir / "models"
dbt_model_dir.mkdir()
full_model_contents = "SELECT 1 as cola"
full_model_file = dbt_model_dir / "full_model.sql"
with open(full_model_file, "w", encoding="utf-8") as f:
f.write(full_model_contents)
model_schema = {
"version": 2,
"models": [
{
"name": "full_model",
"description": "A full model bad ref for audit and constraints",
"columns": [
{
"name": "cola",
"description": "A column that is used in a ref audit and constraints",
"constraints": [
{
"type": "primary_key",
"columns": ["cola"],
"expression": "ref('not_real_model') (cola)",
}
],
"tests": [
{
# References a model that doesn't exist
"relationships": {
"to": "ref('not_real_model')",
"field": "cola",
},
},
{
# Reference a source that doesn't exist
"relationships": {
"to": "source('not_real_source', 'not_real_table')",
"field": "cola",
},
},
],
}
],
}
],
}
model_schema_file = dbt_model_dir / "schema.yml"
with open(model_schema_file, "w", encoding="utf-8") as f:
yaml.dump(model_schema, f)
dbt_project_config = {
"name": "invalid_ref_audit_constraints",
"version": "1.0.0",
"config-version": 2,
"profile": "test",
"model-paths": ["models"],
}
dbt_project_file = dbt_project_dir / "dbt_project.yml"
with open(dbt_project_file, "w", encoding="utf-8") as f:
yaml.dump(dbt_project_config, f)
sqlmesh_config = {
"model_defaults": {
"start": "2025-01-01",
}
}
sqlmesh_config_file = dbt_project_dir / "sqlmesh.yaml"
with open(sqlmesh_config_file, "w", encoding="utf-8") as f:
yaml.dump(sqlmesh_config, f)
dbt_data_dir = tmp_path / "dbt_data"
dbt_data_dir.mkdir()
dbt_data_file = dbt_data_dir / "local.db"
dbt_profile_config = {
"test": {
"outputs": {"duckdb": {"type": "duckdb", "path": str(dbt_data_file)}},
"target": "duckdb",
}
}
db_profile_file = dbt_project_dir / "profiles.yml"
with open(db_profile_file, "w", encoding="utf-8") as f:
yaml.dump(dbt_profile_config, f)
context = Context(paths=dbt_project_dir)
assert (
"Skipping audit 'relationships_full_model_cola__cola__ref_not_real_model_' because model 'not_real_model' is not a valid ref"
in caplog.text
)
assert (
"Skipping audit 'relationships_full_model_cola__cola__source_not_real_source_not_real_table_' because source 'not_real_source.not_real_table' is not a valid ref"
in caplog.text
)
fqn = '"local"."main"."full_model"'
assert fqn in context.snapshots
# The audit isn't loaded due to the invalid ref
assert context.snapshots[fqn].model.audits == []