SQL test framework
This needs some conceptional work.
Some open questions
-
Which SQL connection do we use and how to ensure we do not kill production systems #56
Scenarios
Files in test-scenarios.d
. Scenarios have a name and can depend on other scenarios. Usually they are just SQL code. But they could also execute SQL code they get from calling an executable.
---
name: minimal
description: Minimal scenario
dependencies:
- main.minimal
---
INSERT INTO myschema.mytable ('a', 1);
Randomness is mainly achieved by calling an executable. Maybe we we also want to allow to fill template variables with SQL queries or executable calls. For example to have random passwords for test users.
Function tests
Returning TABLE. Execute given SQL query. Simplest assertion is number of returned rows.
tests:
- scenario: mymodule.minimal
test: SELECT * FROM $(THIS)
assertions:
- rows: 1
- sql: (SELECT a FROM $(TEST)) IN ('x', 'y', 'z')
Returning value.
tests:
- scenario: myschema.minimal
parameters: (25, u: '{"a": 2}'::jsonb)
assertion:
value_equals: "'a'"
Test Execution
- Install DB structure
- Create
SAVEPOINT clean
- Loop through all existing scenarios:
- Deploy scenario
- Create
SAVEPOINT scenario
- Loop through all tests based on current scenario
- Run test (block repeated for each assertion)
ROLLBACK TO scenario
ROLLBACK TO clean
Deploy scenarios
For integration tests or development.
$ hamsql deploy-scenario myschema.minimal
Notes for scenario "main"."minimal"
You can use the following user for tests:
User: test1
Password: testtest