db.test-utils

Database test utilities providing fixtures and helpers for database testing.

db-fixture

(db-fixture {:keys [db-url migrations]})

Creates a database fixture for test isolation.

Ensures test database exists, runs migrations, and provides connection pooling. Uses exclusive-initializer to ensure setup runs only once across parallel tests.

Args: config: Map with keys: :db-url - JDBC URL for test database :migrations - Collection of Ragtime migrations

Returns: Test fixture function that accepts a test function

do-global-frozen-time

(do-global-frozen-time f)

Internal helper for with-global-frozen-time.

ensure-database-exists!

(ensure-database-exists! db-url)

Ensures the database specified in the JDBC URL exists.

Creates the database if it doesn’t exist. Safe to call multiple times as it gracefully handles the ‘database already exists’ error.

Returns nil.

rollback-fixture

(rollback-fixture f)

Transaction fixture that automatically rolls back all database changes.

Wraps test execution in a transaction that is always rolled back, ensuring database state is restored after each test.

Args: f: Test function to execute

Raises: IllegalStateException: If no datasource is bound (db-fixture must run first)

with-global-frozen-time

macro

(with-global-frozen-time time & body)

Freezes time both in JVM and PostgreSQL transaction.

Combines java-time.api/with-clock for JVM-level time freezing with PostgreSQL session variable setting for database-level time control.

Args: time: Time instant to freeze to body: Code to execute with frozen time

Raises: IllegalStateException: If not executed within a database transaction