@@ -631,6 +631,49 @@ def test_env_and_default_schema_normalization(mocker: MockerFixture):
631631 assert list (context .fetchdf ('select c from "DEFAULT__DEV"."X"' )["c" ])[0 ] == 1
632632
633633
634+ def test_jinja_macro_undefined_variable_error (tmp_path : pathlib .Path ):
635+ models_dir = tmp_path / "models"
636+ models_dir .mkdir (parents = True )
637+ macros_dir = tmp_path / "macros"
638+ macros_dir .mkdir (parents = True )
639+
640+ macro_file = macros_dir / "my_macros.sql"
641+ macro_file .write_text ("""
642+ {%- macro generate_select(table_name) -%}
643+ {%- if target.name == 'production' -%}
644+ {%- set results = run_query('SELECT 1') -%}
645+ {%- endif -%}
646+ SELECT {{ results.columns[0].values()[0] }} FROM {{ table_name }}
647+ {%- endmacro -%}
648+ """ )
649+
650+ model_file = models_dir / "my_model.sql"
651+ model_file .write_text ("""
652+ MODEL (
653+ name my_schema.my_model,
654+ kind FULL
655+ );
656+
657+ JINJA_QUERY_BEGIN;
658+ {{ generate_select('users') }}
659+ JINJA_END;
660+ """ )
661+
662+ config_file = tmp_path / "config.yaml"
663+ config_file .write_text ("""
664+ model_defaults:
665+ dialect: duckdb
666+ """ )
667+
668+ with pytest .raises (ConfigError ) as exc_info :
669+ Context (paths = str (tmp_path ))
670+
671+ error_message = str (exc_info .value )
672+ assert "Failed to load model" in error_message
673+ assert "Could not render jinja for" in error_message
674+ assert "Undefined macro/variable: 'target' in macro: 'generate_select'" in error_message
675+
676+
634677def test_clear_caches (tmp_path : pathlib .Path ):
635678 models_dir = tmp_path / "models"
636679
@@ -2497,7 +2540,7 @@ def test_plan_min_intervals(tmp_path: Path):
24972540 ),
24982541 start '2020-01-01',
24992542 cron '@daily'
2500- );
2543+ );
25012544
25022545 select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
25032546 """ )
@@ -2510,9 +2553,9 @@ def test_plan_min_intervals(tmp_path: Path):
25102553 ),
25112554 start '2020-01-01',
25122555 cron '@weekly'
2513- );
2556+ );
25142557
2515- select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
2558+ select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
25162559 """ )
25172560
25182561 (tmp_path / "models" / "monthly_model.sql" ).write_text ("""
@@ -2523,9 +2566,9 @@ def test_plan_min_intervals(tmp_path: Path):
25232566 ),
25242567 start '2020-01-01',
25252568 cron '@monthly'
2526- );
2569+ );
25272570
2528- select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
2571+ select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
25292572 """ )
25302573
25312574 (tmp_path / "models" / "ended_daily_model.sql" ).write_text ("""
@@ -2537,9 +2580,9 @@ def test_plan_min_intervals(tmp_path: Path):
25372580 start '2020-01-01',
25382581 end '2020-01-18',
25392582 cron '@daily'
2540- );
2583+ );
25412584
2542- select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
2585+ select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
25432586 """ )
25442587
25452588 context .load ()
@@ -2672,7 +2715,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
26722715 ),
26732716 start '2020-01-01',
26742717 cron '@hourly'
2675- );
2718+ );
26762719
26772720 select @start_dt as start_dt, @end_dt as end_dt;
26782721 """ )
@@ -2681,11 +2724,11 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
26812724 MODEL (
26822725 name sqlmesh_example.two_hourly_model,
26832726 kind INCREMENTAL_BY_TIME_RANGE (
2684- time_column start_dt
2727+ time_column start_dt
26852728 ),
26862729 start '2020-01-01',
26872730 cron '0 */2 * * *'
2688- );
2731+ );
26892732
26902733 select start_dt, end_dt from sqlmesh_example.hourly_model where start_dt between @start_dt and @end_dt;
26912734 """ )
@@ -2694,11 +2737,11 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
26942737 MODEL (
26952738 name sqlmesh_example.unrelated_monthly_model,
26962739 kind INCREMENTAL_BY_TIME_RANGE (
2697- time_column start_dt
2740+ time_column start_dt
26982741 ),
26992742 start '2020-01-01',
27002743 cron '@monthly'
2701- );
2744+ );
27022745
27032746 select @start_dt as start_dt, @end_dt as end_dt;
27042747 """ )
@@ -2711,7 +2754,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
27112754 ),
27122755 start '2020-01-01',
27132756 cron '@daily'
2714- );
2757+ );
27152758
27162759 select start_dt, end_dt from sqlmesh_example.hourly_model where start_dt between @start_dt and @end_dt;
27172760 """ )
@@ -2724,7 +2767,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
27242767 ),
27252768 start '2020-01-01',
27262769 cron '@weekly'
2727- );
2770+ );
27282771
27292772 select start_dt, end_dt from sqlmesh_example.daily_model where start_dt between @start_dt and @end_dt;
27302773 """ )
0 commit comments