EqualExperts/dbt-unit-testing

I want to compare only the ref mocked data with the expectations not also what is in the real model

rcepoiu opened this issue · 8 comments

Hey there,

I wrote a test like the following:

-- depends_on: {{ ref('my_first_dbt_model_conformed') }}
{{ config(tags=['unit-test-conformed']) }}

{% call dbt_unit_testing.test('my_second_dbt_model_conformed', 'products we are interested in should be present in the output') %}

{% call dbt_unit_testing.mock_ref('my_first_dbt_model_conformed') %}
select 1 as C_CUSTKEY, 'Customer1' as C_NAME, 'AUTOMOBILE' as C_MKTSEGMENT
UNION ALL
select 2 as C_CUSTKEY, 'Customer2' as C_NAME, 'HOUSEHOLD' as C_MKTSEGMENT
UNION ALL
select 3 as C_CUSTKEY, 'Customer3' as C_NAME, 'BUILDING' as C_MKTSEGMENT
{% endcall %}

{% call dbt_unit_testing.expect() %}
select 1 as C_CUSTKEY, 'Customer1' as C_NAME, 'AUTOMOBILE' as C_MKTSEGMENT
{% endcall %}
{% endcall %}

The problem here is when I run the dbt test it fails because the expected row is different than what's in the real model... however I don't need to compare what's in the real model, I just want to validate the model by making use of the pre-defined mocks...
How can I do that as I don't want to compare the mocked expected row with "actual" data from the model?

11:42:13 1 of 1 START test my_second_dbt_model_conformed_unit_test ...................... [RUN]
11:42:14 MODEL: model_name
11:42:14 TEST: products we are interested in should be present in the output
11:42:14 ERROR: Rows mismatch:
11:42:14 | DIFF | COUNT | C_CUSTKEY | C_NAME | C_MKTSEGMENT |
11:42:14 | ---- | ----- | --------- | ------------------ | ------------ |
11:42:14 | + | 1 | 1 | Customer#000000001 | BUILDING |
11:42:14 | - | 1 | 1 | Customer1 | AUTOMOBILE |
11:42:14 1 of 1 FAIL 1 my_second_dbt_model_conformed_unit_test .......................... [FAIL 1 in 0.92s]

I really try to compare only the mocked data and obtain a passing results based only on that data. I saw you mentioned this here:
image

However when I go to "How", there's no explanation how you can do it, there's just an explanation of the possibilities, but not how to implement them:
image

Hi @rcepoiu , which one of the strategies described here are you using?

Make sure you have added the following overrides to your project:

{% macro ref() %}
   {{ return(dbt_unit_testing.ref(*varargs, **kwargs)) }}
{% endmacro %}

{% macro source() %}
   {{ return(dbt_unit_testing.source(*varargs, **kwargs)) }}
{% endmacro %}

This doesn't work for me either, I am seeing bpoth the data from the real source + the mocked source

@henryupton where would those overrides go? Will they affect the normal dbt model runs? Seems a big change to run unit tests..

@henryupton where would those overrides go? Will they affect the normal dbt model runs? Seems a big change to run unit tests..

Anywhere in your macros/ folder.

They maintain the existing functionality so nothing to worry about there.

Thanks @henryupton for your reply. I had one more question not related to this thread - I realised that post-hooks are not run via the unit tests, is that correct? Any chance we can make then run?

I'm closing this due to inactivity. @abi-mutinex We answered the post hooks in the other issue.