using vargs in custom tests

The problem I’m having

I’d like to extend the current ‘unique’ test to be able to take a variable list of parameters so it can test if the concatenation of the list of columns is unique i.e. col1 || col2 || col3

The context of why I’m trying to do this

I have tables that need to have this assertion tested.

What I’ve already tried

looking through the docs, it seems custom tests must take named arguements atm.

Some example code or error messages

N/A

This already exists in the dbt-utils package: https://github.com/dbt-labs/dbt-utils#unique_combination_of_columns-source

Note: @Owen originally posted this reply in Slack. It might not have transferred perfectly.

1 Like

Thanks <@U0291GP2AQ0>, I was doing it in a more complex way :sweat_smile:

Note: @Bruno de Lima originally posted this reply in Slack. It might not have transferred perfectly.

oh nice! I didn’t realise that!! below is what I tried but am now getting a compilation error complaining about the macro takes no keyword argument ‘column_name’ which I can’t control… ideas?

  - name: METRIC_NAME
    tests:
      - dbt_utils.unique_combination_of_columns:
          combination_of_columns:
            - GROUP_NAME
            - METRIC_NAME
          quote_columns: false

Compilation Error in test dbt_utils_unique_combination_of_columns_SEED_METRIC_LIST_CNMS_METRIC_NAME__GROUP_NAME__METRIC_NAME__False (seeds/Metadata/metadata.yml)
macro ‘dbt_macro__test_unique_combination_of_columns’ takes no keyword argument ‘column_name’

Try to define the test below the name of the seed, not below the name of the column

Like this:

version: 2

seeds:
  - name: your_seed
    tests:
      - dbt_utils.unique_combination_of_columns:
          combination_of_columns:
            - GROUP_NAME
            - METRIC_NAME
    columns:
      - name: METRIC_NAME

just check the indentation.

Just explaining a little bit more:

  • When you put the test below the model/seed name, dbt assumes you are passing to the test an arg called “model” with the name of your model/seed.
  • When you put the test below the column name, dbt assumes you are passing to the test two args, model like before, and column_name, which is the name of your column.

This specific test does not accept a column_name arg, because it is not designed for a specific column, but a group of them. This group of columns is passed by a custom arg called “combination_of_columns” that is why you have to specify it in the .yml.

2 Likes

thank you so much!! much appreciated.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.