kura_sandbox (kura v1.19.2)

View Source

Test sandbox that wraps each test in a rolled-back transaction.

Supports three modes:

Single-process (default) — the test process owns the connection:

init_per_testcase(_TestCase, Config) ->
    kura_sandbox:checkout(my_repo),
    Config.

end_per_testcase(_TestCase, _Config) ->
    kura_sandbox:checkin(my_repo),
    ok.

Allow — let spawned processes share the sandbox connection:

kura_sandbox:checkout(my_repo),
kura_sandbox:allow(my_repo, self(), WorkerPid).

Shared — all processes use a single sandbox connection (simplest for tests that spawn many processes):

kura_sandbox:checkout(my_repo, #{shared => true}).

All database operations within the sandbox are rolled back on checkin. No test data persists.

Summary

Functions

Allow ChildPid to use OwnerPid's sandbox connection.

Roll back the sandbox transaction and return the connection to the pool.

Check out a sandbox connection with default options.

Check out a sandbox connection.

Look up a sandbox connection for the current process and pool.

Create the sandbox ETS table. Call once before tests run.

Functions

allow(RepoMod, OwnerPid, ChildPid)

-spec allow(module(), pid(), pid()) -> ok.

Allow ChildPid to use OwnerPid's sandbox connection.

The child process must call this before making any database queries.

checkin(RepoMod)

-spec checkin(module()) -> ok.

Roll back the sandbox transaction and return the connection to the pool.

checkout(RepoMod)

-spec checkout(module()) -> ok.

Check out a sandbox connection with default options.

checkout(RepoMod, Opts)

-spec checkout(module(), map()) -> ok.

Check out a sandbox connection.

Options:

  • shared — when true, all processes use this connection (default: false)

get_conn(Pool)

-spec get_conn(atom()) -> {ok, term()} | not_found.

Look up a sandbox connection for the current process and pool.

Returns {ok, Conn} if a sandbox connection exists (owned, allowed, or shared), not_found otherwise. Called from kura_repo_worker.

start()

-spec start() -> ok.

Create the sandbox ETS table. Call once before tests run.