Agent Wranglers

TST-12Required

Test against the same database engine

Test database code against the same database engine and version as production, never against a different engine or a mock.

How to do it

Start the database in a container for the test run, for example with Testcontainers or a Compose file. Run the migrations before the tests. An in-memory stand-in such as SQLite for a PostgreSQL app hides bugs in queries, types and locking. Unit tests of logic that takes plain data need no database at all. Test schema changes the same way, as Test migrations on a copy of production data says. Each migration must also work with the previous release, so run that release's tests against the new schema, as Expand first and contract later says.

Examples

JavaScript example

import { PostgreSqlContainer } from '@testcontainers/postgresql';

const db = await new PostgreSqlContainer('postgres:18').start();
process.env.DATABASE_URL = db.getConnectionUri();
await runMigrations();

Python example

# Python with testcontainers
from testcontainers.postgres import PostgresContainer

with PostgresContainer("postgres:18", driver=None) as database:
    os.environ["DATABASE_URL"] = database.get_connection_url()
    run_migrations()

How to check

The test setup starts the production database engine at the same major version as production and runs every migration. A query that only works on the stand-in fails the suite.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/integration-and-contract-tests/same-database-engine">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.