News for hackers. Stuff that matters.
Re:Database mocks are just not worth it by ariofrio on Monday December 30, 2024 @04:08PM (#42553547)
Does something like PGlite work for your use case? https://pglite.dev/
Re:Database mocks are just not worth it by devoutsalsa on Monday December 30, 2024 @04:13PM (#42553595)
I want test with the same API as my database, which means using the same database. Setting up an instance of Postgres/MySQL/whatever isn't hard. Maybe harder if you're using an online database that doesn't fit into a container, but that's a different problem.
Re:Database mocks are just not worth it by smackeyacky on Monday December 30, 2024 @04:14PM (#42553614)
You can just simply run postgres, why bother with pglite?

postgres installs easily on WSL2 or whatever Linux distribution you're using.

Re:Database mocks are just not worth it by fweimer on Monday December 30, 2024 @06:07PM (#42554618)
You don't even need to install it. It doesn't take a lot of code to run initdb to create a temporary instance, write a suitable configuration file, launch the postmaster process, and delete the temporary database directory tree after terminating the database after testing is complete. On a not-too-fast Linux system, the time for all that is around half a second. Too much overhead for individual unit tests, but fast enough to run once per test suite run.
Re:Database mocks are just not worth it by rikthevik on Monday December 30, 2024 @09:56PM (#42555960)
Agreed. Running docker-compose and postgres-alpine works just fine.
Re:Database mocks are just not worth it by samwillis on Monday December 30, 2024 @04:31PM (#42553784)
Unit tests work well with PGlite, at least in the TS/JS world where it natively sits at the moment. You can have a unique Postgres instance for each unit test that's up and running in just a few ms.

It's possible to use PGlite from any language using native Postgres clients and pg-gateway, but you lose some of the nice test DX when it's embed directly in the test code.

I'm hopeful that we can bring PGlite to other platforms, it's being actively worked on.

The other thing I hope we can look at at some point is instant forks of in memory databases, it would make it possible to setup a test db once and then reset it to a known state for each test.

(I work on PGlite)