Skip to content

Commit

Permalink
Create expect_column_set_to_be_unique_case_insensitive.sql (#138)
Browse files Browse the repository at this point in the history
* Create expect_column_set_to_be_unique_case_insensitive.sql

* adjusting consistent casing test and documentation

* adjusting formatting and adding integration test

* Fix formatting

* Formatting updates

* adjusting Readme and adding test instance

* Fix spelling

Co-authored-by: clausherther <[email protected]>
  • Loading branch information
agusfigueroa-htg and clausherther authored Feb 24, 2022
1 parent 590d599 commit 854e653
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
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 number of inconsistent values in the column will be displayed in the terminal whereas the inconsistent values themselves will be returned if the SQL compiled test is run.

*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
7 changes: 4 additions & 3 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,11 @@ models:
- dbt_expectations.expect_column_distinct_values_to_contain_set:
value_set: ['a','b']
quote_values: true

- dbt_expectations.expect_column_value_lengths_to_equal :
- dbt_expectations.expect_column_value_lengths_to_equal:
value: 1
- dbt_expectations.expect_column_values_to_have_consistent_casing
- dbt_expectations.expect_column_values_to_have_consistent_casing:
display_inconsistent_columns: true

- name: col_string_b
tests:
Expand Down Expand Up @@ -328,4 +330,3 @@ models:
datepart: day
interval: 1
row_condition: group_id = 4

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% 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
from
{{ model }}

),
{% if display_inconsistent_columns %}
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

)
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 -%}

0 comments on commit 854e653

Please sign in to comment.