Skip to content

Commit 8388f76

Browse files
JavierMontonclrcrl
authored andcommitted
Add not_accepted_values schema test (#284)
Schema Test - Not accepted values, test fixed empty lines at the end Update changelog
1 parent 230d329 commit 8388f76

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## Features
33
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
44
* 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])
5+
* Add new schema test, `not_accepted_values` ([#284](https://github.com/fishtown-analytics/dbt-utils/pull/284) [@JavierMonton](https://github.com/JavierMonton))
56

67
## Fixes
78
* Handle booleans gracefully in the unpivot macro ([#305](https://github.com/fishtown-analytics/dbt-utils/pull/305) [@avishalom](https://github.com/avishalom))
@@ -24,7 +25,6 @@
2425

2526
- Bump `require-dbt-version` to `[">=0.18.0", "<0.20.0"]` to support dbt v0.19.0 ([#308](https://github.com/fishtown-analytics/dbt-utils/pull/308), [#309](https://github.com/fishtown-analytics/dbt-utils/pull/309))
2627

27-
2828
# dbt-utils v0.6.2
2929

3030
## 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)