Built-in tests help check your data for common problems automatically. They make sure your data is clean and reliable.
Built-in tests (unique, not_null, accepted_values, relationships) in dbt
Start learning this pattern below
Jump into concepts and practice - no test required
tests:
- unique:
column_name: your_column
- not_null:
column_name: your_column
- accepted_values:
column_name: your_column
values: [allowed_value1, allowed_value2]
- relationships:
column_name: your_column
to:
table: other_table
column: other_columnEach test is defined under the tests key in your model's YAML file.
You specify the column to test and any extra parameters like allowed values or related tables.
id column has no duplicate values.tests:
- unique:
column_name: idemail column has no missing values.tests:
- not_null:
column_name: emailstatus only contains these three allowed values.tests:
- accepted_values:
column_name: status
values: ['active', 'inactive', 'pending']user_id in this table exists in the users.id column.tests:
- relationships:
column_name: user_id
to:
table: users
column: idThis YAML config adds built-in tests to the orders model. It checks that order_id is unique and not null, customer_id is not null and exists in the customers table, and status only has allowed values.
version: 2 models: - name: orders columns: - name: order_id tests: - unique - not_null - name: customer_id tests: - not_null - relationships: to: table: customers column: id - name: status tests: - accepted_values: values: ['pending', 'shipped', 'delivered', 'cancelled']
Built-in tests are easy to add and help catch data issues early.
Tests run automatically when you run dbt test.
Failing tests mean you should check your data or logic.
Built-in tests check common data quality issues like uniqueness and missing values.
You add tests in your model YAML files under the tests section.
Running dbt test runs all tests and shows if your data is healthy.
Practice
unique test in dbt check for in a column?Solution
Step 1: Understand the purpose of the unique test
The unique test ensures that each value in the specified column appears only once, meaning no duplicates.Step 2: Compare with other test types
Other tests like not_null check for missing values, accepted_values check for allowed values, and relationships check for foreign key matches.Final Answer:
It checks that all values in the column are different with no duplicates. -> Option DQuick Check:
unique test = no duplicates [OK]
- Confusing unique with not_null test
- Thinking unique checks accepted values
- Mixing unique with relationships test
not_null test on the column user_id in a dbt model YAML file?Solution
Step 1: Recall YAML structure for dbt tests
Tests are added under the columns list, each column has a name and a tests list with test names.Step 2: Identify correct indentation and keys
columns: - name: user_id tests: - not_null correctly uses 'name' for the column and 'tests' as a list with '- not_null'. Other options have wrong keys or structure.Final Answer:
columns: - name: user_id tests: - not_null -> Option AQuick Check:
YAML tests under columns with name and tests list [OK]
- Using 'test' instead of 'tests'
- Incorrect indentation breaking YAML
- Placing tests outside columns section
columns:
- name: status
tests:
- accepted_values:
values: ['active', 'inactive', 'pending']
What happens if the status column contains the value 'deleted' when you run dbt test?Solution
Step 1: Understand accepted_values test behavior
The accepted_values test checks if all column values are within the specified list.Step 2: Check if 'deleted' is in the list
'deleted' is not in ['active', 'inactive', 'pending'], so the test will fail.Final Answer:
The test fails because 'deleted' is not in the accepted values list. -> Option BQuick Check:
accepted_values rejects values outside list [OK]
- Assuming test passes if value is a string
- Confusing accepted_values with not_null
- Thinking test skips unknown values
columns:
- name: order_id
tests:
- relationships:
to: ref('orders')
But running dbt test gives an error. What is the most likely cause?Solution
Step 1: Understand relationships test syntax
The relationships test requires both 'to' (target table) and 'field' (target column).Step 2: Identify the error cause
The YAML is missing the 'field' key, causing a configuration error when running dbt test.Final Answer:
The 'field' key is missing in the relationships test. -> Option AQuick Check:
relationships 'to' + 'field' required [OK]
- Using ref() in YAML instead of table name string
- Omitting the 'field' key
- Assuming 'field' must match column name
customer_id column in your orders model is unique, not null, and only contains values that exist in the customers table's id column. Which combination of built-in tests should you add in your YAML?Solution
Step 1: Identify tests for uniqueness and non-null
Use 'unique' to ensure no duplicates and 'not_null' to prevent missing values.Step 2: Ensure foreign key relationship
Use 'relationships' test with 'to' as 'customers' table and 'field' as 'id' to check existence.Step 3: Verify other options
Options B, C, and D misuse accepted_values or mix concepts incorrectly.Final Answer:
- unique - not_null - relationships: to: customers field: id -> Option CQuick Check:
unique + not_null + relationships = correct tests [OK]
- Using accepted_values to check null or uniqueness
- Misconfiguring relationships test
- Missing one of the required tests
