@@ -3227,3 +3227,103 @@ def execute(
32273227 test .context .resolve_table ("silver.sushi.bar" )
32283228
32293229 _check_successful_or_raise (test .run ())
3230+
3231+
3232+ def test_python_model_test_variables_override (tmp_path : Path ) -> None :
3233+ py_model = tmp_path / "models" / "test_var_model.py"
3234+ py_model .parent .mkdir (parents = True , exist_ok = True )
3235+ py_model .write_text (
3236+ """
3237+ import pandas as pd # noqa: TID253
3238+ from sqlmesh import model, ExecutionContext
3239+ import typing as t
3240+
3241+ @model(
3242+ name="test_var_model",
3243+ columns={"id": "int", "flag_value": "boolean", "var_value": "varchar"},
3244+ )
3245+ def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame:
3246+ my_flag = context.var("my_flag")
3247+ other_var = context.var("other_var")
3248+
3249+ return pd.DataFrame([{
3250+ "id": 1 if my_flag else 2,
3251+ "flag_value": my_flag,
3252+ "var_value": other_var,
3253+ }])"""
3254+ )
3255+
3256+ config = Config (
3257+ model_defaults = ModelDefaultsConfig (dialect = "duckdb" ),
3258+ variables = {"my_flag" : False , "other_var" : "default_value" },
3259+ )
3260+ context = Context (config = config , paths = tmp_path )
3261+
3262+ python_model = context .models ['"test_var_model"' ]
3263+
3264+ # Test when Flag is True
3265+ # Overriding the config default flag_value to True
3266+ # AND the var_value to use test one
3267+ test_flag_true = _create_test (
3268+ body = load_yaml ("""
3269+ test_flag_true:
3270+ model: test_var_model
3271+ vars:
3272+ my_flag: true
3273+ other_var: "test_value"
3274+ outputs:
3275+ query:
3276+ rows:
3277+ - id: 1
3278+ flag_value: true
3279+ var_value: "test_value"
3280+ """ ),
3281+ test_name = "test_flag_true" ,
3282+ model = python_model ,
3283+ context = context ,
3284+ )
3285+
3286+ _check_successful_or_raise (test_flag_true .run ())
3287+
3288+ # Test when Flag is False
3289+ # Overriding the config default flag_value to False
3290+ # AND the var_value to use test one (since the above would be false for both)
3291+ test_flag_false = _create_test (
3292+ body = load_yaml ("""
3293+ test_flag_false:
3294+ model: test_var_model
3295+ vars:
3296+ my_flag: false
3297+ other_var: "another_test_value"
3298+ outputs:
3299+ query:
3300+ rows:
3301+ - id: 2
3302+ flag_value: false
3303+ var_value: "another_test_value"
3304+ """ ),
3305+ test_name = "test_flag_false" ,
3306+ model = python_model ,
3307+ context = context ,
3308+ )
3309+
3310+ _check_successful_or_raise (test_flag_false .run ())
3311+
3312+ # Test with no vars specified
3313+ # (should use config defaults for both flag and var_value)
3314+ test_default_vars = _create_test (
3315+ body = load_yaml ("""
3316+ test_default_vars:
3317+ model: test_var_model
3318+ outputs:
3319+ query:
3320+ rows:
3321+ - id: 2
3322+ flag_value: false
3323+ var_value: "default_value"
3324+ """ ),
3325+ test_name = "test_default_vars" ,
3326+ model = python_model ,
3327+ context = context ,
3328+ )
3329+ _check_successful_or_raise (test_default_vars .run ())
0 commit comments