From 16cb498b560ccb2f4f691197281513cdfacfc25f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 14 Feb 2023 18:33:12 -0500 Subject: [PATCH] [Backport 1.4.latest] Set relation_name in tests at compile time (#6978) * Set relation_name in tests at compile time (#6949) (cherry picked from commit 480e0e55c56b2db7e76e8af39897c471d6239d1d) * Tweak artifact test --------- Co-authored-by: Gerda Shank --- .../unreleased/Fixes-20230210-194157.yaml | 6 +++ core/dbt/compilation.py | 12 +++++ .../artifacts/test_artifact_fields.py | 48 +++++++++++++++++++ tests/functional/artifacts/test_artifacts.py | 1 + 4 files changed, 67 insertions(+) create mode 100644 .changes/unreleased/Fixes-20230210-194157.yaml create mode 100644 tests/functional/artifacts/test_artifact_fields.py diff --git a/.changes/unreleased/Fixes-20230210-194157.yaml b/.changes/unreleased/Fixes-20230210-194157.yaml new file mode 100644 index 00000000000..b9ec101687a --- /dev/null +++ b/.changes/unreleased/Fixes-20230210-194157.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Set relation_name in test nodes at compile time +time: 2023-02-10T19:41:57.386766-05:00 +custom: + Author: gshank + Issue: "6930" diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py index 19e603b6312..37761207d48 100644 --- a/core/dbt/compilation.py +++ b/core/dbt/compilation.py @@ -378,6 +378,18 @@ def _compile_node( node, ) + # relation_name is set at parse time, except for tests without store_failures, + # but cli param can turn on store_failures, so we set here. + if ( + node.resource_type == NodeType.Test + and node.relation_name is None + and node.is_relational + ): + adapter = get_adapter(self.config) + relation_cls = adapter.Relation + relation_name = str(relation_cls.create_from(self.config, node)) + node.relation_name = relation_name + node.compiled = True return node diff --git a/tests/functional/artifacts/test_artifact_fields.py b/tests/functional/artifacts/test_artifact_fields.py new file mode 100644 index 00000000000..9276f545c8d --- /dev/null +++ b/tests/functional/artifacts/test_artifact_fields.py @@ -0,0 +1,48 @@ +import pytest +from dbt.tests.util import run_dbt, get_manifest, get_artifact + +# This is a place to put specific tests for contents of artifacts that we +# don't want to bother putting in the big artifact output test, which is +# hard to update. + +my_model_sql = "select 1 as fun" + +schema_yml = """ +version: 2 +models: + - name: my_model + columns: + - name: fun + tests: + - not_null +""" + + +class TestRelationNameInTests: + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_model_sql, + "schema.yml": schema_yml, + } + + def test_relation_name_in_tests(self, project): + results = run_dbt(["run"]) + assert len(results) == 1 + manifest = get_manifest(project.project_root) + test_id = "test.test.not_null_my_model_fun.bf3b032a01" + assert test_id in manifest.nodes + assert manifest.nodes[test_id].relation_name is None + + results = run_dbt(["test", "--store-failures"]) + assert len(results) == 1 + # The relation_name for tests with previously generated manifest and + # store_failures passed in on the command line, will be in the manifest.json + # but not in the parsed manifest. + manifest = get_manifest(project.project_root) + assert manifest.nodes[test_id].relation_name is None + manifest_json = get_artifact(project.project_root, "target", "manifest.json") + assert test_id in manifest_json["nodes"] + relation_name = manifest_json["nodes"][test_id]["relation_name"] + assert relation_name + assert '"not_null_my_model_fun"' in relation_name diff --git a/tests/functional/artifacts/test_artifacts.py b/tests/functional/artifacts/test_artifacts.py index 3f6c1304534..9e374f539f4 100644 --- a/tests/functional/artifacts/test_artifacts.py +++ b/tests/functional/artifacts/test_artifacts.py @@ -428,6 +428,7 @@ def verify_run_results(project, expected_run_results, start_time, run_results_sc class BaseVerifyProject: @pytest.fixture(scope="class", autouse=True) def setup(self, project): + dbt.flags.STORE_FAILURES = False alternate_schema_name = project.test_schema + "_test" project.create_test_schema(schema_name=alternate_schema_name) os.environ["DBT_ENV_CUSTOM_ENV_env_key"] = "env_value"