Skip to content

Commit

Permalink
Add fewer_rows_than schema test (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire Carroll authored and clrcrl committed May 18, 2021
1 parent acdd030 commit ff841c3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Add new schema test, `sequential_values` ([#318](https://github.com/fishtown-analytics/dbt-utils/pull/318), inspired by [@hundredwatt](https://github.com/hundredwatt))
* Support `quarter` in the `postgres__last_day` macro ([#333](https://github.com/fishtown-analytics/dbt-utils/pull/333/files), [@seunghanhong](https://github.com/seunghanhong))
* Add new argument, `unit`, to `haversine_distance` [#340](https://github.com/fishtown-analytics/dbt-utils/pull/340) [@bastienboutonnet](https://github.com/bastienboutonnet)
* Add new schema test, `fewer_rows_than` (code originally in [#221](https://github.com/fishtown-analytics/dbt-utils/pull/230/) from [@dmarts](https://github.com/dmarts), merged via [#343])


## Fixes
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ models:

```

#### fewer_rows_than ([source](macros/schema_tests/fewer_rows_than.sql))
This schema test asserts that this model has fewer rows than the referenced model.

Usage:
```yaml
version: 2

models:
- name: model_name
tests:
- dbt_utils.fewer_rows_than:
compare_model: ref('other_table_name')
```
#### equality ([source](macros/schema_tests/equality.sql))
This schema test asserts the equality of two relations. Optionally specify a subset of columns to compare.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
field
1
2
3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
field
1
2
3
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with data as (

select * from {{ ref('data_test_fewer_rows_than_table_1') }}

)

select
field
from data
43 changes: 43 additions & 0 deletions macros/schema_tests/fewer_rows_than.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% macro test_fewer_rows_than(model) %}
{{ return(adapter.dispatch('test_fewer_rows_than', packages = dbt_utils._get_utils_namespaces())(model, combination_of_columns, quote_columns, where)) }}
{% endmacro %}

{% macro default__test_fewer_rows_than(model) %}

{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %}

with a as (

select count(*) as count_ourmodel from {{ model }}

),
b as (

select count(*) as count_comparisonmodel from {{ compare_model }}

),
counts as (

select
(select count_ourmodel from a) as count_model_with_fewer_rows,
(select count_comparisonmodel from b) as count_model_with_more_rows

),
final as (

select
case
-- fail the test if we have more rows than the reference model and return the row count delta
when count_model_with_fewer_rows > count_model_with_more_rows then (count_model_with_fewer_rows - count_model_with_more_rows)
-- fail the test if they are the same number
when count_model = count_comparison then 1
-- pass the test if the delta is positive (i.e. return the number 0)
else 0
end as row_count_delta
from counts

)

select row_count_delta from final

{% endmacro %}

0 comments on commit ff841c3

Please sign in to comment.