Tests
In order to ensure models are correct, Quary provides testing tools that allow you to build confidence in your models. Testing provides assurance that your models are correct and that they will continue to be correct as you make changes. Although testing may initially feel like a burden, over time the confidence far outweighs the cost of writing tests.
Running tests
CLI
Testing in the CLI can be done by using the following command.
Visual Studio Code Extension
Testing in the Visual Studio Code extension can be done running the QUARY: Test
command in the Command Palette (Ctrl+Shift+P
/Cmd+Shift+P
).
Writing tests
Standard column tests
In addition to writing some SQL tests, Quary also provides the facility to write tests in project files for some standard tests that apply to columns in the model.
Not null
Asserting id
in products
is not null can be done as following:
Unique
Asserting id
in products
is unique can be done as following:
Relationship
Asserting id
in products
is a foreign key to product_catalog
can be done as following:
Accepted Values
Asserting type
in products
is in a particular set of accepted values can be done as following:
Less Than
Asserting price_in_usd
in products
is less than 100 can be done as following:
Less Than Or Equal
Asserting price_in_usd
in products
is less than or equal to 100 can be done as following:
Greater Than
Asserting price_in_usd
in products
is greater than 100 can be done as following:
Greater Than Or Equal
Asserting price_in_usd
in products
is greater than or equal to 100 can be done as following:
Standard model tests
In addition to writing some tests that apply to columns, Quary also provides the facility to write tests in project files for some standard tests that apply to models.
Multi column unique
Asserting that name
and price_in_usd
in products
is unique can be done as following:
SQL tests
Tests can be added to the tests
directory. The tests are written in the SQL language and are assertions that if
correct, return an empty result set. For example, suppose we have a list of products that looks like following:
id | time_created | name | price_in_usd |
1 | 2023-05-05T07:22:35Z | Tooth Brush | 1.99 |
2 | 2023-05-05T07:22:35Z | Tooth Paste | 2.99 |
The schema is represented by the following SQL:
A simple assertion that we could foresee is that we don't expect any prices to be negative. So we could write a test that looks as following:
If we save this file as tests/price_in_usd.sql
, this will check that no prices are below 0.
Note the direction of the inequality sign. Your assertions should return incorrect values, in the case where all is right the assertion should return an empty result set.