Skip to content

Commit

Permalink
Create accepted_range test
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Labes authored and clrcrl committed Nov 23, 2020
1 parent 18f0db1 commit 2e3a343
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions macros/schema_tests/accepted_range.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% macro test_accepted_range(model, min_value = none, max_value = none, inclusive = true, where = "true") %}

{%- set column_name = kwargs.get('column_name', kwargs.get('field')) -%}

with meet_condition as(
select {{ column_name }}
from {{ model }}
where {{ where }}
),

validation_errors as (
select *
from meet_condition
where
-- never true, defaults to an empty result set. Exists to ensure any combo of the `or` clauses below succeeds
1 = 2

{%- if min_value is not none %}
-- records with a value >= min_value are permitted. The `not` flips this to find records that don't meet the rule.
or not {{ column_name }} >{{"=" if inclusive}} {{min_value}}
{%- endif %}

{%- if max_value is not none %}
-- records with a value <= max_value are permitted. The `not` flips this to find records that don't meet the rule.
or not {{ column_name }} <{{"=" if inclusive}} {{max_value}}
{%- endif %}
)

select count(*)
from validation_errors

{% endmacro %}

0 comments on commit 2e3a343

Please sign in to comment.