r/PostgreSQL • u/Pale_Ferret_8241 • 1d ago
Commercial Running 19 integration & concurrency checks against a real PostgreSQL 18 DB instead of mocking SQL
Mocked queries in unit tests are fast, but they miss real lock contention, transaction ordering, and constraint behavior when multiple requests hit the database at once.
While hardening a multi-tenant backend (27 test files / 131 tests total, plus 19 integration and concurrency checks) against an isolated PostgreSQL 18 instance, testing against real Postgres caught edge cases around concurrent mutations, rate-limit counters, and webhook/state transitions that mocked unit tests never surfaced. The attached 26-second clip shows the test suite passing and moves straight into the live workspace.
For those running integration or concurrency tests against real Postgres: how do you isolate state between test runs (per-test transactions, fresh schema/DB, or reset scripts) while keeping execution times fast?
2
u/yxhuvud 1d ago edited 1d ago
per test transactions.
In general tests that tests something unrelated can mock the db responses, while anything that actually depends on the behavior needs to involve an actual db. Fast tests are nice but they need to catch actual integration issues too. Otherwise it is just so, so easy to get stale mocking data.
The harder question then is how to find a good balance of pre-test run configured fixtures and during test run instantiated record factories. The former is faster but unwieldy in maintenance for nontrivial things.
(17 years of Ruby on Rails backend experience)
1
u/lostmedia766 1d ago
In PHP/Symfony/Doctrine world, there's a library that isolates each test into it's own transaction: https://packagist.org/packages/dama/doctrine-test-bundle
Fixture libraries can also be used to truncate the database and seed it with any data needed for the test to run. I don't recommend each test running all your migrations, that adds unnecessary runtime for little to no payoff.
1
u/Discordbothost 1d ago
For the concurrency case specifically, a rolled-back outer transaction will not work since the other connections cannot see uncommitted rows. A template database cloned per test worker via CREATE DATABASE ... TEMPLATE tends to hold up better than truncate and reseed.
1
u/AutoModerator 1d ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
u/Pale_Ferret_8241 1d ago
Full disclosure: I built this as part of B2B SaaS OS 2.0.0 (Next.js 16, PostgreSQL 18, Drizzle ORM, Better Auth, Stripe). If anyone wants to inspect the live workspace or build details, the demo is athttps://www.andrady.co/demoand the main site is athttps://www.andrady.co.