Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Conversion for Query Tag Tests #235

Merged
merged 8 commits into from
Aug 23, 2022
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20220815-151111.yaml
Original file line number Diff line number Diff line change
@@ -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"
120 changes: 120 additions & 0 deletions tests/functional/query_tag/test_query_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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
}

@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', '{{"check_tag": "{}"}}'.format(self.prefix)])
McKnight-42 marked this conversation as resolved.
Show resolved Hide resolved

def test_snowflake_query_tag(self, project):
self.build_all_with_query_tags(project)
self.build_all_with_query_tags(project)




3 changes: 1 addition & 2 deletions tests/integration/query_tag_tests/test_query_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand All @@ -43,4 +43,3 @@ def test__snowflake__build_tagged_twice(self):
self.build_all_with_query_tags()
self.build_all_with_query_tags()