Skip to content

Commit 98304fb

Browse files
committed
Chore: Add extra tests for the model test framework
1 parent 5dfdeca commit 98304fb

File tree

1 file changed

+183
-1
lines changed

1 file changed

+183
-1
lines changed

tests/core/test_test.py

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ def _create_model(
6969
meta: str = SUSHI_FOO_META,
7070
dialect: t.Optional[str] = None,
7171
default_catalog: t.Optional[str] = None,
72+
**kwargs: t.Any,
7273
) -> SqlModel:
7374
parsed_definition = parse(f"{meta};{query}", default_dialect=dialect)
7475
return t.cast(
7576
SqlModel,
76-
load_sql_based_model(parsed_definition, dialect=dialect, default_catalog=default_catalog),
77+
load_sql_based_model(
78+
parsed_definition, dialect=dialect, default_catalog=default_catalog, **kwargs
79+
),
7780
)
7881

7982

@@ -2814,3 +2817,182 @@ def test_test_generation_with_timestamp_nat(tmp_path: Path) -> None:
28142817
assert query_output[0]["ts_col"] == datetime.datetime(2024, 9, 20, 11, 30, 0, 123456)
28152818
assert query_output[1]["ts_col"] is None
28162819
assert query_output[2]["ts_col"] == datetime.datetime(2024, 9, 21, 15, 45, 0, 987654)
2820+
2821+
2822+
def test_parameterized_name_sql_model() -> None:
2823+
variables = {"table_catalog": "gold"}
2824+
model = _create_model(
2825+
"select 1 as id, 'foo' as name",
2826+
meta="""
2827+
MODEL (
2828+
name @{table_catalog}.sushi.foo,
2829+
kind FULL
2830+
)
2831+
""",
2832+
dialect="snowflake",
2833+
variables=variables,
2834+
)
2835+
assert model.fqn == '"GOLD"."SUSHI"."FOO"'
2836+
2837+
test = _create_test(
2838+
body=load_yaml(
2839+
"""
2840+
test_foo:
2841+
model: {{ var('table_catalog' ) }}.sushi.foo
2842+
outputs:
2843+
query:
2844+
- id: 1
2845+
name: foo
2846+
""",
2847+
variables=variables,
2848+
),
2849+
test_name="test_foo",
2850+
model=model,
2851+
context=Context(
2852+
config=Config(
2853+
model_defaults=ModelDefaultsConfig(dialect="snowflake"), variables=variables
2854+
)
2855+
),
2856+
)
2857+
2858+
assert test.body["model"] == '"GOLD"."SUSHI"."FOO"'
2859+
2860+
_check_successful_or_raise(test.run())
2861+
2862+
2863+
def test_parameterized_name_python_model() -> None:
2864+
variables = {"table_catalog": "gold"}
2865+
2866+
@model(
2867+
name="@{table_catalog}.sushi.foo",
2868+
columns={
2869+
"id": "int",
2870+
"name": "varchar",
2871+
},
2872+
dialect="snowflake",
2873+
)
2874+
def execute(
2875+
context: ExecutionContext,
2876+
**kwargs: t.Any,
2877+
) -> pd.DataFrame:
2878+
return pd.DataFrame([{"ID": 1, "NAME": "foo"}])
2879+
2880+
python_model = model.get_registry()["@{table_catalog}.sushi.foo"].model(
2881+
module_path=Path("."), path=Path("."), variables=variables
2882+
)
2883+
2884+
assert python_model.fqn == '"GOLD"."SUSHI"."FOO"'
2885+
2886+
test = _create_test(
2887+
body=load_yaml(
2888+
"""
2889+
test_foo:
2890+
model: {{ var('table_catalog' ) }}.sushi.foo
2891+
outputs:
2892+
query:
2893+
- id: 1
2894+
name: foo
2895+
""",
2896+
variables=variables,
2897+
),
2898+
test_name="test_foo",
2899+
model=python_model,
2900+
context=Context(
2901+
config=Config(
2902+
model_defaults=ModelDefaultsConfig(dialect="snowflake"), variables=variables
2903+
)
2904+
),
2905+
)
2906+
2907+
assert test.body["model"] == '"GOLD"."SUSHI"."FOO"'
2908+
2909+
_check_successful_or_raise(test.run())
2910+
2911+
2912+
def test_parameterized_name_self_referential_model():
2913+
variables = {"table_catalog": "gold"}
2914+
model = _create_model(
2915+
"""
2916+
with last_value as (
2917+
select coalesce(max(v), 0) as v from @{table_catalog}.sushi.foo
2918+
)
2919+
select v + 1 as v from last_value
2920+
""",
2921+
meta="""
2922+
MODEL (
2923+
name @{table_catalog}.sushi.foo,
2924+
kind FULL
2925+
)
2926+
""",
2927+
dialect="snowflake",
2928+
variables=variables,
2929+
)
2930+
assert model.fqn == '"GOLD"."SUSHI"."FOO"'
2931+
2932+
test1 = _create_test(
2933+
body=load_yaml(
2934+
"""
2935+
test_foo_intial_state:
2936+
model: {{ var('table_catalog' ) }}.sushi.foo
2937+
inputs:
2938+
{{ var('table_catalog' ) }}.sushi.foo:
2939+
rows: []
2940+
columns:
2941+
v: int
2942+
outputs:
2943+
query:
2944+
- v: 1
2945+
""",
2946+
variables=variables,
2947+
),
2948+
test_name="test_foo_intial_state",
2949+
model=model,
2950+
context=Context(
2951+
config=Config(
2952+
model_defaults=ModelDefaultsConfig(dialect="snowflake"), variables=variables
2953+
)
2954+
),
2955+
)
2956+
assert isinstance(test1, SqlModelTest)
2957+
assert test1.body["model"] == '"GOLD"."SUSHI"."FOO"'
2958+
test1_model_query = test1._render_model_query().sql(dialect="snowflake")
2959+
assert '"GOLD"."SUSHI"."FOO"' not in test1_model_query
2960+
assert (
2961+
test1._test_fixture_table('"GOLD"."SUSHI"."FOO"').sql(dialect="snowflake", identify=True)
2962+
in test1_model_query
2963+
)
2964+
2965+
test2 = _create_test(
2966+
body=load_yaml(
2967+
"""
2968+
test_foo_cumulative:
2969+
model: {{ var('table_catalog' ) }}.sushi.foo
2970+
inputs:
2971+
{{ var('table_catalog' ) }}.sushi.foo:
2972+
rows:
2973+
- v: 5
2974+
outputs:
2975+
query:
2976+
- v: 6
2977+
""",
2978+
variables=variables,
2979+
),
2980+
test_name="test_foo_cumulative",
2981+
model=model,
2982+
context=Context(
2983+
config=Config(
2984+
model_defaults=ModelDefaultsConfig(dialect="snowflake"), variables=variables
2985+
)
2986+
),
2987+
)
2988+
assert isinstance(test2, SqlModelTest)
2989+
assert test2.body["model"] == '"GOLD"."SUSHI"."FOO"'
2990+
test2_model_query = test2._render_model_query().sql(dialect="snowflake")
2991+
assert '"GOLD"."SUSHI"."FOO"' not in test2_model_query
2992+
assert (
2993+
test2._test_fixture_table('"GOLD"."SUSHI"."FOO"').sql(dialect="snowflake", identify=True)
2994+
in test2_model_query
2995+
)
2996+
2997+
_check_successful_or_raise(test1.run())
2998+
_check_successful_or_raise(test2.run())

0 commit comments

Comments
 (0)