Test helpers
Three helpers under ORM::ActiveRecord::Testing keep test data isolated and
easy to set up: a transactional wrapper, a YAML fixture loader, and a database
cleaner.
Transactional tests
Wrapping each example in a transaction that is rolled back at teardown leaves
the database untouched between tests, with no DELETE or TRUNCATE. Nested
model transactions join the open one (via savepoints), so a test's own
transaction { ... } still behaves.
1 2 3 4 | |
Or wrap a single body:
1 2 3 4 | |
begin-transactional-test accepts an :isolation level
(READ COMMITTED, REPEATABLE READ, ...).
Fixtures
Fixtures are YAML files, one per table (the filename is the table name). Each top-level key is a label; its id is derived deterministically from the label, so references resolve across files regardless of load order.
1 2 3 4 5 6 | |
1 2 3 4 | |
1 2 3 4 5 6 7 | |
Loading deletes the table's existing rows first, so it is repeatable.
Interpolation
<%= expr %> in a value is replaced by the stringified result of the Raku
expression before the YAML is parsed — the equivalent of Rails' ERB fixtures.
1 2 | |
References between files
A key that is not a column but whose <key>_id is a column is treated as a
belongs-to reference: the value names another fixture's label, and the loader
sets <key>_id to that label's deterministic id. Because the id comes from the
label alone, the referenced file need not be loaded first (or at all).
1 | |
Database cleaner
When you are not using the transactional wrapper, clean explicitly:
1 2 3 4 5 6 | |
truncation issues TRUNCATE ... RESTART IDENTITY CASCADE on PostgreSQL,
disables foreign-key checks around TRUNCATE on MySQL, and falls back to
DELETE (plus clearing sqlite_sequence) on SQLite.
The transaction strategy runs a block and rolls it back, like the wrapper:
1 2 3 | |
The migrations table is never touched. Pass except => <migrations other> to
spare more tables.