Skip to content

Commit

Permalink
migrate test_jinja_static from unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Jul 19, 2024
1 parent ff096d7 commit 8591dd4
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions tests/unit/clients/test_jinja_static.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8591dd4

Please sign in to comment.