Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate tests found when overriding default dbt.test_unique #496

Open
2 tasks done
PatFitzner opened this issue Sep 18, 2024 · 1 comment · May be fixed by #497
Open
2 tasks done

Duplicate tests found when overriding default dbt.test_unique #496

PatFitzner opened this issue Sep 18, 2024 · 1 comment · May be fixed by #497
Assignees
Labels
bug Something isn't working triage
Milestone

Comments

@PatFitzner
Copy link

PatFitzner commented Sep 18, 2024

Describe the bug

If the unique test is overriden within a project, it follows that the variable primary_key_test_macros should be explicitly declared in the project.yml, specifying the project's own test version instead of the builtin (because our test will otherwise not be considered to cover unique key testing).

Since int_all_graph_resources is composing a list of tests from the primary_key_test_macros variable, our version of unique will be included.

However, right after the test list is composed, the line

{%- do test_macro_list.append("dbt.test_unique") -%}

adds the builtin version of unique to the list, which leads the block

{% for test in test_macro_set %}
        unioned_with_calc.macro_dependencies like '%macro.{{ test }}%' and unioned_with_calc.resource_type = 'test' as is_{{ test.split('.')[1] }}

to cause a name collision, since both our_project.test_unique and dbt.test_unique return the same value after test.split('.')[1].

Steps to reproduce

In a dbt project (let's use jaffle-shop-classic as an example), install the dbt_project_evaluator package, and run it

dbt run -s package:dbt_project_evaluator 

to ensure that it is working properly.

Create a custom version of the unique test as jaffle-shop-classic/tests/test_unique.sql, such as:

{% test unique(model, column_name) %}

{{ config(severity = 'warn') }}

select
    {{ column_name }} as unique_field,
    count(*) as n_records
from 
    {{ model }}
where 
    `{{ column_name }}` is not null
group by 
    `{{ column_name }}`
having 
    count(*) > 1

{% endtest %}

and add the variable

primary_key_test_macros: [["jaffle_shop.test_unique", "dbt.test_not_null"],["dbt_utils.test_unique_combination_of_columns"]]

to your dbt_project.yml .

Run

dbt run -s package:dbt_project_evaluator 

Expected results

The package builds its models and the results are leveraging our custom test instead of the built-in one.

Actual results

We receive a

Database Error in model int_all_graph_resources (models/marts/core/int_all_graph_resources.sql)
  column "is_test_unique" specified more than once

because int_all_graph_resources is appending the builtin unique test to the test_macro_list, causing a name collision with our test.

System information

packages:
  - package: dbt-labs/dbt_project_evaluator
    version: 0.14.2

Which database are you using dbt with?

  • postgres
  • bigquery

The output of dbt --version:

Core:
  - installed: 1.8.6
  - latest:    1.8.6 - Up to date!

Plugins:
  - postgres: 1.8.2 - Up to date!

Additional context

The problem does not reproduce if we override other tests (such as not_null), or if the

{%- do test_macro_list.append("dbt.test_unique") -%}

line is removed.

Are you interested in contributing the fix?

UPDATE: a PR has been submitted!

@PatFitzner PatFitzner added bug Something isn't working triage labels Sep 18, 2024
@PatFitzner
Copy link
Author

The removal of that line produces identical int_all_graph_resources models, both in jaffle-shop-classic run locally on PostgreSQL, as in our production datawarehouse running on BigQuery.

I have opened a PR with a fix, which tries to maintain my best guess of the intended functionality of the line.

@dave-connors-3 dave-connors-3 added this to the v1.0.0 milestone Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
3 participants