From 91d55868a3e0f1ec4f041014e4c000f348d319a2 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Fri, 10 Jan 2025 15:17:32 -0500 Subject: [PATCH] Create test and fix by getting builder.config --- core/dbt/parser/generic_test_builders.py | 30 ----------------- core/dbt/parser/schema_generic_tests.py | 2 +- .../schema_tests/test_custom_test_config.py | 33 ++++++++++++++----- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/core/dbt/parser/generic_test_builders.py b/core/dbt/parser/generic_test_builders.py index 1c9478734bf..d6551884871 100644 --- a/core/dbt/parser/generic_test_builders.py +++ b/core/dbt/parser/generic_test_builders.py @@ -285,36 +285,6 @@ def database(self) -> Optional[str]: def schema(self) -> Optional[str]: return self.config.get("schema") - def get_static_config(self): - config = {} - if self.alias is not None: - config["alias"] = self.alias - if self.severity is not None: - config["severity"] = self.severity - if self.enabled is not None: - config["enabled"] = self.enabled - if self.where is not None: - config["where"] = self.where - if self.limit is not None: - config["limit"] = self.limit - if self.warn_if is not None: - config["warn_if"] = self.warn_if - if self.error_if is not None: - config["error_if"] = self.error_if - if self.fail_calc is not None: - config["fail_calc"] = self.fail_calc - if self.store_failures is not None: - config["store_failures"] = self.store_failures - if self.store_failures_as is not None: - config["store_failures_as"] = self.store_failures_as - if self.meta is not None: - config["meta"] = self.meta - if self.database is not None: - config["database"] = self.database - if self.schema is not None: - config["schema"] = self.schema - return config - def tags(self) -> List[str]: tags = self.config.get("tags", []) if isinstance(tags, str): diff --git a/core/dbt/parser/schema_generic_tests.py b/core/dbt/parser/schema_generic_tests.py index 58be6dc94be..5eaf4aa948e 100644 --- a/core/dbt/parser/schema_generic_tests.py +++ b/core/dbt/parser/schema_generic_tests.py @@ -286,7 +286,7 @@ def render_test_update(self, node, config, builder, schema_file_id): # to the context in rendering processing node.depends_on.add_macro(macro_unique_id) if macro_unique_id in ["macro.dbt.test_not_null", "macro.dbt.test_unique"]: - config_call_dict = builder.get_static_config() + config_call_dict = builder.config config._config_call_dict = config_call_dict # This sets the config from dbt_project self.update_parsed_node_config(node, config) diff --git a/tests/functional/schema_tests/test_custom_test_config.py b/tests/functional/schema_tests/test_custom_test_config.py index 820205f1d25..b5632cd7664 100644 --- a/tests/functional/schema_tests/test_custom_test_config.py +++ b/tests/functional/schema_tests/test_custom_test_config.py @@ -5,7 +5,7 @@ from dbt.contracts.graph.manifest import Manifest from dbt.contracts.graph.nodes import TestNode from dbt.exceptions import CompilationError -from dbt.tests.util import get_manifest, run_dbt +from dbt.tests.util import get_manifest, run_dbt, update_config_file custom_config_yml = """ models: @@ -23,6 +23,9 @@ test_color: orange store_failures: true unlogged: True + - not_null: + config: + not_null_key: abc """ mixed_config_yml = """ @@ -128,7 +131,7 @@ def models(self): def test_custom_config(self, project): run_dbt(["run"]) - run_dbt(["test", "--log-level", "debug"], expect_pass=False) + run_dbt(["test"], expect_pass=False) manifest = get_manifest(project.project_root) # Pattern to match the test_id without the specific suffix @@ -139,15 +142,29 @@ def test_custom_config(self, project): assert "custom_config_key" in test_node.config assert test_node.config["custom_config_key"] == "some_value" - pattern = re.compile(r"test\.test\.custom_color_from_config.*") - test_node = _select_test_node(manifest, pattern) - assert "test_color" in test_node.config - assert "unlogged" in test_node.config - + custom_color_pattern = re.compile(r"test\.test\.custom_color_from_config.*") + custom_color_test_node = _select_test_node(manifest, custom_color_pattern) + assert custom_color_test_node.config.get("test_color") == "orange" + assert custom_color_test_node.config.get("unlogged") is True persistence = get_table_persistence(project, "custom_color_from_config_table_color") - assert persistence == "u" + not_null_pattern = re.compile(r"test\.test\.not_null.*") + not_null_test_node = _select_test_node(manifest, not_null_pattern) + assert not_null_test_node.config.get("not_null_key") == "abc" + + # set dbt_project.yml config and ensure that schema configs override project configs + config_patch = { + "data_tests": {"test_color": "blue", "some_key": "strange", "not_null_key": "def"} + } + update_config_file(config_patch, project.project_root, "dbt_project.yml") + manifest = run_dbt(["parse"]) + custom_color_test_node = _select_test_node(manifest, custom_color_pattern) + assert custom_color_test_node.config.get("test_color") == "orange" + assert custom_color_test_node.config.get("some_key") == "strange" + not_null_test_node = _select_test_node(manifest, not_null_pattern) + assert not_null_test_node.config.get("not_null_key") == "abc" + class TestMixedDataTestConfig(BaseDataTestsConfig): @pytest.fixture(scope="class")