|
7 | 7 | from sqlmesh.core.config import ModelDefaultsConfig |
8 | 8 | from sqlmesh.dbt.basemodel import Dependencies |
9 | 9 | 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 |
11 | 11 | from sqlmesh.dbt.profile import Profile |
12 | 12 | from sqlmesh.dbt.builtin import Api, _relation_info_to_relation |
13 | 13 | from sqlmesh.dbt.util import DBT_VERSION |
@@ -257,3 +257,45 @@ def test_top_level_dbt_adapter_macros(): |
257 | 257 | customers_macros = helper.macros("customers") |
258 | 258 | assert not customers_macros["default__current_engine"].info.is_top_level |
259 | 259 | 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