0
0
dbtdata~10 mins

Built-in tests (unique, not_null, accepted_values, relationships) in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in tests (unique, not_null, accepted_values, relationships)
Start with dbt model data
Apply unique test on column
Apply not_null test on column
Apply accepted_values test on column
Apply relationships test between tables
Collect test results
Pass or Fail report
This flow shows how dbt runs built-in tests on data columns and relationships, then reports if data passes or fails.
Execution Sample
dbt
version: 2
models:
  - name: customers
    columns:
      - name: customer_id
        tests:
          - unique
          - not_null
      - name: status
        tests:
          - accepted_values:
              values: ['active', 'inactive', 'pending']
This dbt YAML config runs unique and not_null tests on customer_id and accepted_values test on status.
Execution Table
StepTest TypeColumn/TableCondition CheckedResultAction
1uniquecustomer_idAll values are unique?TruePass
2not_nullcustomer_idNo null values?TruePass
3accepted_valuesstatusValues in ['active','inactive','pending']?FalseFail - found 'deleted'
4relationshipsorders.customer_id -> customers.customer_idAll foreign keys exist in parent?TruePass
5Summary-All tests checked1 Fail, 3 PassReport results
💡 All tests run; one accepted_values test failed due to unexpected value.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
unique_test_customer_idNot runTrueTrueTrueTrueTrue
not_null_test_customer_idNot runNot runTrueTrueTrueTrue
accepted_values_test_statusNot runNot runNot runFalseFalseFalse
relationships_test_orders_customer_idNot runNot runNot runNot runTrueTrue
test_summaryEmptyPassPassFailFail1 Fail, 3 Pass
Key Moments - 3 Insights
Why did the accepted_values test fail even though most values looked correct?
The accepted_values test checks every value strictly against the allowed list. The failure happened because one value ('deleted') was not in the allowed list ['active', 'inactive', 'pending'], as shown in step 3 of the execution_table.
Does the unique test check for null values?
No, the unique test only checks if all values are distinct. Null values are handled separately by the not_null test, as seen in steps 1 and 2.
What does the relationships test verify?
It verifies that all foreign key values in the child table exist in the parent table. Step 4 shows it passed because all orders.customer_id values matched customers.customer_id.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What caused the accepted_values test to fail?
ADuplicate values were found
BThere were null values in the column
CA value outside the allowed list was found
DForeign key mismatch occurred
💡 Hint
Check the 'Result' and 'Action' columns in step 3 of the execution_table.
According to variable_tracker, what is the final result of the relationships test?
AFalse
BTrue
CNot run
DPartial
💡 Hint
Look at the 'relationships_test_orders_customer_id' row in variable_tracker under 'Final' column.
If the not_null test on customer_id failed, which step in the execution_table would show that?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Refer to the 'Test Type' column to find where not_null test is executed.
Concept Snapshot
dbt built-in tests check data quality:
- unique: all values distinct
- not_null: no nulls allowed
- accepted_values: values in allowed list
- relationships: foreign keys exist in parent
Tests run on columns or tables and report pass/fail.
Full Transcript
This visual execution shows how dbt runs built-in tests on data columns and relationships. First, it checks if values in a column are unique, then if there are no null values. Next, it verifies if column values are within an accepted list. Finally, it checks relationships between tables to ensure foreign keys exist in the parent table. The execution table traces each test step, showing conditions checked and results. Variable tracker records test outcomes after each step. Key moments clarify common confusions like why accepted_values test fails or what relationships test means. The quiz asks questions about specific steps and results to reinforce learning. The snapshot summarizes the main tests and their purpose.