0
0
dbtdata~20 mins

Built-in tests (unique, not_null, accepted_values, relationships) in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
dbt Built-in Tests Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a unique test on a column with duplicates
Given a dbt model with the following data in the users table:

id | name
---|------
1  | Alice
2  | Bob
2  | Charlie
3  | David

What will be the result of running a unique test on the id column?
dbt
version: 2
models:
  - name: users
    columns:
      - name: id
        tests:
          - unique
AThe test will fail because the <code>id</code> value 2 appears twice.
BThe test will pass because all <code>id</code> values are integers.
CThe test will fail because the <code>name</code> column has duplicates.
DThe test will pass because there are no null values in <code>id</code>.
Attempts:
2 left
💡 Hint
Think about what the unique test checks for in a column.
data_output
intermediate
2:00remaining
Result of a not_null test on a column with null values
Consider a products table with this data:

product_id | price
----------|-------
101       | 9.99
102       | NULL
103       | 15.00

What will be the result of running a not_null test on the price column?
dbt
version: 2
models:
  - name: products
    columns:
      - name: price
        tests:
          - not_null
AThe test will pass because most values in <code>price</code> are numbers.
BThe test will fail because there is a NULL value in <code>price</code>.
CThe test will pass because <code>product_id</code> is unique.
DThe test will fail because <code>price</code> contains decimal numbers.
Attempts:
2 left
💡 Hint
Remember what the not_null test checks for.
🧠 Conceptual
advanced
1:30remaining
Purpose of accepted_values test in dbt
What is the main purpose of the accepted_values test in dbt?
ATo ensure that no values in a column are NULL.
BTo check if all values in a column are unique.
CTo verify that all values in a column belong to a predefined list of allowed values.
DTo confirm that foreign key relationships exist between tables.
Attempts:
2 left
💡 Hint
Think about validating categorical or enumerated data.
Predict Output
advanced
2:00remaining
Output of a relationships test with missing foreign key
Given two tables:

orders:
order_id | customer_id
---------|------------
1        | 10
2        | 20
3        | 30

customers:
customer_id
-----------
10
20

What will be the result of running a relationships test on orders.customer_id referencing customers.customer_id?
dbt
version: 2
models:
  - name: orders
    columns:
      - name: customer_id
        tests:
          - relationships:
              to: ref('customers')
              field: customer_id
AThe test will fail because <code>customer_id</code> 30 in <code>orders</code> does not exist in <code>customers</code>.
BThe test will fail because <code>orders</code> has duplicate <code>order_id</code> values.
CThe test will pass because all <code>customer_id</code> values exist in <code>customers</code>.
DThe test will pass because <code>customers</code> has no NULL values.
Attempts:
2 left
💡 Hint
Check if all foreign keys in orders exist in customers.
🔧 Debug
expert
2:30remaining
Identify the error in this dbt test configuration
Look at this dbt test configuration snippet:

version: 2
models:
  - name: sales
    columns:
      - name: region
        tests:
          - accepted_values:
              values: ["North", "South", "East", "West"]
          - relationships:
              to: ref('regions')
              field: region_id

What is the main issue that will cause a test failure or error?
AThe <code>accepted_values</code> test is missing the <code>field</code> key.
BThe <code>ref</code> function cannot be used inside tests.
CThe <code>values</code> list in <code>accepted_values</code> contains invalid strings.
DThe <code>relationships</code> test references <code>region_id</code> but the column tested is <code>region</code>.
Attempts:
2 left
💡 Hint
Check if the columns referenced in tests match the column names defined.