From 05f80efea404c7756bdec4d44f8e97f488cf43d4 Mon Sep 17 00:00:00 2001 From: Dave Connors Date: Mon, 6 May 2024 21:27:25 -0500 Subject: [PATCH] add functional tests --- tests/functional/primary_keys/fixtures.py | 88 ++++++++++ .../primary_keys/test_primary_keys.py | 157 ++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 tests/functional/primary_keys/fixtures.py create mode 100644 tests/functional/primary_keys/test_primary_keys.py diff --git a/tests/functional/primary_keys/fixtures.py b/tests/functional/primary_keys/fixtures.py new file mode 100644 index 00000000000..888e9d65190 --- /dev/null +++ b/tests/functional/primary_keys/fixtures.py @@ -0,0 +1,88 @@ +simple_model_sql = """ +select 1 as id, 'blue' as color +""" + +simple_model_unique_test = """ +models: + - name: simple_model + columns: + - name: id + tests: + - unique +""" + +simple_model_disabled_unique_test = """ +models: + - name: simple_model + columns: + - name: id + tests: + - unique: + enabled: false + +""" + +simple_model_unique_not_null_tests = """ +models: + - name: simple_model + columns: + - name: id + tests: + - unique + - not_null +""" + +simple_model_unique_combo_of_columns = """ +models: + - name: simple_model + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: [id, color] +""" + +simple_model_constraints = """ +models: + - name: simple_model + config: + contract: + enforced: true + columns: + - name: id + data_type: int + constraints: + - type: not_null + - type: primary_key + - name: color + data_type: text +""" + +simple_model_two_versions_both_configured = """ +models: + - name: simple_model + latest_version: 1 + columns: + - name: id + tests: + - unique + - not_null + versions: + - v: 1 + - v: 2 +""" + +simple_model_two_versions_exclude_col = """ +models: + - name: simple_model + latest_version: 1 + columns: + - name: id + tests: + - unique + - not_null + versions: + - v: 1 + - v: 2 + columns: + - include: all + exclude: [id] +""" diff --git a/tests/functional/primary_keys/test_primary_keys.py b/tests/functional/primary_keys/test_primary_keys.py new file mode 100644 index 00000000000..866ff2967ce --- /dev/null +++ b/tests/functional/primary_keys/test_primary_keys.py @@ -0,0 +1,157 @@ +import pytest + +from dbt.tests.util import get_manifest, run_dbt +from tests.functional.primary_keys.fixtures import ( + simple_model_constraints, + simple_model_disabled_unique_test, + simple_model_sql, + simple_model_two_versions_both_configured, + simple_model_two_versions_exclude_col, + simple_model_unique_combo_of_columns, + simple_model_unique_not_null_tests, + simple_model_unique_test, +) + + +class TestSimpleModelNoYml: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + } + + def test_simple_model_no_yml(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == [] + + +class TestSimpleModelConstraints: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + "schema.yml": simple_model_constraints, + } + + def test_simple_model_constraints(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == ["id"] + + +class TestSimpleModelUniqueNotNullTests: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + "schema.yml": simple_model_unique_not_null_tests, + } + + def test_simple_model_unique_not_null_tests(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == ["id"] + + +class TestSimpleModelUniqueTests: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + "schema.yml": simple_model_unique_test, + } + + def test_simple_model_unique_test(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == ["id"] + + +class TestSimpleModelDisabledUniqueTests: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + "schema.yml": simple_model_disabled_unique_test, + } + + def test_simple_model_disabled_unique_test(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == ["id"] + + +class TestVersionedSimpleModel: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model_v1.sql": simple_model_sql, + "simple_model_v2.sql": simple_model_sql, + "schema.yml": simple_model_two_versions_both_configured, + } + + def test_versioned_simple_model(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node_v1 = manifest.nodes["model.test.simple_model.v1"] + node_v2 = manifest.nodes["model.test.simple_model.v2"] + assert node_v1.primary_key == ["id"] + assert node_v2.primary_key == ["id"] + + +class TestVersionedSimpleModelExcludeTests: + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model_v1.sql": simple_model_sql, + "simple_model_v2.sql": simple_model_sql, + "schema.yml": simple_model_two_versions_exclude_col, + } + + def test_versioned_simple_model_exclude_col(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node_v1 = manifest.nodes["model.test.simple_model.v1"] + node_v2 = manifest.nodes["model.test.simple_model.v2"] + assert node_v1.primary_key == ["id"] + assert node_v2.primary_key == [] + + +class TestSimpleModelCombinationOfColumns: + @pytest.fixture(scope="class") + def packages(self): + return { + "packages": [ + { + "git": "https://github.com/dbt-labs/dbt-utils.git", + "revision": "1.1.0", + }, + ] + } + + @pytest.fixture(scope="class") + def models(self): + return { + "simple_model.sql": simple_model_sql, + "schema.yml": simple_model_unique_combo_of_columns, + } + + def test_versioned_simple_combo_of_columns(self, project): + run_dbt(["deps"]) + run_dbt(["run"]) + manifest = get_manifest(project.project_root) + node = manifest.nodes["model.test.simple_model"] + assert node.primary_key == ["color", "id"]