Skip to content

Commit 7a4c5e6

Browse files
Claire Carrollclrcrl
Claire Carroll
authored andcommitted
Add fewer_rows_than schema test (#343)
1 parent 81466f4 commit 7a4c5e6

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Add new schema test, `sequential_values` ([#318](https://github.com/fishtown-analytics/dbt-utils/pull/318), inspired by [@hundredwatt](https://github.com/hundredwatt))
88
* Support `quarter` in the `postgres__last_day` macro ([#333](https://github.com/fishtown-analytics/dbt-utils/pull/333/files), [@seunghanhong](https://github.com/seunghanhong))
99
* Add new argument, `unit`, to `haversine_distance` [#340](https://github.com/fishtown-analytics/dbt-utils/pull/340) [@bastienboutonnet](https://github.com/bastienboutonnet)
10+
* 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])
1011

1112

1213
## Fixes

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ models:
138138

139139
```
140140

141+
#### fewer_rows_than ([source](macros/schema_tests/fewer_rows_than.sql))
142+
This schema test asserts that this model has fewer rows than the referenced model.
143+
144+
Usage:
145+
```yaml
146+
version: 2
147+
148+
models:
149+
- name: model_name
150+
tests:
151+
- dbt_utils.fewer_rows_than:
152+
compare_model: ref('other_table_name')
153+
```
154+
141155
#### equality ([source](macros/schema_tests/equality.sql))
142156
This schema test asserts the equality of two relations. Optionally specify a subset of columns to compare.
143157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
field
2+
1
3+
2
4+
3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
field
2+
1
3+
2
4+
3
5+
4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with data as (
2+
3+
select * from {{ ref('data_test_fewer_rows_than_table_1') }}
4+
5+
)
6+
7+
select
8+
field
9+
from data
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{% macro test_fewer_rows_than(model) %}
2+
{{ return(adapter.dispatch('test_fewer_rows_than', packages = dbt_utils._get_utils_namespaces())(model, combination_of_columns, quote_columns, where)) }}
3+
{% endmacro %}
4+
5+
{% macro default__test_fewer_rows_than(model) %}
6+
7+
{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %}
8+
9+
with a as (
10+
11+
select count(*) as count_ourmodel from {{ model }}
12+
13+
),
14+
b as (
15+
16+
select count(*) as count_comparisonmodel from {{ compare_model }}
17+
18+
),
19+
counts as (
20+
21+
select
22+
(select count_ourmodel from a) as count_model_with_fewer_rows,
23+
(select count_comparisonmodel from b) as count_model_with_more_rows
24+
25+
),
26+
final as (
27+
28+
select
29+
case
30+
-- fail the test if we have more rows than the reference model and return the row count delta
31+
when count_model_with_fewer_rows > count_model_with_more_rows then (count_model_with_fewer_rows - count_model_with_more_rows)
32+
-- fail the test if they are the same number
33+
when count_model = count_comparison then 1
34+
-- pass the test if the delta is positive (i.e. return the number 0)
35+
else 0
36+
end as row_count_delta
37+
from counts
38+
39+
)
40+
41+
select row_count_delta from final
42+
43+
{% endmacro %}

0 commit comments

Comments
 (0)