Skip to content

Commit

Permalink
test advanced ref override (#8552)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk authored Sep 6, 2023
1 parent d66fe21 commit ebeb0f1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230906-110249.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Add functional test for advanced ref override
time: 2023-09-06T11:02:49.253486-04:00
custom:
Author: michelleark
Issue: "8566"
131 changes: 99 additions & 32 deletions tests/functional/ref_override/test_ref_override.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from dbt.tests.util import run_dbt, check_relations_equal
from dbt.tests.fixtures.project import write_project_files


models__ref_override_sql = """
Expand All @@ -28,52 +27,120 @@
3,6"""


@pytest.fixture(scope="class")
def models():
return {"ref_override.sql": models__ref_override_sql}
class TestRefOverride:
@pytest.fixture(scope="class")
def models(self):
return {"ref_override.sql": models__ref_override_sql}

@pytest.fixture(scope="class")
def macros(self):
return {"ref_override_macro.sql": macros__ref_override_macro_sql}

@pytest.fixture(scope="class")
def macros():
return {"ref_override_macro.sql": macros__ref_override_macro_sql}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_2.csv": seeds__seed_2_csv, "seed_1.csv": seeds__seed_1_csv}

def test_ref_override(
self,
project,
):
run_dbt(["seed"])
run_dbt(["run"])

@pytest.fixture(scope="class")
def seeds():
return {"seed_2.csv": seeds__seed_2_csv, "seed_1.csv": seeds__seed_1_csv}
# We want it to equal seed_2 and not seed_1. If it's
# still pointing at seed_1 then the override hasn't worked.
check_relations_equal(project.adapter, ["ref_override", "seed_2"])


@pytest.fixture(scope="class")
def project_files(
project_root,
models,
macros,
seeds,
):
write_project_files(project_root, "models", models)
write_project_files(project_root, "macros", macros)
write_project_files(project_root, "seeds", seeds)
models__version_ref_override_sql = """
select
*
from {{ ref('versioned_model', version=1) }}
"""

models__package_ref_override_sql = """
select
*
from {{ ref('package', 'versioned_model') }}
"""

class TestRefOverride:
models__package_version_ref_override_sql = """
select
*
from {{ ref('package', 'versioned_model', version=1) }}
"""

models__v1_sql = """
select 1
"""

models__v2_sql = """
select 2
"""

schema__versions_yml = """
models:
- name: versioned_model
versions:
- v: 1
- v: 2
"""

macros__package_version_ref_override_macro_sql = """
-- Macro to override ref and always return the same result
{% macro ref() %}
-- extract user-provided positional and keyword arguments
{% set version = kwargs.get('version') %}
{% set packagename = none %}
{%- if (varargs | length) == 1 -%}
{% set modelname = varargs[0] %}
{%- else -%}
{% set packagename = varargs[0] %}
{% set modelname = varargs[1] %}
{% endif %}
{%- set version_override = 2 -%}
{%- set packagename_override = 'test' -%}
-- call builtins.ref based on provided positional arguments
{% if packagename is not none %}
{% do return(builtins.ref(packagename_override, modelname, version=version_override)) %}
{% else %}
{% do return(builtins.ref(modelname, version=version_override)) %}
{% endif %}
{% endmacro %}
"""


class TestAdvancedRefOverride:
@pytest.fixture(scope="class")
def project_config_update(self):
def models(self):
return {
"config-version": 2,
"seed-paths": ["seeds"],
"macro-paths": ["macros"],
"seeds": {
"quote_columns": False,
},
"version_ref_override.sql": models__version_ref_override_sql,
"package_ref_override.sql": models__package_ref_override_sql,
"package_version_ref_override.sql": models__package_version_ref_override_sql,
"versioned_model_v1.sql": models__v1_sql,
"versioned_model_v2.sql": models__v2_sql,
"model.sql": models__v1_sql,
"schema.yml": schema__versions_yml,
}

@pytest.fixture(scope="class")
def macros(self):
return {"ref_override_macro.sql": macros__package_version_ref_override_macro_sql}

def test_ref_override(
self,
project,
):
run_dbt(["seed"])
run_dbt(["run"])

# We want it to equal seed_2 and not seed_1. If it's
# still pointing at seed_1 then the override hasn't worked.
check_relations_equal(project.adapter, ["ref_override", "seed_2"])
# We want versioned_ref_override to equal to versioned_model_v2, otherwise the
# ref override macro has not worked
check_relations_equal(project.adapter, ["version_ref_override", "versioned_model_v2"])

check_relations_equal(project.adapter, ["package_ref_override", "versioned_model_v2"])

check_relations_equal(
project.adapter, ["package_version_ref_override", "versioned_model_v2"]
)

0 comments on commit ebeb0f1

Please sign in to comment.