Skip to content

Commit

Permalink
Check for subset of columns (Close dbt-labs#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlh1994 committed May 2, 2023
1 parent 9d911b6 commit 2aa0a25
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with data as (

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

)

select
col_a, col_b
from data
7 changes: 7 additions & 0 deletions integration_tests/models/generic_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,10 @@ models:
- dbt_utils.fewer_rows_than:
compare_model: ref('data_test_fewer_rows_than_table_2')
group_by_columns: ['col_a']

- name: equality_less_columns
tests:
- dbt_utils.equality:
compare_model: ref('data_test_equality_a')
error_if: "<1" #sneaky way to ensure that the test is returning failing rows
warn_if: "<0"
4 changes: 2 additions & 2 deletions integration_tests/models/sql/test_union.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
select
id,
name,
favorite_color
favorite_color,
favorite_number

from {{ ref('test_union_base') }}

13 changes: 13 additions & 0 deletions macros/generic_tests/equality.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@
{{ return('') }}
{% endif %}



-- setup
{%- do dbt_utils._is_relation(model, 'test_equality') -%}

{# Ensure there are no extra columns in the compare_model vs model #}
{%- if not compare_columns and not ignore_columns -%}
{%- do dbt_utils._is_ephemeral(model, 'test_equality') -%}
{%- set compare_columns_set = set(adapter.get_columns_in_relation(model) | map(attribute='quoted')) -%}
{%- do dbt_utils._is_ephemeral(model, 'test_equality') -%}
{%- set compare_model_columns_set = set(adapter.get_columns_in_relation(compare_model) | map(attribute='quoted')) -%}
{% if compare_columns_set != compare_model_columns_set %}
{{ return("select 1, 'b_minus_a' as which_diff from " ~ compare_model) }}
{% endif %}
{% endif %}

{%- if not precision -%}
{#-
If the compare_cols arg is provided, we can run this test without querying the
Expand Down

0 comments on commit 2aa0a25

Please sign in to comment.