|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from sqlmesh import Context |
3 | 6 | from sqlmesh.dbt.common import Dependencies |
4 | 7 | from sqlmesh.dbt.context import DbtContext |
5 | 8 | from sqlmesh.dbt.model import ModelConfig |
6 | 9 | from sqlmesh.dbt.test import TestConfig |
7 | 10 | from sqlmesh.utils.errors import ConfigError |
| 11 | +from sqlmesh.utils.yaml import YAML |
8 | 12 |
|
9 | 13 | pytestmark = pytest.mark.dbt |
10 | 14 |
|
@@ -44,3 +48,89 @@ def test_model_test_circular_references() -> None: |
44 | 48 | upstream_model.check_for_circular_test_refs(context) |
45 | 49 | with pytest.raises(ConfigError, match="between tests"): |
46 | 50 | downstream_model.check_for_circular_test_refs(context) |
| 51 | + |
| 52 | + |
| 53 | +def test_load_invalid_ref_audit_constraints(tmp_path: Path, caplog) -> None: |
| 54 | + yaml = YAML() |
| 55 | + dbt_project_dir = tmp_path / "dbt" |
| 56 | + dbt_project_dir.mkdir() |
| 57 | + dbt_model_dir = dbt_project_dir / "models" |
| 58 | + dbt_model_dir.mkdir() |
| 59 | + full_model_contents = "SELECT 1 as cola" |
| 60 | + full_model_file = dbt_model_dir / "full_model.sql" |
| 61 | + with open(full_model_file, "w", encoding="utf-8") as f: |
| 62 | + f.write(full_model_contents) |
| 63 | + model_schema = { |
| 64 | + "version": 2, |
| 65 | + "models": [ |
| 66 | + { |
| 67 | + "name": "full_model", |
| 68 | + "description": "A full model bad ref for audit and constraints", |
| 69 | + "columns": [ |
| 70 | + { |
| 71 | + "name": "cola", |
| 72 | + "description": "A column that is used in a ref audit and constraints", |
| 73 | + "constraints": [ |
| 74 | + { |
| 75 | + "type": "primary_key", |
| 76 | + "columns": ["cola"], |
| 77 | + "expression": "ref('not_real_model') (cola)", |
| 78 | + } |
| 79 | + ], |
| 80 | + "tests": [ |
| 81 | + { |
| 82 | + "relationships": { |
| 83 | + "to": "ref('not_real_model')", |
| 84 | + "field": "cola", |
| 85 | + "description": "A test that references a model that does not exist", |
| 86 | + } |
| 87 | + } |
| 88 | + ], |
| 89 | + } |
| 90 | + ], |
| 91 | + } |
| 92 | + ], |
| 93 | + } |
| 94 | + model_schema_file = dbt_model_dir / "schema.yml" |
| 95 | + with open(model_schema_file, "w", encoding="utf-8") as f: |
| 96 | + yaml.dump(model_schema, f) |
| 97 | + dbt_project_config = { |
| 98 | + "name": "invalid_ref_audit_constraints", |
| 99 | + "version": "1.0.0", |
| 100 | + "config-version": 2, |
| 101 | + "profile": "test", |
| 102 | + "model-paths": ["models"], |
| 103 | + } |
| 104 | + dbt_project_file = dbt_project_dir / "dbt_project.yml" |
| 105 | + with open(dbt_project_file, "w", encoding="utf-8") as f: |
| 106 | + yaml.dump(dbt_project_config, f) |
| 107 | + sqlmesh_config = { |
| 108 | + "model_defaults": { |
| 109 | + "start": "2025-01-01", |
| 110 | + } |
| 111 | + } |
| 112 | + sqlmesh_config_file = dbt_project_dir / "sqlmesh.yaml" |
| 113 | + with open(sqlmesh_config_file, "w", encoding="utf-8") as f: |
| 114 | + yaml.dump(sqlmesh_config, f) |
| 115 | + dbt_data_dir = tmp_path / "dbt_data" |
| 116 | + dbt_data_dir.mkdir() |
| 117 | + dbt_data_file = dbt_data_dir / "local.db" |
| 118 | + dbt_profile_config = { |
| 119 | + "test": { |
| 120 | + "outputs": {"duckdb": {"type": "duckdb", "path": str(dbt_data_file)}}, |
| 121 | + "target": "duckdb", |
| 122 | + } |
| 123 | + } |
| 124 | + db_profile_file = dbt_project_dir / "profiles.yml" |
| 125 | + with open(db_profile_file, "w", encoding="utf-8") as f: |
| 126 | + yaml.dump(dbt_profile_config, f) |
| 127 | + |
| 128 | + context = Context(paths=dbt_project_dir) |
| 129 | + assert ( |
| 130 | + "Skipping audit 'relationships_full_model_cola__cola__ref_not_real_model_' because model 'not_real_model' is not a valid ref" |
| 131 | + in caplog.text |
| 132 | + ) |
| 133 | + fqn = '"local"."main"."full_model"' |
| 134 | + assert fqn in context.snapshots |
| 135 | + # The audit isn't loaded due to the invalid ref |
| 136 | + assert context.snapshots[fqn].model.audits == [] |
0 commit comments