@@ -631,6 +631,49 @@ def test_env_and_default_schema_normalization(mocker: MockerFixture):
631
631
assert list (context .fetchdf ('select c from "DEFAULT__DEV"."X"' )["c" ])[0 ] == 1
632
632
633
633
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
+
634
677
def test_clear_caches (tmp_path : pathlib .Path ):
635
678
models_dir = tmp_path / "models"
636
679
@@ -2497,7 +2540,7 @@ def test_plan_min_intervals(tmp_path: Path):
2497
2540
),
2498
2541
start '2020-01-01',
2499
2542
cron '@daily'
2500
- );
2543
+ );
2501
2544
2502
2545
select @start_ds as start_ds, @end_ds as end_ds, @start_dt as start_dt, @end_dt as end_dt;
2503
2546
""" )
@@ -2510,9 +2553,9 @@ def test_plan_min_intervals(tmp_path: Path):
2510
2553
),
2511
2554
start '2020-01-01',
2512
2555
cron '@weekly'
2513
- );
2556
+ );
2514
2557
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;
2516
2559
""" )
2517
2560
2518
2561
(tmp_path / "models" / "monthly_model.sql" ).write_text ("""
@@ -2523,9 +2566,9 @@ def test_plan_min_intervals(tmp_path: Path):
2523
2566
),
2524
2567
start '2020-01-01',
2525
2568
cron '@monthly'
2526
- );
2569
+ );
2527
2570
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;
2529
2572
""" )
2530
2573
2531
2574
(tmp_path / "models" / "ended_daily_model.sql" ).write_text ("""
@@ -2537,9 +2580,9 @@ def test_plan_min_intervals(tmp_path: Path):
2537
2580
start '2020-01-01',
2538
2581
end '2020-01-18',
2539
2582
cron '@daily'
2540
- );
2583
+ );
2541
2584
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;
2543
2586
""" )
2544
2587
2545
2588
context .load ()
@@ -2672,7 +2715,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
2672
2715
),
2673
2716
start '2020-01-01',
2674
2717
cron '@hourly'
2675
- );
2718
+ );
2676
2719
2677
2720
select @start_dt as start_dt, @end_dt as end_dt;
2678
2721
""" )
@@ -2681,11 +2724,11 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
2681
2724
MODEL (
2682
2725
name sqlmesh_example.two_hourly_model,
2683
2726
kind INCREMENTAL_BY_TIME_RANGE (
2684
- time_column start_dt
2727
+ time_column start_dt
2685
2728
),
2686
2729
start '2020-01-01',
2687
2730
cron '0 */2 * * *'
2688
- );
2731
+ );
2689
2732
2690
2733
select start_dt, end_dt from sqlmesh_example.hourly_model where start_dt between @start_dt and @end_dt;
2691
2734
""" )
@@ -2694,11 +2737,11 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
2694
2737
MODEL (
2695
2738
name sqlmesh_example.unrelated_monthly_model,
2696
2739
kind INCREMENTAL_BY_TIME_RANGE (
2697
- time_column start_dt
2740
+ time_column start_dt
2698
2741
),
2699
2742
start '2020-01-01',
2700
2743
cron '@monthly'
2701
- );
2744
+ );
2702
2745
2703
2746
select @start_dt as start_dt, @end_dt as end_dt;
2704
2747
""" )
@@ -2711,7 +2754,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
2711
2754
),
2712
2755
start '2020-01-01',
2713
2756
cron '@daily'
2714
- );
2757
+ );
2715
2758
2716
2759
select start_dt, end_dt from sqlmesh_example.hourly_model where start_dt between @start_dt and @end_dt;
2717
2760
""" )
@@ -2724,7 +2767,7 @@ def test_plan_min_intervals_adjusted_for_downstream(tmp_path: Path):
2724
2767
),
2725
2768
start '2020-01-01',
2726
2769
cron '@weekly'
2727
- );
2770
+ );
2728
2771
2729
2772
select start_dt, end_dt from sqlmesh_example.daily_model where start_dt between @start_dt and @end_dt;
2730
2773
""" )
0 commit comments