From 5cbba8d9efa1b9228015618b94c191da29f1b4f8 Mon Sep 17 00:00:00 2001 From: nathanskone Date: Sat, 11 Oct 2025 11:30:30 -0700 Subject: [PATCH 1/3] Adding unit test that reproduces macro composition failure in unit tests --- .../functional/unit_testing/test_ut_macros.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/functional/unit_testing/test_ut_macros.py diff --git a/tests/functional/unit_testing/test_ut_macros.py b/tests/functional/unit_testing/test_ut_macros.py new file mode 100644 index 00000000000..f65b031258d --- /dev/null +++ b/tests/functional/unit_testing/test_ut_macros.py @@ -0,0 +1,77 @@ +import pytest + +from dbt.tests.util import run_dbt_and_capture + +my_model_without_composition_sql = """ +{{ config(materialized='table') }} + +{% set one = macro_one() %} +{% set two = macro_two() %} + +select 1 as id +""" + +my_model_with_composition_sql = """ +{{ config(materialized='table') }} + +{% set one = macro_one() %} +{% set two = macro_two() %} +{% set one_plus_two = one + two %} + +select 1 as id +""" + +my_macro_sql = """ +{% macro macro_one() -%} + {{ return(1) }} +{%- endmacro %} + + +{% macro macro_two() -%} + {{ return(2) }} +{%- endmacro %} +""" + +my_unit_test_yml = """ +unit_tests: + - name: my_unit_test + model: my_model + given: [] + expect: + rows: + - {id: 1} +""" + + +class TestMacroWithoutComposition: + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_model_without_composition_sql, + "my_unit_test.yml": my_unit_test_yml, + } + + @pytest.fixture(scope="class") + def macros(self): + return {"my_macros.sql": my_macro_sql} + + def test_macro_in_unit_test(self, project): + # Test that a model without macro composition properly resolves macro names in unit tests + run_dbt_and_capture(["test"], expect_pass=True) + + +class TestMacroComposition: + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_model_with_composition_sql, + "my_unit_test.yml": my_unit_test_yml, + } + + @pytest.fixture(scope="class") + def macros(self): + return {"my_macros.sql": my_macro_sql} + + def test_macro_composition_in_unit_test(self, project): + # Test that a model with macro composition properly resolves macro names in unit tests + run_dbt_and_capture(["test"], expect_pass=True) From ca8456c0540f1765d0271287ef3bd086bece71a6 Mon Sep 17 00:00:00 2001 From: nathanskone Date: Sat, 11 Oct 2025 12:18:20 -0700 Subject: [PATCH 2/3] Verify model works outside of unit test --- tests/functional/unit_testing/test_ut_macros.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/functional/unit_testing/test_ut_macros.py b/tests/functional/unit_testing/test_ut_macros.py index f65b031258d..8fd8119a4fa 100644 --- a/tests/functional/unit_testing/test_ut_macros.py +++ b/tests/functional/unit_testing/test_ut_macros.py @@ -1,6 +1,6 @@ import pytest -from dbt.tests.util import run_dbt_and_capture +from dbt.tests.util import run_dbt, run_dbt_and_capture my_model_without_composition_sql = """ {{ config(materialized='table') }} @@ -73,5 +73,9 @@ def macros(self): return {"my_macros.sql": my_macro_sql} def test_macro_composition_in_unit_test(self, project): + # Verify model works fine outside of unit testing + results = run_dbt(["run"]) + assert len(results) == 1 + # Test that a model with macro composition properly resolves macro names in unit tests run_dbt_and_capture(["test"], expect_pass=True) From 4985ecd9416e487812881530bd770ddfcc1dc89f Mon Sep 17 00:00:00 2001 From: nathanskone Date: Sat, 11 Oct 2025 16:00:46 -0700 Subject: [PATCH 3/3] Adding tests for issue 10539 --- .../unit_testing/test_ut_variables.py | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/functional/unit_testing/test_ut_variables.py diff --git a/tests/functional/unit_testing/test_ut_variables.py b/tests/functional/unit_testing/test_ut_variables.py new file mode 100644 index 00000000000..f9fa2bed11e --- /dev/null +++ b/tests/functional/unit_testing/test_ut_variables.py @@ -0,0 +1,87 @@ +import pytest + +from dbt.tests.util import run_dbt, run_dbt_and_capture + +dbt_project_yml = """ +vars: + columns_list_one: + - column_a + - column_b + + columns_list_two: + - column_c +""" + +my_model_one_variable_sql = """ +{{ config(materialized='table') }} +-- {{ get_columns(include=var('columns_list_one'))}} +select 1 as id +""" + +my_model_two_variables_sql = """ +{{ config(materialized='table') }} +-- {{ get_columns(include=var('columns_list_one') + var('columns_list_two'))}} +select 1 as id +""" + +my_macro_sql = """ +{%- macro get_columns(include=[]) -%} + {%- for col in include -%} + {{ col }}{% if not loop.last %}, {% endif %} + {%- endfor -%} +{%- endmacro -%} +""" + +my_unit_test_yml = """ +unit_tests: + - name: my_unit_test + model: my_model + given: [] + expect: + rows: + - {id: 1} +""" + + +class TestUnitTestOneVariables: + @pytest.fixture(scope="class") + def project_config_update(self): + return dbt_project_yml + + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_model_one_variable_sql, + "my_unit_test.yml": my_unit_test_yml, + } + + @pytest.fixture(scope="class") + def macros(self): + return {"my_macros.sql": my_macro_sql} + + def test_one_variable_as_input_to_macro(self, project): + run_dbt_and_capture(["test"], expect_pass=True) + + +class TestUnitTestTwoVariables: + @pytest.fixture(scope="class") + def project_config_update(self): + return dbt_project_yml + + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_model_two_variables_sql, + "my_unit_test.yml": my_unit_test_yml, + } + + @pytest.fixture(scope="class") + def macros(self): + return {"my_macros.sql": my_macro_sql} + + def test_two_variables_as_input_to_macro(self, project): + # Verify model works fine outside of unit testing + results = run_dbt(["run"]) + assert len(results) == 1 + + run_dbt_and_capture(["test"], expect_pass=True)