Skip to content

Commit ec3202f

Browse files
committed
Add tests
1 parent df3d295 commit ec3202f

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

tests/core/test_model.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,114 @@ def test_opt_out_of_time_column_in_partitioned_by():
571571
assert model.partitioned_by == [exp.to_column('"b"')]
572572

573573

574+
def test_model_no_name():
575+
expressions = d.parse(
576+
"""
577+
MODEL (
578+
dialect bigquery,
579+
);
580+
581+
SELECT 1::int AS a, 2::int AS b;
582+
"""
583+
)
584+
585+
with pytest.raises(ConfigError) as ex:
586+
load_sql_based_model(expressions)
587+
assert (
588+
str(ex.value)
589+
== "Please add the required 'name' field to the MODEL block at the top of the file.\n\nLearn more at https://sqlmesh.readthedocs.io/en/stable/concepts/models/overview"
590+
)
591+
592+
593+
def test_model_field_name_suggestions():
594+
# top-level field
595+
expressions = d.parse(
596+
"""
597+
MODEL (
598+
name db.table,
599+
dialects bigquery,
600+
);
601+
602+
SELECT 1::int AS a, 2::int AS b;
603+
"""
604+
)
605+
606+
with pytest.raises(ConfigError) as ex:
607+
load_sql_based_model(expressions)
608+
assert (
609+
str(ex.value)
610+
== "Invalid field name present in the MODEL block: 'dialects'. Did you mean 'dialect'?"
611+
)
612+
613+
# kind field
614+
expressions = d.parse(
615+
"""
616+
MODEL (
617+
name db.table,
618+
kind INCREMENTAL_BY_TIME_RANGE(
619+
time_column a,
620+
batch_sizes 1
621+
),
622+
);
623+
624+
SELECT 1::int AS a, 2::int AS b;
625+
"""
626+
)
627+
628+
with pytest.raises(ConfigError) as ex:
629+
load_sql_based_model(expressions)
630+
assert (
631+
str(ex.value)
632+
== "Invalid field name present in the MODEL block 'kind INCREMENTAL_BY_TIME_RANGE(' field: 'batch_sizes'. Did you mean 'batch_size'?"
633+
)
634+
635+
# multiple fields
636+
expressions = d.parse(
637+
"""
638+
MODEL (
639+
name db.table,
640+
dialects bigquery,
641+
descriptions 'a',
642+
asdfasdf true
643+
);
644+
645+
SELECT 1::int AS a, 2::int AS b;
646+
"""
647+
)
648+
649+
with pytest.raises(ConfigError) as ex:
650+
load_sql_based_model(expressions)
651+
ex_str = str(ex.value)
652+
# field order is non-deterministic, so we can't test the output string directly
653+
assert "Invalid field names present in the MODEL block: " in ex_str
654+
assert "'descriptions'" in ex_str
655+
assert "'dialects'" in ex_str
656+
assert "'asdfasdf'" in ex_str
657+
assert "- descriptions: Did you mean 'description'?" in ex_str
658+
assert "- dialects: Did you mean 'dialect'?" in ex_str
659+
assert "- asdfasdf: Did you mean " not in ex_str
660+
661+
662+
def test_model_required_field_missing():
663+
expressions = d.parse(
664+
"""
665+
MODEL (
666+
name db.table,
667+
kind INCREMENTAL_BY_TIME_RANGE (),
668+
);
669+
670+
SELECT 1::int AS a, 2::int AS b;
671+
"""
672+
)
673+
674+
with pytest.raises(ConfigError) as ex:
675+
load_sql_based_model(expressions)
676+
assert (
677+
str(ex.value)
678+
== "Please add required field 'time_column' to the MODEL block 'kind INCREMENTAL_BY_TIME_RANGE(' field."
679+
)
680+
681+
574682
def test_no_model_statement(tmp_path: Path):
575683
# No name inference => MODEL (...) is required
576684
expressions = d.parse("SELECT 1 AS x")

0 commit comments

Comments
 (0)