Skip to content

Commit 8e515af

Browse files
Joel Labesclrcrl
Joel Labes
authored andcommitted
Create accepted_range test (#276)
Add integration tests Add accepted_range documentation Fixing invalid yaml in readme Add missing columns: entry Co-authored-by: Claire Carroll <carroll.claire.e@gmail.com> Update changelog
1 parent 176426c commit 8e515af

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# dbt-utils v0.6.5 (unreleased)
22
## Features
3+
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
34

45
## Fixes
56

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,45 @@ An optional `quote_columns` parameter (`default=false`) can also be used if a co
418418
quote_columns: true
419419
```
420420

421+
422+
#### accepted_range ([source](macros/schema_tests/accepted_range.sql))
423+
This test checks that a column's values fall inside an expected range. Any combination of `min_value` and `max_value` is allowed, and the range can be inclusive or exclusive. Provide a `where` argument to filter to specific records only.
424+
425+
In addition to comparisons to a scalar value, you can also compare to another column's values. Any data type that supports the `>` or `<` operators can be compared, so you could also run tests like checking that all order dates are in the past.
426+
427+
Usage:
428+
```yaml
429+
version: 2
430+
431+
models:
432+
- name: model_name
433+
columns:
434+
- name: user_id
435+
tests:
436+
- dbt_utils.accepted_range:
437+
min_value: 0
438+
inclusive: false
439+
440+
- name: account_created_at
441+
tests:
442+
- dbt_utils.accepted_range:
443+
max_value: "getdate()"
444+
#inclusive is true by default
445+
446+
- name: num_returned_orders
447+
tests:
448+
- dbt_utils.accepted_range:
449+
min_value: 0
450+
max_value: "num_orders"
451+
452+
- name: num_web_sessions
453+
tests:
454+
- dbt_utils.accepted_range:
455+
min_value: 0
456+
inclusive: false
457+
where: "num_orders > 0"
458+
```
459+
421460
---
422461
### SQL helpers
423462
#### get_query_results_as_dict ([source](macros/sql/get_query_results_as_dict.sql))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id
2+
-1
3+
11

integration_tests/models/schema_tests/schema.yml

+19
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,22 @@ models:
101101
combination_of_columns:
102102
- month
103103
- product
104+
105+
- name: data_test_accepted_range
106+
columns:
107+
- name: id
108+
tests:
109+
- dbt_utils.accepted_range:
110+
min_value: -1
111+
max_value: 11
112+
inclusive: true
113+
114+
- dbt_utils.accepted_range:
115+
min_value: -2
116+
max_value: 11.1
117+
inclusive: false
118+
119+
- dbt_utils.accepted_range:
120+
min_value: 0
121+
inclusive: true
122+
where: "id <> -1"
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{% macro test_accepted_range(model, min_value = none, max_value = none, inclusive = true, where = "true") %}
2+
3+
{%- set column_name = kwargs.get('column_name', kwargs.get('field')) -%}
4+
5+
with meet_condition as(
6+
select {{ column_name }}
7+
from {{ model }}
8+
where {{ where }}
9+
),
10+
11+
validation_errors as (
12+
select *
13+
from meet_condition
14+
where
15+
-- never true, defaults to an empty result set. Exists to ensure any combo of the `or` clauses below succeeds
16+
1 = 2
17+
18+
{%- if min_value is not none %}
19+
-- records with a value >= min_value are permitted. The `not` flips this to find records that don't meet the rule.
20+
or not {{ column_name }} > {{- "=" if inclusive }} {{ min_value }}
21+
{%- endif %}
22+
23+
{%- if max_value is not none %}
24+
-- records with a value <= max_value are permitted. The `not` flips this to find records that don't meet the rule.
25+
or not {{ column_name }} < {{- "=" if inclusive }} {{ max_value }}
26+
{%- endif %}
27+
)
28+
29+
select count(*)
30+
from validation_errors
31+
32+
{% endmacro %}

0 commit comments

Comments
 (0)