Skip to content

Running a macro not within a model (e.g. using dbt run-operation) will fail because macros have no "tags" #151

@wircho

Description

@wircho

Overview

We have a project where we sometimes run dbt run-operation some_macro and the some_macro being run uses ref somewhere inside it. This ends up calling dbt_unit_testing's running_unit_test(), which checks for tags in the config.

This is fine when config is a model's config. But it does not work when no model is being compiled, because in the latter scenario, config has no "tags" at all.

How to reproduce

In a project set up with dbt_unit_testing run:

> dbt run-operation ref --args "model_name: any_existing_model"
...
  argument of type 'NoneType' is not iterable
...

Suggestion 1

My recommendation is to edit this macro:

{% macro running_unit_test() %}
{{ return ('unit-test' in config.get('tags')) }}
{% endmacro %}

to this:

{% macro running_unit_test() %}
  {{ return (config.model.resource_type.value == 'model' and 'unit-test' in config.get('tags')) }}
{% endmacro %}

Suggestion 2

A simpler (and arguably safer) solution is:

{% macro running_unit_test() %}
  {{ return (config.get('tags') and 'unit-test' in config.get('tags')) }}
{% endmacro %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions