Skip to content

Commit 6bec09b

Browse files
author
Claire Carroll
authored
Merge pull request #284 from JavierMonton/schema_test/not_accepted_values
Schema test/not accepted values
2 parents 76db1b7 + f60eebc commit 6bec09b

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
## Features
55
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
66
* Make `expression_is_true` work as a column test (code originally in [#226](https://github.com/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https://github.com/elliottohara), merged via [#313])
7-
* Allow individual columns in star macro to be aliased (code originally in [#230](https://github.com/fishtown-analytics/dbt-utils/pull/230/) from [@elliottohara](https://github.com/elliottohara), merged via [#245])
7+
* Allow individual columns in star macro to be aliased (code originally in [#230](https://github.com/fishtown-analytics/dbt-utils/pull/230/) from [@elliottohara](https://github.com/elliottohara), merged via [#245])
8+
* Add new schema test, `not_accepted_values` ([#284](https://github.com/fishtown-analytics/dbt-utils/pull/284) [@JavierMonton](https://github.com/JavierMonton))
89

910

1011
## Fixes

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ models:
300300
where: "_deleted = false"
301301
```
302302

303+
#### not_accepted_values ([source](macros/schema_tests/not_accepted_values.sql))
304+
This test validates that there are no rows that match the given values.
305+
306+
Usage:
307+
```yaml
308+
version: 2
309+
310+
models:
311+
- name: my_model
312+
columns:
313+
- name: city
314+
tests:
315+
- dbt_utils.not_accepted_values:
316+
values: ['Barcelona', 'New York']
317+
```
318+
303319
#### relationships_where ([source](macros/schema_tests/relationships_where.sql))
304320
This test validates the referential integrity between two relations (same as the core relationships schema test) with an added predicate to filter out some rows from the test. This is useful to exclude records such as test entities, rows created in the last X minutes/hours to account for temporary gaps due to ETL limitations, etc.
305321

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id,city
2+
1,Barcelona
3+
2,London
4+
3,Paris
5+
4,New York

integration_tests/models/schema_tests/schema.yml

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ models:
7474
- dbt_utils.not_null_where:
7575
where: "_deleted = false"
7676

77+
- name: data_test_not_accepted_values
78+
columns:
79+
- name: city
80+
tests:
81+
- dbt_utils.not_accepted_values:
82+
values: ['Madrid', 'Berlin']
83+
7784
- name: data_test_relationships_where_table_2
7885
columns:
7986
- name: id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{% macro test_not_accepted_values(model, values) %}
2+
3+
{% set column_name = kwargs.get('column_name', kwargs.get('field')) %}
4+
{% set quote_values = kwargs.get('quote', True) %}
5+
6+
with all_values as (
7+
8+
select distinct
9+
{{ column_name }} as value_field
10+
11+
from {{ model }}
12+
13+
),
14+
15+
validation_errors as (
16+
17+
select
18+
value_field
19+
20+
from all_values
21+
where value_field in (
22+
{% for value in values -%}
23+
{% if quote_values -%}
24+
'{{ value }}'
25+
{%- else -%}
26+
{{ value }}
27+
{%- endif -%}
28+
{%- if not loop.last -%},{%- endif %}
29+
{%- endfor %}
30+
)
31+
32+
)
33+
34+
select count(*) as validation_errors
35+
from validation_errors
36+
37+
{% endmacro %}

0 commit comments

Comments
 (0)