Utilizing dbt_utils or dbt_expectations macros inside test macros

I’m basically trying to create a Data Quality macro that uses some great expectations macros, ala:

{% test primary_key_test(model, columns) %}
    {%- for column in columns %}
        {{ dbt_expectations.expect_column_values_to_not_be_null(model, column) }}
        {{ dbt_expectations.expect_column_values_to_be_unique(model, column) }}
    {% endfor %}
{% endtest %}

It errors when testing with with “expect_column_values_to_not_be_null is undefined”. It generates a .sql file for the test in target>run>models>schema.yml but it is just the wrapper, not the actual .sql test.

Calling dbt_expectations.expect_column_values_to_not_be_null in the schema.yml as a test works fine.

Had the same issue trying to utilize macros from dbt_utils in my tests.

Any insight?

I don’t think you can call tests like macros. You can only call macros.

Take this example here:

{% test expression_is_true(model,
                                 expression,
                                 test_condition="= true",
                                 group_by_columns=None,
                                 row_condition=None
                                 ) %}

    {{ dbt_expectations.expression_is_true(model, expression, test_condition, group_by_columns, row_condition) }}

{% endtest %}

{% macro expression_is_true(model,
                                 expression,
                                 test_condition="= true",
                                 group_by_columns=None,
                                 row_condition=None
                                 ) %}
    {{ adapter.dispatch('expression_is_true', 'dbt_expectations') (model, expression, test_condition, group_by_columns, row_condition) }}
{%- endmacro %}

The test expression_is_true is calling the macro expression_is_true.

This macro you are trying to call does not exist. What exists is a test with this name that calls the macro expresion_is_true

{% test expect_column_values_to_not_be_null(model, column_name, row_condition=None) %}

{% set expression = column_name ~ " is not null" %}

{{ dbt_expectations.expression_is_true(model,
                                        expression=expression,
                                        group_by_columns=None,
                                        row_condition=row_condition
                                        )
                                        }}
{% endtest %}

Lastly, a test should return a single select statement that can return records or not.