Search code examples
pythonsqlsnowflake-cloud-data-platformjinja2dbt

How to test Data Bulid Tools(DBT) macros in DBT


I have Data Build Tools (BDT) macro for Snowflake tables in aws and the snowflake table table name is source.customer. The macro is here:

{% macro get_customer(customer_status='active') -%}

 {{ source("source", "customer") }}
 WHERE 
 
{% if customer_status.lower() == 'active' -%}

     customerdesc = 'Existing' and customer_status = 0

{%- endif -%}
{%- endmacro %}

I add tests folder in dbt_project.yml file and create a test cases store under the tests folder. my test cases is here:

   {% test get_customer_active %}
expect query
with expected as (
    select 'Existing' as customerdesc, 0 as customer_status
)
{{ get_customer('active') }}
union all
select * from expected;

I run the test case at this command:

dbt test --select get_customer_active

The test run is success, and I get this message "The selection criterion 'get_customer_active' does not match any nodes"

Actually, the test case does not test the macor.

Question: How to test DBT macro?


Solution

  • We can use the following queries to verify the macro

    select customer_status,customerdesc
    from get_customer('status')
    

    replace status with the following status

    get_customer()

    get_customer('any_string')

    get_customer('active')

    get_customer('ACtive')