Skip to main content

Ontology Unit Tests (Ontology Readiness)

Ontology unit tests let you check automatically that a knowledge graph behaves as expected before it is queried by users or handed to a data agent. A test suite is a named collection of tests. Each test pairs a Timbr SQL statement with an expectation: the statement must run without error, or its result must match an expected value. Suites are bound to ontologies and run against them, on demand or on a schedule.

This page is the SQL reference for test suites. The same suites are managed in the UI through Ontology Readiness, which turns each run into a READY / NOT READY verdict per ontology.

Why it matters

A change to a source table, a mapping or a relationship can silently break the queries that users and agents depend on. A readiness suite turns "the ontology still works" into something you can check: run it after every change and on a schedule, and gate promotions on the result.


Typical workflow​

StepWhat you doStatement
1. CreateCreate an empty suiteCREATE TESTSUITE
2. Add testsAdd tests, each with a query and an assertionALTER TESTSUITE ... ADD TEST
3. BindBind the suite to the ontologies it should checkALTER ONTOLOGY ... ADD TESTSUITE
4. RunRun the suite against the ontology you are connected toRUN TESTSUITE
5. ScheduleRun it on a recurring scheduleCREATE JOB ... AS RUN TESTSUITE
6. ReviewRead the resultstimbr.sys_testsuite_history

The examples on this page use a sample sales ontology named sales with the concepts customer and order.


Create a test suite​

CREATE TESTSUITE creates an empty suite. Tests are added afterwards with ALTER TESTSUITE.

CREATE [OR REPLACE] TESTSUITE <suite_name> [DESCRIPTION '<description>'];

Example:

CREATE TESTSUITE prod_gate DESCRIPTION 'Pre-promotion gate for the sales graph';

CREATE OR REPLACE on an existing suite updates its description in place:

CREATE OR REPLACE TESTSUITE prod_gate DESCRIPTION 'Pre-promotion gate for the sales graph (v2)';

Test suites are not stored inside an ontology. A suite is defined once and can be bound to several ontologies, for example the same checks for a sales_test and a sales_prod ontology.


Add, update and drop tests​

Add a test​

ALTER TESTSUITE <suite_name>
ADD TEST <test_name>
OPTIONS ( option_name = 'value' [, ...] )
AS <timbr_sql_statement>;

The statement after AS is any Timbr SQL statement that can run against the ontology: a SELECT over concepts, views or cubes, or a validation statement such as VALIDATE ONTOLOGY.

Test options​

OptionRequiredDescription
assertion✓What is checked against the statement's result. See Assertions.
operatorFor scalar and row_countComparison operator: =, !=, >, >=, <, <=. Not allowed with no_error.
expectedFor scalar and row_countThe expected value, compared to the actual value with operator.
expected_is_sql'true' to evaluate expected as a SQL expression (for example current_date, or a subquery) instead of a literal. Default 'false'.
descriptionRecommendedWhat the test checks and why. Shown in the UI next to the test.

Option values are written as quoted strings.

Assertions​

AssertionPasses whenNeeds operator / expected
no_errorThe statement runs without an errorNo
scalarThe single value returned by the statement compares to expected using operatorYes
row_countThe number of rows returned compares to expected using operatorYes
emptyThe statement returns no rowsNo
not_emptyThe statement returns at least one rowNo

Invalid combinations are rejected when the test is added, and nothing is saved. For example, scalar without operator and expected, an operator on a no_error test, or an unknown assertion name.

Examples​

The ontology compiles cleanly:

ALTER TESTSUITE prod_gate ADD TEST ontology_structure_valid OPTIONS (
assertion = 'no_error',
description = 'Ontology definition compiles cleanly.'
) AS VALIDATE ONTOLOGY;

Every mapping still resolves against its source:

ALTER TESTSUITE prod_gate ADD TEST mappings_valid OPTIONS (
assertion = 'no_error',
description = 'All mappings compile against their source tables.'
) AS VALIDATE MAPPINGS;

Mappings against one datasource still resolve:

ALTER TESTSUITE prod_gate ADD TEST mappings_valid_crm OPTIONS (
assertion = 'no_error',
description = 'Mappings against datasource crm resolve.'
) AS VALIDATE DATASOURCE `crm`;

The customer base has not dropped below a known floor:

ALTER TESTSUITE prod_gate ADD TEST customer_volume OPTIONS (
assertion = 'scalar',
operator = '>',
expected = '20000',
description = 'Customer base must not drop below the known floor.'
) AS SELECT count(1) FROM dtimbr.customer;

Orders are loaded up to today, with the expected value computed by SQL:

ALTER TESTSUITE prod_gate ADD TEST order_freshness OPTIONS (
assertion = 'scalar',
operator = '=',
expected = 'current_date',
expected_is_sql = 'true',
description = 'Orders are as recent as today.'
) AS SELECT max(`order_date`) FROM dtimbr.`order`;

No customer is missing its key:

ALTER TESTSUITE prod_gate ADD TEST customer_id_not_null OPTIONS (
assertion = 'empty',
description = 'Every customer has a customer_id.'
) AS SELECT customer_id FROM dtimbr.customer WHERE customer_id IS NULL;
Validation tests catch source changes

VALIDATE ONTOLOGY, VALIDATE MAPPINGS and VALIDATE DATASOURCE tests are checked against the live source schemas. If a column a mapping uses is dropped or renamed in the source, the next run fails, before users or agents run into the error.

Update a test​

UPDATE TEST changes only the options you list; the others keep their current values.

ALTER TESTSUITE prod_gate UPDATE TEST customer_volume OPTIONS ( expected = '25000' );

To change a test's statement, drop the test and add it again.

Drop a test​

ALTER TESTSUITE prod_gate DROP TEST order_freshness;

Bind a suite to an ontology​

A suite can only be run against the ontologies it is bound to.

ALTER ONTOLOGY <ontology_name> ADD TESTSUITE <suite_name>;
ALTER ONTOLOGY <ontology_name> DROP TESTSUITE <suite_name>;

Example:

ALTER ONTOLOGY sales ADD TESTSUITE prod_gate;
  • Binding the same suite twice has no effect.
  • Dropping a binding that doesn't exist is a silent no-op.
  • Binding a suite that doesn't exist is rejected.
  • A suite can be bound to several ontologies, and an ontology can have several suites.

Run a test suite​

RUN TESTSUITE <suite_name>;
  • The suite runs against the ontology you are connected to. That ontology must be bound to the suite, or the statement is rejected.
  • The statement waits for all tests to finish and returns one row with the run's run_id.
  • The full results are then available in timbr.sys_testsuite_history.

To check a suite on several ontologies, run it once while connected to each of them. The Run button in Ontology Readiness does this for every bound ontology in one go.

Each test ends with one of these results:

ResultMeaning
passedThe assertion held
failedThe assertion did not hold, or a no_error statement returned an error
errorThe test could not be evaluated
skippedThe test was not run

Schedule test runs with a job​

To run a suite on a schedule, wrap RUN TESTSUITE in a job. The job runs on its schedule, or whenever you call RUN JOB.

Create the job on the ontology to test

A job runs in the ontology it was created in, and RUN TESTSUITE runs against that ontology. Create the job while connected to the ontology you want the suite to check. To check the same suite on several ontologies, create one job in each.

CREATE OR REPLACE JOB {job_name}
REFRESH '{refresh_value}'
AS RUN TESTSUITE {suite_name}

{refresh_value} is one of none, daily, weekly, monthly, yearly, or a cron expression. Cron schedules are in UTC. See Intervals for refresh_value.

Every morning at 06:00 UTC, while connected to sales:

CREATE OR REPLACE JOB readiness_sales_prod_gate
REFRESH '0 6 * * *'
AS RUN TESTSUITE prod_gate

An on-demand job that runs only when called, for example from a CI/CD pipeline before promoting a change:

CREATE OR REPLACE JOB readiness_sales_on_demand
REFRESH 'none'
AS RUN TESTSUITE prod_gate
RUN JOB readiness_sales_on_demand

Run several suites in one job​

A bulk job can run several suites. With PARALLEL above 1 they run at the same time.

CREATE OR REPLACE JOB readiness_sales_nightly
REFRESH '0 3 * * *'
PARALLEL '2'
AS BEGIN
RUN TESTSUITE smoke;
RUN TESTSUITE prod_gate;
END

Test after a cache refresh​

Statements inside a bulk job's BEGIN … END body run together in the job's thread pool, so a suite placed next to cache refreshes may run before they finish. To test the data after it is refreshed, put the suite in its own job and call it with RUN JOB, which runs only after all the refreshes are done:

-- Test job, triggered by the pipeline below
CREATE OR REPLACE JOB readiness_sales_after_refresh
REFRESH 'none'
AS RUN TESTSUITE prod_gate

-- Daily pipeline: refresh the caches, then test
CREATE OR REPLACE JOB refresh_sales_pipeline
REFRESH 'daily'
PARALLEL '2'
AS BEGIN
REFRESH CACHE MAPPING map_orders
WHERE order_date > CURRENT_DATE - 2;
REFRESH CACHE MAPPING map_customers;
RUN JOB readiness_sales_after_refresh;
END

Test jobs are managed like any other job with SHOW CREATE JOB, RUN JOB and DROP JOB. They also appear in Scheduled Jobs.

Scheduling from the UI

A suite can also be scheduled from Ontology Readiness, which creates the same kind of job. Use SQL for cron schedules, bulk jobs, and running tests after a cache refresh.


Review results​

Test suites are exposed through four system tables:

TableContents
timbr.sys_testsuitesOne row per suite: test_suite, description, number_of_tests, and the ontologies it is bound to
timbr.sys_testsuite_testsOne row per test: test_suite, test_name, assertion, operator, expected, expected_is_sql, …
timbr.sys_testsuite_runningRuns in progress. A run is removed from this table once it completes.
timbr.sys_testsuite_historyOne row per completed run and ontology: run_id, ontology, status, number_of_tests, passed, failed, error, and results, a JSON document with each test's result

SHOW TESTSUITES and SHOW TESTSUITE TESTS are shortcuts for the first two.

-- Suites and the ontologies they are bound to
SHOW TESTSUITES;

-- Tests in one suite
SELECT test_name, assertion, operator, expected, expected_is_sql
FROM timbr.sys_testsuite_tests
WHERE test_suite = 'prod_gate';

-- Runs for the sales ontology
SELECT run_id, status, number_of_tests, passed, failed, error
FROM timbr.sys_testsuite_history
WHERE ontology = 'sales';

-- Per-test results of one run
SELECT results
FROM timbr.sys_testsuite_history
WHERE run_id = '<run_id>' AND ontology = 'sales';

Runs started by a job are also recorded in timbr.sys_jobs_history under the job's name. If the job itself fails, status_log contains the error. See Cache/Jobs tables.


Remove a test suite​

DROP TESTSUITE [IF EXISTS] <suite_name>;

Dropping a suite also removes its tests and its ontology bindings. IF EXISTS makes the statement a no-op when the suite doesn't exist. Jobs that run the suite are not dropped; remove them with DROP JOB.


Permissions​

Test suites are a permissioned resource of their own (testsuite), separate from the ontologies they test:

TaskRequires
Create a suiteEDIT on testsuite
Alter or drop a suiteEDIT on the suite
Run a suiteQUERY (or EDIT) on the suite, and QUERY on the ontology it runs against
Bind a suite to an ontologyEDIT on the suite and EDIT on the ontology
See suites and testsACCESS on the suite
See run historyACCESS on the suite and on the ontology the run belongs to

A user who creates a suite automatically receives EDIT on it WITH GRANT OPTION.

GRANT EDIT  ON `testsuite` TO USER `alice`;                  -- create suites
GRANT QUERY ON `testsuite`.`prod_gate` TO ROLE `ci_runner`; -- run one suite

See Test suites (Ontology Readiness) in Permissions & Roles for the full model and more examples.


Best practices​

  • Start with structure. Add VALIDATE ONTOLOGY and VALIDATE MAPPINGS tests to every suite. They catch broken mappings and source schema changes with no tuning.
  • Calibrate thresholds. Set scalar and row_count floors from real data, not guesses. A threshold that is always red or always green tells you nothing. The Suggest tests flow in Ontology Readiness dry-runs candidates for this.
  • Describe every test. A description explains a failure to whoever reads the run report.
  • Keep suites focused. Use a small, fast smoke suite for frequent runs and a fuller prod_gate suite before promotions.
  • Reuse suites across environments. Bind the same suite to the test and production ontologies so both are held to the same checks.
  • Run after changes and on a schedule. Run the suite after every significant ontology change, and schedule it to follow your data loads.
  • Grant QUERY to automation. CI/CD and schedulers only need to run suites, not edit them.