Skip to content

Commit

Permalink
Create test and fix by getting builder.config
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jan 10, 2025
1 parent e0d5859 commit 91d5586
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
30 changes: 0 additions & 30 deletions core/dbt/parser/generic_test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/parser/schema_generic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 25 additions & 8 deletions tests/functional/schema_tests/test_custom_test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -23,6 +23,9 @@
test_color: orange
store_failures: true
unlogged: True
- not_null:
config:
not_null_key: abc
"""

mixed_config_yml = """
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 91d5586

Please sign in to comment.