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..8fd8119a4fa --- /dev/null +++ b/tests/functional/unit_testing/test_ut_macros.py @@ -0,0 +1,81 @@ +import pytest + +from dbt.tests.util import run_dbt, 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): + # 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) 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)