|
| 1 | +import os |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
|
3 | 5 | from dbt.tests.adapter.basic.test_base import BaseSimpleMaterializations
|
|
9 | 11 | from dbt.tests.adapter.basic.test_singular_tests_ephemeral import BaseSingularTestsEphemeral
|
10 | 12 | from dbt.tests.adapter.basic.test_snapshot_check_cols import BaseSnapshotCheckCols
|
11 | 13 | from dbt.tests.adapter.basic.test_snapshot_timestamp import BaseSnapshotTimestamp
|
| 14 | +from dbt.tests.util import run_dbt |
12 | 15 |
|
13 | 16 |
|
14 | 17 | class TestSimpleMaterializations(BaseSimpleMaterializations):
|
15 |
| - pass |
| 18 | + |
| 19 | + def test_existing_view_materialization(self, project, models): |
| 20 | + """Test that materializing an existing view works correctly.""" |
| 21 | + # Create a temporary model file directly in the project |
| 22 | + model_path = os.path.join(project.project_root, "models", "view_model_exists.sql") |
| 23 | + |
| 24 | + # Write the initial model without the value column |
| 25 | + with open(model_path, "w") as f: |
| 26 | + f.write( |
| 27 | + """ |
| 28 | + {{ config(materialized='view') }} |
| 29 | + select |
| 30 | + 1 as id |
| 31 | + {% if var('include_value_column', false) %} |
| 32 | + , 2 as value |
| 33 | + {% endif %} |
| 34 | + """ |
| 35 | + ) |
| 36 | + |
| 37 | + # First run to create the view without the extra column |
| 38 | + results = run_dbt(["run", "-m", "view_model_exists"]) |
| 39 | + assert len(results) == 1 |
| 40 | + |
| 41 | + # Generate catalog to get column information |
| 42 | + catalog = run_dbt(["docs", "generate"]) |
| 43 | + |
| 44 | + # Check columns in the catalog |
| 45 | + node_id = "model.base.view_model_exists" |
| 46 | + assert node_id in catalog.nodes |
| 47 | + |
| 48 | + # Get columns from the catalog |
| 49 | + columns = catalog.nodes[node_id].columns |
| 50 | + column_names = [name.lower() for name in columns.keys()] |
| 51 | + |
| 52 | + # Verify only the id column exists |
| 53 | + assert "id" in column_names |
| 54 | + assert "value" not in column_names |
| 55 | + |
| 56 | + # Second run with a variable to include the extra column |
| 57 | + results = run_dbt( |
| 58 | + ["run", "-m", "view_model_exists", "--vars", '{"include_value_column": true}'] |
| 59 | + ) |
| 60 | + assert len(results) == 1 |
| 61 | + |
| 62 | + # Generate catalog again to get updated column information |
| 63 | + catalog = run_dbt(["docs", "generate"]) |
| 64 | + |
| 65 | + # Get updated columns from the catalog |
| 66 | + columns = catalog.nodes[node_id].columns |
| 67 | + column_names = [name.lower() for name in columns.keys()] |
| 68 | + |
| 69 | + # Verify both columns exist now |
| 70 | + assert "id" in column_names |
| 71 | + assert "value" in column_names |
16 | 72 |
|
17 | 73 |
|
18 | 74 | class TestSingularTests(BaseSingularTests):
|
|
0 commit comments