dbt-unit-testing: `ref` and `source` overrides not working when importing models from another project

Hello ! I found a bug in the ref() (and potentially also in source()) macro override. Here’s the setup to reproduce :

  • Create a dbt project project_a in which you create model_a
  • Then, create a dbt project project_b in which :
    • you create model_b that uses model_a
    • you add the dependency to dbt-unit-testing in packages.yml
    • you add the macro override for ref() and source()
  • In project_a import project_b as a package, depending on your setup, it will look like :
    packages:
      - local: "../../../project_b"
    

Now, if you try to perform a dbt ls command from project_a, you’ll have the following error :

Compilation Error
  Model 'model.project_b.model_b' (models/model_b.sql) depends on a node named 'model_a' in package or project 'project_b' which was not found

But if you remove the macro override for ref() it will work just fine.

-> The issue is that now it simply performs a look-up in the project in which the model is defined and not in the project that imports the model which contains the model in this case

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 1
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Hi @rantibi, your test file is in common or in my_project? And in which are you running the dbt test command?

It seems to have worked indeed thank’s!

Oh ok, now it’s clear, thank you! 🙂

Let me try to find a way to fix this, not sure if it’s possible, we’ll see.

Thank you again for your report.

This seems to work, but not sure if 100% correct :

{% macro ref(project_or_package, model_name) %}
  {% if running_unit_test() %}
    {% set project_or_package, model_name = setup_project_and_model_name(project_or_package, model_name) %}
    {% set node_version = kwargs["version"] | default (kwargs["v"]) %}
    {% set node = {"package_name": project_or_package, "name": model_name, "version": node_version} %}
    {{ return (ref_cte_name(node)) }}
  {% else %}
    {{ return (builtins.ref(project_or_package, **kwargs)) }}
  {% endif %}
{% endmacro %}