From 8591dd44ed5dd1911642329ac2173005253d6c57 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Fri, 19 Jul 2024 16:31:23 -0400 Subject: [PATCH] migrate test_jinja_static from unittest --- tests/unit/clients/test_jinja_static.py | 68 ++++++++++++++----------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/tests/unit/clients/test_jinja_static.py b/tests/unit/clients/test_jinja_static.py index d575cfb76e8..e09097c2026 100644 --- a/tests/unit/clients/test_jinja_static.py +++ b/tests/unit/clients/test_jinja_static.py @@ -1,44 +1,54 @@ -import unittest +import pytest from dbt.clients.jinja_static import statically_extract_macro_calls from dbt.context.base import generate_base_context -class MacroCalls(unittest.TestCase): - def setUp(self): - self.macro_strings = [ +@pytest.mark.parametrize( + "macro_string,expected_possible_macro_calls", + [ + ( "{% macro parent_macro() %} {% do return(nested_macro()) %} {% endmacro %}", - "{% macro lr_macro() %} {{ return(load_result('relations').table) }} {% endmacro %}", - "{% macro get_snapshot_unique_id() -%} {{ return(adapter.dispatch('get_snapshot_unique_id')()) }} {%- endmacro %}", - "{% macro get_columns_in_query(select_sql) -%} {{ return(adapter.dispatch('get_columns_in_query')(select_sql)) }} {% endmacro %}", - """{% macro test_mutually_exclusive_ranges(model) %} - with base as ( - select {{ get_snapshot_unique_id() }} as dbt_unique_id, - * - from {{ model }} ) - {% endmacro %}""", - "{% macro test_my_test(model) %} select {{ current_timestamp_backcompat() }} {% endmacro %}", - "{% macro some_test(model) -%} {{ return(adapter.dispatch('test_some_kind4', 'foo_utils4')) }} {%- endmacro %}", - "{% macro some_test(model) -%} {{ return(adapter.dispatch('test_some_kind5', macro_namespace = 'foo_utils5')) }} {%- endmacro %}", - ] - - self.possible_macro_calls = [ ["nested_macro"], + ), + ( + "{% macro lr_macro() %} {{ return(load_result('relations').table) }} {% endmacro %}", ["load_result"], + ), + ( + "{% macro get_snapshot_unique_id() -%} {{ return(adapter.dispatch('get_snapshot_unique_id')()) }} {%- endmacro %}", ["get_snapshot_unique_id"], + ), + ( + "{% macro get_columns_in_query(select_sql) -%} {{ return(adapter.dispatch('get_columns_in_query')(select_sql)) }} {% endmacro %}", ["get_columns_in_query"], + ), + ( + """{% macro test_mutually_exclusive_ranges(model) %} + with base as ( + select {{ get_snapshot_unique_id() }} as dbt_unique_id, + * + from {{ model }} ) + {% endmacro %}""", ["get_snapshot_unique_id"], + ), + ( + "{% macro test_my_test(model) %} select {{ current_timestamp_backcompat() }} {% endmacro %}", ["current_timestamp_backcompat"], + ), + ( + "{% macro some_test(model) -%} {{ return(adapter.dispatch('test_some_kind4', 'foo_utils4')) }} {%- endmacro %}", ["test_some_kind4", "foo_utils4.test_some_kind4"], + ), + ( + "{% macro some_test(model) -%} {{ return(adapter.dispatch('test_some_kind5', macro_namespace = 'foo_utils5')) }} {%- endmacro %}", ["test_some_kind5", "foo_utils5.test_some_kind5"], - ] - - def test_macro_calls(self): - cli_vars = {"local_utils_dispatch_list": ["foo_utils4"]} - ctx = generate_base_context(cli_vars) + ), + ], +) +def test_extract_macro_calls(self, macro_string, expected_possible_macro_calls): + cli_vars = {"local_utils_dispatch_list": ["foo_utils4"]} + ctx = generate_base_context(cli_vars) - index = 0 - for macro_string in self.macro_strings: - possible_macro_calls = statically_extract_macro_calls(macro_string, ctx) - self.assertEqual(self.possible_macro_calls[index], possible_macro_calls) - index += 1 + possible_macro_calls = statically_extract_macro_calls(macro_string, ctx) + assert possible_macro_calls == expected_possible_macro_calls