diff --git a/README.md b/README.md index 9a7fd308..0f5dfe17 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,31 @@ model_name: - ref('other_table_name') ``` + +#### at_least_one ([source](macros/schema_tests/at_least_one.sql)) +This schema test asserts if column has at least one value. + +Usage: +``` +model_name: + constraints: + at_least_one: + - column_name + +``` + +#### not_constant ([source](macros/schema_tests/not_constant.sql)) +This schema test asserts if column does not have same value in all rows. + +Usage: +``` +model_name: + constraints: + not_constant: + - column_name + +``` + --- ### SQL helpers #### group_by ([source](macros/sql/groupby.sql)) diff --git a/macros/schema_tests/at_least_one.sql b/macros/schema_tests/at_least_one.sql new file mode 100644 index 00000000..94c00fe8 --- /dev/null +++ b/macros/schema_tests/at_least_one.sql @@ -0,0 +1,15 @@ +{% macro test_at_least_one(model, arg) %} + +select count(*) +from ( + select + + count({{ arg }}) + + from {{ model }} + + having count({{ arg }}) = 0 + +) validation_errors + +{% endmacro %} diff --git a/macros/schema_tests/not_constant.sql b/macros/schema_tests/not_constant.sql new file mode 100644 index 00000000..80b9b0df --- /dev/null +++ b/macros/schema_tests/not_constant.sql @@ -0,0 +1,18 @@ + +{% macro test_not_constant(model, arg) %} + +select count(*) + +from ( + + select + count(distinct {{ arg }}) + + from {{ model }} + + having count(distinct {{ arg }}) = 1 + + ) validation_errors + + +{% endmacro %}