Skip to content

Commit 71f3eb7

Browse files
authored
fix: prevent duplicate audit names (#5431)
1 parent d15446c commit 71f3eb7

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

sqlmesh/dbt/basemodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def sqlmesh_model_kwargs(
305305
jinja_macros.add_globals(self._model_jinja_context(model_context, dependencies))
306306

307307
model_kwargs = {
308-
"audits": [(test.name, {}) for test in self.tests],
308+
"audits": [(test.canonical_name, {}) for test in self.tests],
309309
"column_descriptions": column_descriptions_to_sqlmesh(self.columns) or None,
310310
"depends_on": {
311311
model.canonical_name(context) for model in model_context.refs.values()

sqlmesh/dbt/loader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def _load_audits(
172172
for test in package.tests.values():
173173
logger.debug("Converting '%s' to sqlmesh format", test.name)
174174
try:
175-
audits[test.name] = test.to_sqlmesh(package_context)
175+
audits[test.canonical_name] = test.to_sqlmesh(package_context)
176+
176177
except BaseMissingReferenceError as e:
177178
ref_type = "model" if isinstance(e, MissingModelError) else "source"
178179
logger.warning(

sqlmesh/dbt/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def _validate_severity(cls, v: t.Union[Severity, str]) -> Severity:
109109
def _lowercase_name(cls, v: str) -> str:
110110
return v.lower()
111111

112+
@property
113+
def canonical_name(self) -> str:
114+
return f"{self.package_name}.{self.name}" if self.package_name else self.name
115+
112116
@property
113117
def is_standalone(self) -> bool:
114118
# A test is standalone if:

tests/core/integration/test_dbt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_dbt_is_incremental_table_is_missing(sushi_test_dbt_context: Context):
4848
model = context.get_model("sushi.waiter_revenue_by_day_v2")
4949
model = model.copy(update={"kind": IncrementalUnmanagedKind(), "start": "2023-01-01"})
5050
context.upsert_model(model)
51-
context._standalone_audits["test_top_waiters"].start = "2023-01-01"
51+
context._standalone_audits["sushi.test_top_waiters"].start = "2023-01-01"
5252

5353
context.plan("prod", auto_apply=True, no_prompts=True, skip_tests=True)
5454

tests/dbt/test_model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,23 @@ def test_manifest_filters_standalone_tests_from_models(
190190
# Should only have "not_null" test, not the "relationships" test
191191
model1_audit_names = [audit[0] for audit in model1_snapshot.model.audits]
192192
assert len(model1_audit_names) == 1
193-
assert model1_audit_names[0] == "not_null_model1_id"
193+
assert model1_audit_names[0] == "local.not_null_model1_id"
194194

195195
# Verify model2 has its non-standalone test
196196
model2_audit_names = [audit[0] for audit in model2_snapshot.model.audits]
197197
assert len(model2_audit_names) == 1
198-
assert model2_audit_names[0] == "not_null_model2_id"
198+
assert model2_audit_names[0] == "local.not_null_model2_id"
199199

200200
# Verify the standalone test (relationships) exists as a StandaloneAudit
201201
all_non_standalone_audits = [name for name in context._audits]
202202
assert sorted(all_non_standalone_audits) == [
203-
"not_null_model1_id",
204-
"not_null_model2_id",
203+
"local.not_null_model1_id",
204+
"local.not_null_model2_id",
205205
]
206206

207207
standalone_audits = [name for name in context._standalone_audits]
208208
assert len(standalone_audits) == 1
209-
assert standalone_audits[0] == "relationships_model1_id__id__ref_model2_"
209+
assert standalone_audits[0] == "local.relationships_model1_id__id__ref_model2_"
210210

211211
plan_builder = context.plan_builder()
212212
dag = plan_builder._build_dag()

0 commit comments

Comments
 (0)