From c12f6fbf4db774356bd1472045ef4e1873969b08 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Fri, 9 Feb 2024 23:38:15 -0500 Subject: [PATCH] Fix: SemanticModel.same_model compares model attribute (#9549) --- .../unreleased/Fixes-20240209-170146.yaml | 6 +++ core/dbt/contracts/graph/nodes.py | 2 +- tests/functional/defer_state/fixtures.py | 38 +++++++++++++++++++ .../defer_state/test_modified_state.py | 22 +++++++++++ tests/unit/test_semantic_models.py | 15 +++++++- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixes-20240209-170146.yaml diff --git a/.changes/unreleased/Fixes-20240209-170146.yaml b/.changes/unreleased/Fixes-20240209-170146.yaml new file mode 100644 index 00000000000..5148219beda --- /dev/null +++ b/.changes/unreleased/Fixes-20240209-170146.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: only include unmodified semantic mdodels in state:modified selection +time: 2024-02-09T17:01:46.676097-05:00 +custom: + Author: michelleark + Issue: "9548" diff --git a/core/dbt/contracts/graph/nodes.py b/core/dbt/contracts/graph/nodes.py index 9153b41ce8a..cb032a953d8 100644 --- a/core/dbt/contracts/graph/nodes.py +++ b/core/dbt/contracts/graph/nodes.py @@ -1525,7 +1525,7 @@ def depends_on_macros(self): return self.depends_on.macros def same_model(self, old: "SemanticModel") -> bool: - return self.model == old.same_model + return self.model == old.model def same_node_relation(self, old: "SemanticModel") -> bool: return self.node_relation == old.node_relation diff --git a/tests/functional/defer_state/fixtures.py b/tests/functional/defer_state/fixtures.py index 8b1d3d35bb7..518cd4cbf19 100644 --- a/tests/functional/defer_state/fixtures.py +++ b/tests/functional/defer_state/fixtures.py @@ -360,6 +360,40 @@ {% endsnapshot %} """ + +semantic_model_schema_yml = """ +models: + - name: view_model + columns: + - name: id + data_tests: + - unique: + severity: error + - not_null + - name: name + +semantic_models: + - name: my_sm + model: ref('view_model') +""" + +modified_semantic_model_schema_yml = """ +models: + - name: view_model + columns: + - name: id + data_tests: + - unique: + severity: error + - not_null + - name: name + +semantic_models: + - name: my_sm + model: ref('view_model') + description: modified description +""" + model_1_sql = """ select * from {{ ref('seed') }} """ @@ -422,3 +456,7 @@ config: group: finance """ + +metricflow_time_spine_sql = """ +SELECT to_date('02/20/2023', 'mm/dd/yyyy') as date_day +""" diff --git a/tests/functional/defer_state/test_modified_state.py b/tests/functional/defer_state/test_modified_state.py index f4b6db1b257..40f5e1e31d1 100644 --- a/tests/functional/defer_state/test_modified_state.py +++ b/tests/functional/defer_state/test_modified_state.py @@ -38,6 +38,9 @@ table_model_now_view_sql, table_model_now_incremental_sql, view_model_now_table_sql, + metricflow_time_spine_sql, + semantic_model_schema_yml, + modified_semantic_model_schema_yml, ) @@ -993,3 +996,22 @@ def test_changed_access(self, project): results = run_dbt(["list", "-s", "state:modified", "--state", "./state"]) assert results == ["test.table_model.v1", "test.table_model.v2"] + + +class TestChangedSemanticModelContents(BaseModifiedState): + @pytest.fixture(scope="class") + def models(self): + return { + "view_model.sql": view_model_sql, + "schema.yml": semantic_model_schema_yml, + "metricflow_time_spine.sql": metricflow_time_spine_sql, + } + + def test_changed_semantic_model_contents(self, project): + self.run_and_save_state() + results = run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert len(results) == 0 + + write_file(modified_semantic_model_schema_yml, "models", "schema.yml") + results = run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert len(results) == 1 diff --git a/tests/unit/test_semantic_models.py b/tests/unit/test_semantic_models.py index 154c57d8585..7ba1bd9c6a1 100644 --- a/tests/unit/test_semantic_models.py +++ b/tests/unit/test_semantic_models.py @@ -1,5 +1,5 @@ +from copy import deepcopy import pytest - from typing import List from dbt.artifacts.resources import Dimension, Entity, Measure, Defaults @@ -79,3 +79,16 @@ def test_checked_agg_time_dimension_for_measure_exception(default_semantic_model f"Aggregation time dimension for measure {measure.name} on semantic model {default_semantic_model.name}" in str(execinfo.value) ) + + +def test_semantic_model_same_contents(default_semantic_model: SemanticModel): + default_semantic_model_copy = deepcopy(default_semantic_model) + + assert default_semantic_model.same_contents(default_semantic_model_copy) + + +def test_semantic_model_same_contents_update_model(default_semantic_model: SemanticModel): + default_semantic_model_copy = deepcopy(default_semantic_model) + default_semantic_model_copy.model = "ref('test_another_model')" + + assert not default_semantic_model.same_contents(default_semantic_model_copy)