Many applications rely heavily on their database — and why shouldn't they! But writing tests that use the database is always a bit awkward, or in the worst case, very slow. Sometimes teams even mock their database out for an in-memory one, which works but only as long as you're not relying on specific features of your database.
Postgres is a fantastic database, and at my last job we relied heavily on its views, functions, data types, etc. A lot of our application logic was tied up with it. But writing tests against it was slow. Naively, each test would have to:
- Create a new database
- Run our migrations on it
- Then run the test
- Then tear down the database
pgtestdb makes this all a lot faster with two major strategies:
- Use a ram-backed or tmpfs-backed server that drops all data consistency guarantees. This isn't an issue for tests at all, and makes them much faster.
- Use template databases to only ever have to run the migrations once, when they change, and then fork that template each time a test asks for a new database.
Check out the docs on the Github page for more information but I hope you find this useful. I've written adaptors for most common migration frameworks so hopefully you can try it out without much issue.
Sincerely, Peter