Write dbt test for positive values
Asked Answered
M

4

13

Is there an easy way to write a test for a column being positive in dbt?

accepted_values doesn't seem to work for continuous variables.

I know you can write queries in ./tests but it looks like an overkill for such a simple thing.

Mulct answered 1/7, 2020 at 11:17 Comment(0)
S
21

You could use dbt_utils.expression_is_true

version: 2

models:
  - name: model_name
    tests:
      - dbt_utils.expression_is_true:
          expression: "col_a > 0"
Seismo answered 1/7, 2020 at 18:21 Comment(1)
Solid answer. Slight addition: if you are adding a test like this to a specific column in the model then you wouldn't include the column name. So the test here would simply be expression: "> 0"Manure
V
12

Previous answer is correct. Another option is accepted_range:

version: 2

models:
  - name: model_name
    columns:
      - name: user_id
        tests:
          - dbt_utils.accepted_range:
              min_value: 0
              inclusive: false
Via answered 21/3, 2023 at 8:27 Comment(0)
C
3

I think the dbt_utils suggestion is good, the only reasonable alternative I can think of is writing a custom schema test:

https://docs.getdbt.com/docs/guides/writing-custom-schema-tests/

But why bother when you can just use expression_is_true @jake

Chancemedley answered 2/7, 2020 at 15:7 Comment(0)
N
2

Another way with dbt-expectations:

version: 2

models:
  - name: model_name
    columns:
      - name: user_id
        tests:
          - dbt_expectations.expect_column_values_to_be_between:
              min_value: 0
              strictly: true
Natalia answered 11/1 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.