Unit testing dbt models - Time & Space Complexity
When we run unit tests on dbt models, we want to know how the time it takes grows as our data or tests grow.
We ask: How does testing time change when we add more data or more tests?
Analyze the time complexity of the following dbt test code snippet.
-- Simple uniqueness test on a model column
select
id,
count(*) as count
from {{ ref('my_model') }}
group by id
having count > 1
This test checks if the 'id' column in the model has duplicate values.
Look for repeated work in the test query.
- Primary operation: Scanning all rows of the model to group by 'id'.
- How many times: Once over the entire dataset for grouping and counting.
As the number of rows in the model grows, the grouping and counting take more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 rows scanned and grouped |
| 100 | About 100 rows scanned and grouped |
| 1000 | About 1000 rows scanned and grouped |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the test time grows linearly as the data size increases.
[X] Wrong: "Unit tests run instantly no matter how big the data is."
[OK] Correct: The test scans all data rows, so bigger data means longer test time.
Understanding how test time grows helps you write efficient tests and explain your choices clearly.
"What if we added multiple columns to test uniqueness on? How would the time complexity change?"
Practice
Solution
Step 1: Understand unit testing in dbt
Unit testing in dbt is designed to verify that data models behave as expected and contain correct data.Step 2: Identify the main goal
The main goal is to check correctness and quality of data models, not to create or deploy them.Final Answer:
To check data models for correctness and quality -> Option AQuick Check:
Unit testing = correctness and quality check [OK]
- Confusing testing with model creation
- Thinking tests deploy models
- Assuming tests speed up SQL writing
Solution
Step 1: Recall YAML test syntax in dbt
Tests are listed as a YAML list under the 'tests' key, each with a dash and test name.Step 2: Match correct indentation and list format
tests: - unique: column_name shows 'tests:' followed by a list item '- unique: column_name', which is correct YAML syntax for dbt tests.Final Answer:
tests: - unique: column_name -> Option AQuick Check:
YAML lists use dash (-) before items [OK]
- Missing dash (-) before test name
- Incorrect indentation
- Placing test name and column incorrectly
columns:
- name: user_id
tests:
- unique
- not_nullWhat will
dbt test check for the user_id column?Solution
Step 1: Read the tests listed for user_id
The YAML shows two tests: 'unique' and 'not_null' for the user_id column.Step 2: Understand what each test does
'unique' ensures no duplicate values; 'not_null' ensures no missing values.Final Answer:
It checks that user_id values are unique and not null -> Option BQuick Check:
unique + not_null = no duplicates, no nulls [OK]
- Thinking tests check opposite conditions
- Assuming only one test runs
- Confusing unique with not_null
columns:
- name: order_id
tests:
unique
not_nullBut when you run
dbt test, it fails with a syntax error. What is the likely cause?Solution
Step 1: Check YAML list syntax for tests
Each test under 'tests' must be a list item starting with a dash (-).Step 2: Identify missing dashes
The snippet shows 'unique' and 'not_null' without dashes, causing YAML syntax error.Final Answer:
Missing dash (-) before 'unique' and 'not_null' tests -> Option DQuick Check:
YAML lists need dash (-) before each item [OK]
- Omitting dash (-) before tests
- Wrong indentation levels
- Confusing 'tests' with 'test' key
email column in your users model is always unique and never empty. Which YAML test configuration correctly applies these tests and how would you run them?Solution
Step 1: Write correct YAML test syntax
The tests must be listed as a YAML list with dashes under 'tests' for the 'email' column, as shown in Define in YAML:columns: - name: email tests: - unique - not_null
Then rundbt test.Step 2: Choose the correct dbt command to run tests
To execute tests, you must rundbt test, notdbt run. So this configuration withdbt testis correct.Final Answer:
Define tests with dashes and run dbt test -> Option CQuick Check:
YAML list + dbt test command = correct testing [OK]
- Running 'dbt run' instead of 'dbt test'
- Incorrect YAML list syntax
- Misnaming column key
Ready to prove it?
Pass the test and this pattern moves unknown
