Search code examples
jinja2dbt

test not recognized in dbt


I have written a generic test in dbt: is_postive.sql

{% test is_postive(model, column_name) %}

select 
    {{ column_name }}
from {{ model }} 
where {{ column_name }} < 0

{% endtest %}

This test is located in the tests/generic folder in my dbt project. The dbt project yaml file sets the tests location to this folder test-paths: ["tests"]

I am trying to reference this test in a yaml file for a model:

models:
  - name: fct__monthly_revenue
    tests:
      - unique:
          column_name: "id || '-' || date"
    config:
      schema: analytics
    columns: 
      - name: revenue
        tests:
          - is_positive:
              severity: warn

however, when I try to build the model I get this error:

Compilation Error in test is_positive_fct__monthly_revenue_revenue (models/analytics/fpaa/fpaa_revenue.yml)
  'test_is_positive' is undefined. This can happen when calling a macro that does not exist. Check for typos and/or install package dependencies with "dbt deps".

Some things I have tried already:

  • changing the file name to test_is_positive.sql
  • originally had the test directly in the test folder instead of the generic sub-folder

And yes I do know that I could use the accepted range from dbt_utils but I'd like to build more custom generic tests and so I'm trying to get a relatively simple one to work first


Solution

  • ugh, sorry turns out it was a typo in my test sql file:

    {% test is_postive(model, column_name) %} should have been {% test is_positive(model, column_name) %}

    Thanks Chiến Lê and Aleix CC for helping me notice that. Not marking those answers as correct since dbt is agnostic as to whether I call the file is_positive.sql or test_is_positive.sql, and whether I have config: prior to severity: