Skip to content

Commit a7feeb7

Browse files
authored
Fix: handle whitespace trimming in dbt test-to-macro jinja normalization (#5232)
1 parent ca2d067 commit a7feeb7

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

examples/sushi_dbt/models/schema.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ models:
2121
tests:
2222
- less_than_amount:
2323
amount: 1000
24+
- greater_than_amount:
25+
amount: 0
2426
- name: ds
2527
description: Date
2628
- name: top_waiters
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{%- test greater_than_amount(model, column_name, amount) -%}
2+
select *
3+
from {{ model }}
4+
where {{ column_name }} < {{ amount }}
5+
{%- endtest -%}

sqlmesh/dbt/manifest.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,17 @@ def _node_base_config(node: ManifestNode) -> t.Dict[str, t.Any]:
673673

674674

675675
def _convert_jinja_test_to_macro(test_jinja: str) -> str:
676-
TEST_TAG_REGEX = r"\s*{%\s*test\s+"
677-
ENDTEST_REGEX = r"{%\s*endtest\s*%}"
676+
TEST_TAG_REGEX = r"\s*{%-?\s*test\s+"
677+
ENDTEST_REGEX = r"{%-?\s*endtest\s*-?%}"
678+
678679
match = re.match(TEST_TAG_REGEX, test_jinja)
679680
if not match:
680681
# already a macro
681682
return test_jinja
682683

683-
macro = "{% macro test_" + test_jinja[match.span()[-1] :]
684-
return re.sub(ENDTEST_REGEX, "{% endmacro %}", macro)
684+
test_tag = test_jinja[: match.span()[-1]]
685+
686+
macro_tag = re.sub(r"({%-?\s*)test\s+", r"\1macro test_", test_tag)
687+
macro = macro_tag + test_jinja[match.span()[-1] :]
688+
689+
return re.sub(ENDTEST_REGEX, lambda m: m.group(0).replace("endtest", "endmacro"), macro)

tests/dbt/test_manifest.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlmesh.core.config import ModelDefaultsConfig
88
from sqlmesh.dbt.basemodel import Dependencies
99
from sqlmesh.dbt.context import DbtContext
10-
from sqlmesh.dbt.manifest import ManifestHelper
10+
from sqlmesh.dbt.manifest import ManifestHelper, _convert_jinja_test_to_macro
1111
from sqlmesh.dbt.profile import Profile
1212
from sqlmesh.dbt.builtin import Api, _relation_info_to_relation
1313
from sqlmesh.dbt.util import DBT_VERSION
@@ -257,3 +257,45 @@ def test_top_level_dbt_adapter_macros():
257257
customers_macros = helper.macros("customers")
258258
assert not customers_macros["default__current_engine"].info.is_top_level
259259
assert not customers_macros["duckdb__current_engine"].info.is_top_level
260+
261+
262+
def test_convert_jinja_test_to_macro():
263+
# Test block with whitespace trimming
264+
test_input = """{%- test assert_positive(model, column_name) -%}
265+
select * from {{ model }} where {{ column_name }} <= 0
266+
{%- endtest -%}"""
267+
268+
expected_output = """{%- macro test_assert_positive(model, column_name) -%}
269+
select * from {{ model }} where {{ column_name }} <= 0
270+
{%- endmacro -%}"""
271+
272+
assert _convert_jinja_test_to_macro(test_input) == expected_output
273+
274+
# Test block without whitespace trimming
275+
test_input_no_ws = """{% test assert_positive(model, column_name) %}
276+
select * from {{ model }} where {{ column_name }} <= 0
277+
{% endtest %}"""
278+
279+
expected_output_no_ws = """{% macro test_assert_positive(model, column_name) %}
280+
select * from {{ model }} where {{ column_name }} <= 0
281+
{% endmacro %}"""
282+
283+
assert _convert_jinja_test_to_macro(test_input_no_ws) == expected_output_no_ws
284+
285+
# Test block with mixed whitespace trimming
286+
test_input_mixed = """{%- test complex_test(model, column_name='id') %}
287+
select count(*) from {{ model }} where {{ column_name }} is null
288+
{% endtest -%}"""
289+
290+
expected_output_mixed = """{%- macro test_complex_test(model, column_name='id') %}
291+
select count(*) from {{ model }} where {{ column_name }} is null
292+
{% endmacro -%}"""
293+
294+
assert _convert_jinja_test_to_macro(test_input_mixed) == expected_output_mixed
295+
296+
# Test already converted macro (should return unchanged)
297+
macro_input = """{%- macro test_already_converted(model) -%}
298+
select * from {{ model }}
299+
{%- endmacro -%}"""
300+
301+
assert _convert_jinja_test_to_macro(macro_input) == macro_input

0 commit comments

Comments
 (0)