Skip to main content

Test Syntax

In wvlet, you can insert test statements in the middle of a query. For example, you can test the number of rows, column names, or the query result itself:

person.json
[
{"id":1, "name": "alice", "age": 10 },
{"id":2, "name": "bob", "age": 24 },
{"id":3, "name": "clark", "age": 40 }
]
from 'person.json'
-- Test the query size and schema
test _.size should be 3
test _.columns should be ['id', 'name', 'age']
test _.columns should contain 'name'
test _.columns should not contain 'address'

-- Test the query output. Surrounding white spaces will be trimmed:
test _.output should be """
┌──────┬────────┬──────┐
│ id │ name │ age │
│ long │ string │ long │
├──────┼────────┼──────┤
│ 1 │ alice │ 10 │
│ 2 │ bob │ 24 │
│ 3 │ clark │ 40 │
├──────┴────────┴──────┤
│ 3 rows │
└──────────────────────┘
"""

-- Test the query result in JSON format
test _.json should be """
{"id":1,"name":"alice","age":10}
{"id":2,"name":"bob","age":24}
{"id":3,"name":"clark","age":40}
"""
test _.json should contain """{"id":2,"name":"bob","age":24}"""
test _.json should not contain """{"id":4,"name":"david","age":32}"""

-- Test rows with arrays
test _.rows should contain [1, "alice", 10]
test _.rows should contain [2, "bob", 24]
test _.rows should not contain [5, "eric", 18]
test _.rows should be [
[1, "alice", 10],
[2, "bob", 24],
[3, "clark", 40],
]

wvlet run, the REPL, and the server evaluate these tests, and a failing test fails the run. wvlet compile ignores them, so the generated SQL contains no tests.

A test that can't be evaluated also fails: an expression that isn't listed below, values that can't be ordered by < or >, such as a number and a string, or a test without a query before it.

A test inside a debug { ... } block checks the result of that block. Like any other test, it fails the run when it fails. The tables that the block shows do not change the result of the query.

from 'person.json'
debug {
where age > 20
test _.size should be 2
}
select name

Result Inspection Functions​

SyntaxOutput
_.sizeThe number of rows in the query result
_.columnsThe column names in the query result
_.typesThe column types, as _.output shows them
_.outputPretty print query result, with the displayed rows (40 by default)
_.jsonJSON line format of every result row
_.errorThe error of the query
_.rowsevery row, in array of array representation

Test Expressions​

SyntaxDescription
x should be yTest if the value of x is equal to y
x should not be yTest if the value of x is not equal to y
_.rows should be {[...], [...]}Test the rows in any order
_.rows should be exactly yTest the rows with their types
x should contain yTest if the string value of x contains y
x should not contain yTest if the string value of x does not contain y
x = yTest if the value of x is equal to y
x is yTest if the value of x is equal to y
x != yTest if the value of x is not equal to y
x is not yTest if the value of x is not equal to y
x (cmp) yCompare x and y with <, <=, >=, >

Rows in Any Order​

Without order by, databases may return rows in any order. Write the expected rows in braces to ignore the order of rows. Duplicates count: {[1], [1]} isn't {[1]}. Write [] for no rows.

from [[1], [2], [2]] as t(id)
test _.rows should be {[2], [1], [2]}
test _.rows should not be {[1], [2]}

Exact Values​

should be is lenient: 1 equals 1.0, '1', and 1.0::double. should be exactly also compares the types of the values. The expected value is of the type that its literal or cast has:

TypeExpected valueCompared
integer1The number, of any width
decimal1.0The number, of any scale
double1.0::double, 'NaN'::doubleThe value. 0.0 isn't -0.0, and NaN equals NaN
float0.1::floatAs double
booleantrueThe value
string'a'The text, with its spaces
date, timestamp'2024-01-02'::dateThe date or time
array[1, 2]Each element
anynullNULL
from [[1, 1.5, 'a']] as t(id, price, name)
test _.rows should be exactly [[1, 1.5, 'a']]
test _.rows should not be exactly [['1', 1.5, 'a']]
test _.rows should be exactly {[1, 1.5, 'a']}

Use _.types to test widths, precisions, and scales. Both should be exactly and should not be exactly fail when the types of the values aren't known: on connectors that return text, such as Trino, for structs, maps, JSON, and timestamps with time zones, for timestamps on DuckDB, and for arrays on the Wvlet engine.

Types that a connector doesn't tell apart are compared as one: on the Wvlet engine, JSON and char values are strings, and float is double. On DuckDB, dates before year 1 lose their era.

Expected Errors​

Use _.error at the end of a statement whose values make it fail. The error then doesn't fail the run, and a query that succeeds fails the test:

from [['1'], ['x']] as t(a)
select a::int as b
test _.error should contain 'Could not convert'
  • It works on DuckDB, for conversion, invalid input, out of range, and constraint errors, and on the Wvlet engine, for values that can't be converted or are out of range, overflows, and scalar subqueries of several rows. The engine reports a cast between types that never convert in the same way, so such an error is expected, too. Every other error fails the run, such as an unknown table, a time-out, a cancelled query, or a lack of memory, and so does any error of another database.
  • Only should contain and should be with a text are supported, as should not contain would also pass for an unrelated error.
  • The message is the database's, so it differs between databases. It doesn't have the SQL and the source line that some messages repeat, but may name the values and columns of the query.
  • Errors in the middle of a statement, in a debug block, and before save fail the run.
  • The other tests of a query that failed, such as _.size, fail.