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

Create expect_column_set_to_be_unique_case_insensitive.sql #138

Merged
merged 7 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ To run the tests:
- [expect_column_values_to_be_unique](#expect_column_values_to_be_unique)
- [expect_column_values_to_be_of_type](#expect_column_values_to_be_of_type)
- [expect_column_values_to_be_in_type_list](#expect_column_values_to_be_in_type_list)
- [expect_column_values_to_have_consistent_casing](#expect_column_values_to_have_consistent_casing)

### Sets and ranges

Expand Down Expand Up @@ -410,6 +411,18 @@ tests:
column_type_list: [date, datetime]
```

### [expect_column_values_to_have_consistent_casing](macros/schema_tests/column_values_basic/expect_column_values_to_have_consistent_casing.sql)

Expect a column to have consistent casing. By setting display_inconsistent_columns to true, the inconsistent columns will be displayed.
agusfigueroa-htg marked this conversation as resolved.
Show resolved Hide resolved

*Applies to:* Column

```yaml
tests:
- dbt_expectations.expect_column_values_to_have_consistent_casing:
display_inconsistent_columns: false # (Optional)
```

### [expect_column_values_to_be_in_set](macros/schema_tests/column_values_basic/expect_column_values_to_be_in_set.sql)

Expect each column value to be in a given set.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% test expect_column_values_to_have_consistent_casing(model, column_name, display_inconsistent_columns=False) %}

with test_data as (

select
distinct {{ column_name }} as distinct_values
agusfigueroa-htg marked this conversation as resolved.
Show resolved Hide resolved
from
{{ model }}
where 1=1

),
{% if display_inconsistent_columns %}
agusfigueroa-htg marked this conversation as resolved.
Show resolved Hide resolved
validation_errors as (

select
lower(distinct_values) as inconsistent_columns,
count(distinct_values) as set_count_case_insensitive
from test_data
group by 1
having count(distinct_values)>1
agusfigueroa-htg marked this conversation as resolved.
Show resolved Hide resolved

)
select * from validation_errors
{% else %}
validation_errors as (

select
count(1) as set_count,
count(distinct lower(distinct_values)) as set_count_case_insensitive
from test_data

)
select * from validation_errors
where set_count!=set_count_case_insensitive
{% endif %}
{%- endtest -%}