sdf test
runs this query, it will check if any data is returned and fail if so.
Working set 1 model file, 1 test file, 1 .sdf file
Running hello.pub.main (./models/main.sql)
Testing hello.pub.test_main (./sdftarget/dbg/tests/hello/pub/test_main.sql)
Finished 1 model [1 succeeded], 1 test [1 passed] in 1.753 secs
[Pass] Test hello.pub.test_main
x > 100
)sum(x) > 100
)Test Result | Meaning |
---|---|
🟡 | Warning |
🔴 | Error |
cool_column
.
Test Name | Type | Description |
---|---|---|
not_null() | Scalar | Column elements must not be null |
valid_scalar(condition) | Scalar | Column elements all meet scalar condition |
valid_aggregate(condition) | Aggregate | Column elements all meet aggregate condition |
unique() | Aggregate | Column elements must be unique |
in_accepted_values([values]) | Aggregate | Column values are all elements of array |
minimum(value) | Aggregate | Column can not have values less than value |
maximum(value) | Aggregate | Column can not have values greater than value |
exclusive_minimum(value) | Aggregate | Column can not have values less or equal to value |
exclusive_maximum(value) | Aggregate | Column can not have values greater than or equal to value |
between(lower, upper) | Aggregate | Column values must be between lower and upper |
max_length(value) | Aggregate | Column values (as strings) are greater than a given length |
min_length(value) | Aggregate | Column values (as strings) are less than a given length |
like(string) | Aggregate | Column elements must contain string |
try_cast(type) | Aggregate | Column elements must include string |
primary_key(column) | Aggregate | Column is primary key of column |
Test Name | Type | Description |
---|---|---|
unique_columns([c1, c2]) | Table | Table with columns c1 and c2 has unique values across these two columns |
sdf_tests
, our open-source tests library, is is available on GitHub here.Prerequisite
sdf_tests
uses Jinja macros. Therefore, setting Jinja
as a preprocessor is required.Create the sample workspace tests
:workspace.sdf.yml
, we have Jinja set as the default preprocessor:Add Tests
src_metadata/raw_inapp_events.sdf.yml
.
In the file, find the three commented out tests and uncomment them. They should look like this:Run Tests
Working set 1 model file, 1 test file, 2 .sdf files
Running tests_workspace.pub.raw_inapp_events (./models/raw_inapp_events.sql)
Testing tests_workspace.pub.test_raw_inapp_events (./sdftarget/dbg/tests/tests_workspace/pub/test_raw_inapp_events.sql)
Finished 1 model [1 succeeded], 1 test [1 passed] in 1.796 secs
[Pass] Test tests_workspace.pub.test_raw_inapp_events
Inspect the Test
sdftarget/dbg/preprocessed/tests_workspace/pub/test_raw_inapp_events.sql
.The file will hold the compiled tests query and will look like this:WITH
raw_inapp_events_constraints AS (
SELECT
CASE WHEN COUNT(DISTINCT event_id) != COUNT(event_id)
THEN
‘err: column event_id is NOT unique’ ELSE NULL END AS event_id_1
,
CASE WHEN COUNT(CASE WHEN event_id IS NULL THEN 1 ELSE NULL END) > 0
THEN
‘err: column event_id has NULL values’ ELSE NULL END AS event_id_2
,
CASE WHEN COUNT(CASE WHEN event_name NOT IN (‘install’, ‘add_to_cart’, ‘go_to_checkout’, ‘place_order’) THEN 1 ELSE NULL END) > 0
THEN
‘wrn: column event_name has values outside accepted values ”install”, ”add_to_cart”, ”go_to_checkout”, ”place_order''' ELSE NULL END AS event_name_1
,
CASE WHEN NOT(MIN(event_value) >= 0) or MAX(event_value) > 1000
THEN
‘wrn: column event_value has values outside of 0..1000’ ELSE NULL END AS event_value_1
FROM tests_workspace.pub.raw_inapp_events
)SELECT reason
FROM (
SELECT event_id_1 as reason FROM raw_inapp_events_constraints WHERE event_id_1 IS NOT NULL
UNION ALL
SELECT event_id_2 as reason FROM raw_inapp_events_constraints WHERE event_id_2 IS NOT NULL
UNION ALL
SELECT event_name_1 as reason FROM raw_inapp_events_constraints WHERE event_name_1 IS NOT NULL
UNION ALL
SELECT event_value_1 as reason FROM raw_inapp_events_constraints WHERE event_value_1 IS NOT NULL
) AS _combined_errors
ORDER BY reason;
Fail a Test
src_metadata/raw_inapp_events.sdf.yml
, edit the test associated with the column event_value
and reduce the maximum in the range to be 10 instead of 1000:warning: SDF2001: tests_workspace.pub.raw_inapp_events: attempting to execute Trino, local execution is currently experimental
—> models/raw_inapp_events.sql:1:1… and 1 more warnings not shown
Create a New Workspace
generic_test_example
Write a Generic Test
generic_test.sql
in a new directory called tests
:Configure Generic Tests
workspace.sdf.yml
to include our new generic test.
To do so, add - path: tests
to your workspace.sdf.yml
includes block like so:- path: tests
includes item does not need the type: test
property since naming your directory tests
defaults
the include type to test
. For a list of all default include types, check out our includes block documentationRun the Test
warning: SDF2001: hello.pub.generic_test: attempting to execute Trino, local execution is currently experimental
—> tests/generic_test.sql:1:1
Setup
custom_tests
.The new directory will look like this: Cleaned sdftarget
Finished clean in 0.316 secs
.
├── custom_tests
├── models
│ └── raw_inapp_events.sql
├── seeds
│ └── inapp_events.csv
├── src_metadata
│ └── raw_inapp_events.sdf.yml
└── workspace.sdf.yml5 directories, 4 files
workspace.sdf.yml
file:Create a Custom Test
my_test.jinja
with the following custom test in the newly created folder:Try It!
src_metadata/raw_inapp_events.sdf.yml
, add our new custom test
to the column event_name
:{sdf_workspace_name}.{custom_test_name}
. In the example above, the workspace name
is tests_workspace
and the custom_test_name is custom_column_test
./bin/sh: 1: sdf: not found
sdftarget/dbg/preprocessed/tests_workspace/pub/test_raw_inapp_events.sql
.
Notice our custom logic in the compiled query:cat: sdftarget/dbg/preprocessed/tests_workspace/pub/test_raw_inapp_events.sql: No such file or directory