From d973d2580f916438bc92d4b04f7fd798a38cfb60 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Mon, 15 Aug 2022 15:08:14 -0500 Subject: [PATCH 1/7] init push for conversion of query_tag_tests to new python format --- tests/functional/query_tag/test_query_tags.py | 96 +++++++++++++++++++ .../query_tag_tests/test_query_tags.py | 3 +- 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 tests/functional/query_tag/test_query_tags.py diff --git a/tests/functional/query_tag/test_query_tags.py b/tests/functional/query_tag/test_query_tags.py new file mode 100644 index 000000000..606d9fed4 --- /dev/null +++ b/tests/functional/query_tag/test_query_tags.py @@ -0,0 +1,96 @@ +import pytest +from dbt.tests.util import run_dbt +import os + +snapshots__snapshot_query_tag_sql = """ +{% snapshot snapshot_query_tag %} + {{ + config( + target_database=database, + target_schema=schema, + unique_key='id', + strategy='check', + check_cols=['color'], + ) + }} + select 1 as id, 'blue' as color +{% endsnapshot %} + +""" + +models__table_model_query_tag_sql = """ +{{ config(materialized = 'table') }} + +select 1 as id + +""" + +models__view_model_query_tag_sql = """ +{{ config(materialized = 'view') }} + +select 1 as id + +""" + +models__incremental_model_query_tag_sql = """ +{{ config(materialized = 'incremental', unique_key = 'id') }} + +select 1 as id + +""" + +macros__check_tag_sql = """ +{% macro check_query_tag() %} + + {% if execute %} + {% set query_tag = get_current_query_tag() %} + {% if query_tag != var("query_tag") %} + {{ exceptions.raise_compiler_error("Query tag not used!") }} + {% endif %} + {% endif %} + +{% endmacro %} + +""" + +seeds__seed_query_tag_csv = """id +1 +""" + +class TestQueryTag: + @pytest.fixture(scope="class") + def models(self): + return { + "table_model_query_tag.sql": models__table_model_query_tag_sql, + "view_model_query_tag.sql": models__view_model_query_tag_sql, + "incremental_model_query_tag.sql": models__incremental_model_query_tag_sql + } + + @pytest.fixture(scope="class") + def snapshots(self): + return { + "snapshot_query_tag.sql": snapshots__snapshot_query_tag_sql + } + + @pytest.fixture(scope="class") + def macros(self): + return { + "check_tag.sql": macros__check_tag_sql + } + + @pytest.fixture(scope="class") + def seeds(self): + return { + "seed_query_tag.csv": seeds__seed_query_tag_csv + } + + def build_all_with_query_tags(self, project): + run_dbt(['build', '--vars', '{{"query_tag": "{}"}}'.format(self.prefix)]) + + def test_snowflake_query_tag(self, project): + self.build_all_with_query_tags(project) + self.build_all_with_query_tags(project) + + + + diff --git a/tests/integration/query_tag_tests/test_query_tags.py b/tests/integration/query_tag_tests/test_query_tags.py index 24cc7c52a..9c1e9b771 100644 --- a/tests/integration/query_tag_tests/test_query_tags.py +++ b/tests/integration/query_tag_tests/test_query_tags.py @@ -34,7 +34,7 @@ def project_config(self): }, }, } - + def build_all_with_query_tags(self): self.run_dbt(['build', '--vars', '{{"query_tag": "{}"}}'.format(self.prefix)]) @@ -43,4 +43,3 @@ def test__snowflake__build_tagged_twice(self): self.build_all_with_query_tags() self.build_all_with_query_tags() - \ No newline at end of file From fa1670c9735ae6696f1e7af0df38af69673d0322 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Mon, 15 Aug 2022 15:11:25 -0500 Subject: [PATCH 2/7] add changelog --- .changes/unreleased/Under the Hood-20220815-151111.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/unreleased/Under the Hood-20220815-151111.yaml diff --git a/.changes/unreleased/Under the Hood-20220815-151111.yaml b/.changes/unreleased/Under the Hood-20220815-151111.yaml new file mode 100644 index 000000000..e1dd737dd --- /dev/null +++ b/.changes/unreleased/Under the Hood-20220815-151111.yaml @@ -0,0 +1,7 @@ +kind: Under the Hood +body: Pytest conversion of query_tag_tests to new format +time: 2022-08-15T15:11:11.606012-05:00 +custom: + Author: mcknight-42 + Issue: "216" + PR: "235" From e02671ca460e31132225561a6e7bf95608ba3047 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Mon, 15 Aug 2022 15:51:48 -0500 Subject: [PATCH 3/7] working on formatting --- tests/functional/query_tag/test_query_tags.py | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/functional/query_tag/test_query_tags.py b/tests/functional/query_tag/test_query_tags.py index 606d9fed4..edacc066e 100644 --- a/tests/functional/query_tag/test_query_tags.py +++ b/tests/functional/query_tag/test_query_tags.py @@ -84,8 +84,32 @@ def seeds(self): "seed_query_tag.csv": seeds__seed_query_tag_csv } + @pytest.fixture(scope="class") + def project_config_update(self): + return { + 'config-version': 2, + 'models': { + 'tests': { + 'query_tag': self.prefix, + 'post-hook': '{{ check_tag() }}' + }, + }, + 'seeds': { + 'tests': { + 'query_tag': self.prefix, + 'post-hook': '{{ check_tag() }}' + }, + }, + 'snapshots': { + 'tests': { + 'query_tag': self.prefix, + 'post-hook': '{{ check_tag() }}' + }, + }, + } + def build_all_with_query_tags(self, project): - run_dbt(['build', '--vars', '{{"query_tag": "{}"}}'.format(self.prefix)]) + run_dbt(['build', '--vars', '{{"check_tag": "{}"}}'.format(self.prefix)]) def test_snowflake_query_tag(self, project): self.build_all_with_query_tags(project) From 476e07b6760761ef2e302c8f4e4319d01fdd164a Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Tue, 16 Aug 2022 10:30:17 -0500 Subject: [PATCH 4/7] require prefix fixture from project.py passing on views, failing on incremental and tables currently during build --- tests/functional/query_tag/test_query_tags.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/functional/query_tag/test_query_tags.py b/tests/functional/query_tag/test_query_tags.py index edacc066e..8419c32ca 100644 --- a/tests/functional/query_tag/test_query_tags.py +++ b/tests/functional/query_tag/test_query_tags.py @@ -85,35 +85,35 @@ def seeds(self): } @pytest.fixture(scope="class") - def project_config_update(self): + def project_config_update(self, prefix): return { 'config-version': 2, 'models': { 'tests': { - 'query_tag': self.prefix, + 'query_tag': prefix, 'post-hook': '{{ check_tag() }}' }, }, 'seeds': { 'tests': { - 'query_tag': self.prefix, + 'query_tag': prefix, 'post-hook': '{{ check_tag() }}' }, }, 'snapshots': { 'tests': { - 'query_tag': self.prefix, + 'query_tag': prefix, 'post-hook': '{{ check_tag() }}' }, }, } - def build_all_with_query_tags(self, project): - run_dbt(['build', '--vars', '{{"check_tag": "{}"}}'.format(self.prefix)]) + def build_all_with_query_tags(self, project, prefix): + run_dbt(['build', '--vars', '{{"check_tag": "{}"}}'.format(prefix)]) - def test_snowflake_query_tag(self, project): - self.build_all_with_query_tags(project) - self.build_all_with_query_tags(project) + def test_snowflake_query_tag(self, project, prefix): + self.build_all_with_query_tags(project, prefix) + self.build_all_with_query_tags(project, prefix) From 1cadd982589e52f8a7863114f09e986f72c5cfd5 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Mon, 22 Aug 2022 12:27:40 -0500 Subject: [PATCH 5/7] updating based on changes from pr 210 to adding query tags to tests --- tests/functional/query_tag/test_query_tags.py | 21 +++++++- .../query_tag_tests/macros/check_tag.sql | 10 ---- .../models/incremental_model_query_tag.sql | 3 -- .../query_tag_tests/models/models.yml | 8 --- .../models/table_model_query_tag.sql | 3 -- .../models/view_model_query_tag.sql | 3 -- .../query_tag_tests/seeds/seed_query_tag.csv | 2 - .../snapshots/snapshot_query_tag.sql | 12 ----- .../query_tag_tests/test_query_tags.py | 51 ------------------- 9 files changed, 20 insertions(+), 93 deletions(-) delete mode 100644 tests/integration/query_tag_tests/macros/check_tag.sql delete mode 100644 tests/integration/query_tag_tests/models/incremental_model_query_tag.sql delete mode 100644 tests/integration/query_tag_tests/models/models.yml delete mode 100644 tests/integration/query_tag_tests/models/table_model_query_tag.sql delete mode 100644 tests/integration/query_tag_tests/models/view_model_query_tag.sql delete mode 100644 tests/integration/query_tag_tests/seeds/seed_query_tag.csv delete mode 100644 tests/integration/query_tag_tests/snapshots/snapshot_query_tag.sql delete mode 100644 tests/integration/query_tag_tests/test_query_tags.py diff --git a/tests/functional/query_tag/test_query_tags.py b/tests/functional/query_tag/test_query_tags.py index 8419c32ca..50ee18b90 100644 --- a/tests/functional/query_tag/test_query_tags.py +++ b/tests/functional/query_tag/test_query_tags.py @@ -25,6 +25,18 @@ """ +models__models_config_yml = """ +version: 2 + +models: + - name: view_model_query_tag + columns: + - name: id + tests: + - unique + +""" + models__view_model_query_tag_sql = """ {{ config(materialized = 'view') }} @@ -63,7 +75,8 @@ def models(self): return { "table_model_query_tag.sql": models__table_model_query_tag_sql, "view_model_query_tag.sql": models__view_model_query_tag_sql, - "incremental_model_query_tag.sql": models__incremental_model_query_tag_sql + "incremental_model_query_tag.sql": models__incremental_model_query_tag_sql, + "models_config.yml": models__models_config_yml, } @pytest.fixture(scope="class") @@ -106,6 +119,12 @@ def project_config_update(self, prefix): 'post-hook': '{{ check_tag() }}' }, }, + 'tests': { + 'test': { + 'query_tag': prefix, + 'post-hook': '{{ check_query_tag() }}' + } + }, } def build_all_with_query_tags(self, project, prefix): diff --git a/tests/integration/query_tag_tests/macros/check_tag.sql b/tests/integration/query_tag_tests/macros/check_tag.sql deleted file mode 100644 index 3cf3232f1..000000000 --- a/tests/integration/query_tag_tests/macros/check_tag.sql +++ /dev/null @@ -1,10 +0,0 @@ -{% macro check_query_tag() %} - - {% if execute %} - {% set query_tag = get_current_query_tag() %} - {% if query_tag != var("query_tag") %} - {{ exceptions.raise_compiler_error("Query tag not used!") }} - {% endif %} - {% endif %} - -{% endmacro %} diff --git a/tests/integration/query_tag_tests/models/incremental_model_query_tag.sql b/tests/integration/query_tag_tests/models/incremental_model_query_tag.sql deleted file mode 100644 index 9f8ef50a9..000000000 --- a/tests/integration/query_tag_tests/models/incremental_model_query_tag.sql +++ /dev/null @@ -1,3 +0,0 @@ -{{ config(materialized = 'incremental', unique_key = 'id') }} - -select 1 as id diff --git a/tests/integration/query_tag_tests/models/models.yml b/tests/integration/query_tag_tests/models/models.yml deleted file mode 100644 index 49ab57765..000000000 --- a/tests/integration/query_tag_tests/models/models.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: 2 - -models: - - name: view_model_query_tag - columns: - - name: id - tests: - - unique \ No newline at end of file diff --git a/tests/integration/query_tag_tests/models/table_model_query_tag.sql b/tests/integration/query_tag_tests/models/table_model_query_tag.sql deleted file mode 100644 index 7f2d6b752..000000000 --- a/tests/integration/query_tag_tests/models/table_model_query_tag.sql +++ /dev/null @@ -1,3 +0,0 @@ -{{ config(materialized = 'table') }} - -select 1 as id diff --git a/tests/integration/query_tag_tests/models/view_model_query_tag.sql b/tests/integration/query_tag_tests/models/view_model_query_tag.sql deleted file mode 100644 index 21ccea8dd..000000000 --- a/tests/integration/query_tag_tests/models/view_model_query_tag.sql +++ /dev/null @@ -1,3 +0,0 @@ -{{ config(materialized = 'view') }} - -select 1 as id diff --git a/tests/integration/query_tag_tests/seeds/seed_query_tag.csv b/tests/integration/query_tag_tests/seeds/seed_query_tag.csv deleted file mode 100644 index 3ff3deb87..000000000 --- a/tests/integration/query_tag_tests/seeds/seed_query_tag.csv +++ /dev/null @@ -1,2 +0,0 @@ -id -1 diff --git a/tests/integration/query_tag_tests/snapshots/snapshot_query_tag.sql b/tests/integration/query_tag_tests/snapshots/snapshot_query_tag.sql deleted file mode 100644 index 5af9ca777..000000000 --- a/tests/integration/query_tag_tests/snapshots/snapshot_query_tag.sql +++ /dev/null @@ -1,12 +0,0 @@ -{% snapshot snapshot_query_tag %} - {{ - config( - target_database=database, - target_schema=schema, - unique_key='id', - strategy='check', - check_cols=['color'], - ) - }} - select 1 as id, 'blue' as color -{% endsnapshot %} diff --git a/tests/integration/query_tag_tests/test_query_tags.py b/tests/integration/query_tag_tests/test_query_tags.py deleted file mode 100644 index 6512ced5c..000000000 --- a/tests/integration/query_tag_tests/test_query_tags.py +++ /dev/null @@ -1,51 +0,0 @@ -import os -import csv -from tests.integration.base import DBTIntegrationTest, use_profile - -class TestSeedWithQueryTag(DBTIntegrationTest): - @property - def schema(self): - return "query_tag" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - 'models': { - 'test': { - 'query_tag': self.prefix, - 'post-hook': '{{ check_query_tag() }}' - }, - }, - 'seeds': { - 'test': { - 'query_tag': self.prefix, - 'post-hook': '{{ check_query_tag() }}' - }, - }, - 'snapshots': { - 'test': { - 'query_tag': self.prefix, - 'post-hook': '{{ check_query_tag() }}' - }, - }, - 'tests': { - 'test': { - 'query_tag': self.prefix, - 'post-hook': '{{ check_query_tag() }}' - } - }, - } - - def build_all_with_query_tags(self): - self.run_dbt(['build', '--vars', '{{"query_tag": "{}"}}'.format(self.prefix)]) - - @use_profile('snowflake') - def test__snowflake__build_tagged_twice(self): - self.build_all_with_query_tags() - self.build_all_with_query_tags() - From fe964ec8bf37e5b4fa9de24ba6d7840d85194d6f Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Tue, 23 Aug 2022 14:56:46 -0500 Subject: [PATCH 6/7] line fix and remove changelog entry --- tests/functional/query_tag/test_query_tags.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/functional/query_tag/test_query_tags.py b/tests/functional/query_tag/test_query_tags.py index 50ee18b90..b625c1e7b 100644 --- a/tests/functional/query_tag/test_query_tags.py +++ b/tests/functional/query_tag/test_query_tags.py @@ -133,7 +133,3 @@ def build_all_with_query_tags(self, project, prefix): def test_snowflake_query_tag(self, project, prefix): self.build_all_with_query_tags(project, prefix) self.build_all_with_query_tags(project, prefix) - - - - From 2b076665f3b75447b806b98b61e3d84daddc7fae Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Tue, 23 Aug 2022 14:58:46 -0500 Subject: [PATCH 7/7] line fix and remove changelog entry --- .changes/unreleased/Under the Hood-20220815-151111.yaml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .changes/unreleased/Under the Hood-20220815-151111.yaml diff --git a/.changes/unreleased/Under the Hood-20220815-151111.yaml b/.changes/unreleased/Under the Hood-20220815-151111.yaml deleted file mode 100644 index e1dd737dd..000000000 --- a/.changes/unreleased/Under the Hood-20220815-151111.yaml +++ /dev/null @@ -1,7 +0,0 @@ -kind: Under the Hood -body: Pytest conversion of query_tag_tests to new format -time: 2022-08-15T15:11:11.606012-05:00 -custom: - Author: mcknight-42 - Issue: "216" - PR: "235"