Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add not_empty_string test #634

Merged
merged 16 commits into from
Sep 14, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

## New features
- New feature to omit the `source_column_name` column on the `union_relations` macro ([#331](https://github.com/dbt-labs/dbt-utils/issues/331), [#624](https://github.com/dbt-labs/dbt-utils/pull/624))
- Add `not_empty_string` generic test that asserts column values are not an empty string. ([#632](https://github.com/dbt-labs/dbt-utils/issues/632), [#634](https://github.com/dbt-labs/dbt-utils/pull/634))

## Fixes
- Better handling of whitespaces in the star macro ([#651](https://github.com/dbt-labs/dbt-utils/pull/651))
- Fix to correct behavior in `mutually_exclusive_ranges` test in certain situations when `zero_length_range_allowed: true` and multiple ranges in a partition have the same value for `lower_bound_column`. ([[#659](https://github.com/dbt-labs/dbt-utils/issues/659)], [#660](https://github.com/dbt-labs/dbt-utils/pull/660))

## Contributors:
- [@christineberger](https://github.com/christineberger) (#624)
- [@epapineau](https://github.com/epapineau) (#634)
- [@courentin](https://github.com/courentin) (#651)
- [@sfc-gh-ancoleman](https://github.com/sfc-gh-ancoleman) (#660)

Expand Down
64 changes: 48 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ Check [dbt Hub](https://hub.getdbt.com/dbt-labs/dbt_utils/latest/) for the lates

**[Generic tests](#generic-tests)**

- [equal_rowcount](#equal_rowcount-source)
- [fewer_rows_than](#fewer_rows_than-source)
- [equality](#equality-source)
- [expression_is_true](#expression_is_true-source)
- [recency](#recency-source)
- [at_least_one](#at_least_one-source)
- [not_constant](#not_constant-source)
- [cardinality_equality](#cardinality_equality-source)
- [unique_where](#unique_where-source)
- [not_null_where](#not_null_where-source)
- [not_null_proportion](#not_null_proportion-source)
- [not_accepted_values](#not_accepted_values-source)
- [relationships_where](#relationships_where-source)
- [mutually_exclusive_ranges](#mutually_exclusive_ranges-source)
- [unique_combination_of_columns](#unique_combination_of_columns-source)
- [accepted_range](#accepted_range-source)
- [equal_rowcount](#equal_rowcount-source)
- [fewer_rows_than](#fewer_rows_than-source)
- [equality](#equality-source)
- [expression_is_true](#expression_is_true-source)
- [recency](#recency-source)
- [at_least_one](#at_least_one-source)
- [not_constant](#not_constant-source)
- [not_empty_string](#not_empty_string-source)
- [cardinality_equality](#cardinality_equality-source)
- [unique_where](#unique_where-source)
- [not_null_where](#not_null_where-source)
- [not_null_proportion](#not_null_proportion-source)
- [not_accepted_values](#not_accepted_values-source)
- [relationships_where](#relationships_where-source)
- [mutually_exclusive_ranges](#mutually_exclusive_ranges-source)
- [unique_combination_of_columns](#unique_combination_of_columns-source)
- [accepted_range](#accepted_range-source)

**[Macros](#macros)**

Expand Down Expand Up @@ -240,6 +241,37 @@ models:
- dbt_utils.not_constant
```

#### not_empty_string ([source](macros/generic_tests/not_empty_string.sql))
Asserts that a column does not have any values equal to `''`.

**Usage:**
```yaml
version: 2

models:
- name: model_name
columns:
- name: column_name
tests:
- dbt_utils.not_empty_string
```

The macro accepts an optional argument `trim_whitespace` that controls whether whitespace should be trimmed from the column when evaluating. The default is `true`.

**Usage:**
```yaml
version: 2

models:
- name: model_name
columns:
- name: column_name
tests:
- dbt_utils.not_empty_string:
trim_whitespace: false

```

#### cardinality_equality ([source](macros/generic_tests/cardinality_equality.sql))

Asserts that values in a given column have exactly the same cardinality as values from a different column in a different model.
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/.env/postgres.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POSTGRES_TEST_HOST=localhost
POSTGRES_TEST_USER=root
POSTGRES_TEST_USER=elizepapineau
POSTGRES_TEST_PASS=''
POSTGRES_TEST_PORT=5432
POSTGRES_TEST_DBNAME=circle_test
23 changes: 22 additions & 1 deletion integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,29 @@ models:
- dbt_utils.equality:
compare_model: ref('data_deduplicate_expected')

- name: test_not_empty_string_failing
columns:
- name: string_trim_whitespace_true
tests:
- dbt_utils.not_empty_string:
config:
severity: error
error_if: "<1"
warn_if: "<0"

- name: test_not_empty_string_passing
columns:
- name: string_trim_whitespace_true
tests:
- dbt_utils.not_empty_string
- name: string_trim_whitespace_false
tests:
- dbt_utils.not_empty_string:
trim_whitespace: false

- name: test_width_bucket
tests:
- assert_equal:
actual: actual
expected: expected
expected: expected

41 changes: 41 additions & 0 deletions integration_tests/models/sql/test_not_empty_string_failing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- dbt seed casts '' as NULL, so we need to select empty strings to enable testing

with blank_data as (

select
1 as id,
'not an empty string' as string_trim_whitespace_true

union all

select
2 as id,
'also not an empty string' as string_trim_whitespace_true

union all

select
3 as id,
'string with trailing whitespace ' as string_trim_whitespace_true

union all

select
4 as id,
' ' as string_trim_whitespace_true

union all

select
5 as id,
'' as string_trim_whitespace_true

union all

select
6 as id,
null as string_trim_whitespace_true

)

select * from blank_data
33 changes: 33 additions & 0 deletions integration_tests/models/sql/test_not_empty_string_passing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- dbt seed casts '' as NULL, so we need to select empty strings to enable testing

with blank_data as (

select
1 as id,
'not an empty string' as string_trim_whitespace_true,
'not an empty string' as string_trim_whitespace_false

union all

select
2 as id,
'also not an empty string' as string_trim_whitespace_true,
'also not an empty string' as string_trim_whitespace_false

union all

select
3 as id,
'string with trailing whitespace ' as string_trim_whitespace_true,
' ' as string_trim_whitespace_false -- This will cause a failure when trim_whitespace = true

union all

select
6 as id,
null as string_trim_whitespace_true,
null as string_trim_whitespace_false

)

select * from blank_data
39 changes: 39 additions & 0 deletions macros/generic_tests/not_empty_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% test not_empty_string(model, column_name, trim_whitespace=true) %}

{{ return(adapter.dispatch('test_not_empty_string', 'dbt_utils')(model, column_name, trim_whitespace)) }}

{% endtest %}

{% macro default__test_not_empty_string(model, column_name, trim_whitespace=true) %}

with

all_values as (

select


{% if trim_whitespace == true -%}

trim({{ column_name }}) as {{ column_name }}

{%- else -%}

{{ column_name }}

{%- endif %}

from {{ model }}

),

errors as (

select * from all_values
where {{ column_name }} = ''

)

select * from errors

{% endmacro %}