News for hackers. Stuff that matters.
Re:Database mocks are just not worth it by rikthevik on Monday December 30, 2024 @04:00PM (#42553467)
> But there should be very few of those if you're using a framework that abstracts away the database.

But I really want that database-specific behaviour. :) PostgreSQL does so many amazing things (recursive CTEs, jsonb, etc) that actively make our system better. If there was a fork of Django that optimized for leveraging advanced postgres features, I'd use it.

Re:Database mocks are just not worth it by zeroonetwothree on Monday December 30, 2024 @04:16PM (#42553633)
Sqlite supports recursive CTE
Re:Database mocks are just not worth it by rikthevik on Monday December 30, 2024 @05:03PM (#42554035)
I think you're missing the point.

I want to develop on postgres and test on postgres because I run postgres in production and I want to take advantage of all of its features. I don't understand why a person would 1) develop/test on a different database than production or 2) restrict one's self to the lowest common denominator of database features.

Test what you fly, fly what you test.

Re:Database mocks are just not worth it by maxbond on Monday December 30, 2024 @05:06PM (#42554057)
And JSONB, but you can't insert or update in a CTE like you can with Postgres.
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)

Re:Database mocks are just not worth it by lmm on Monday December 30, 2024 @07:14PM (#42555076)
Embedded/in-memory Postgres for testing is easier than you might think.
Re:Database mocks are just not worth it by cryptonector on Monday December 30, 2024 @10:18PM (#42556095)
SQLite does have recursive CTEs, but yeah, everything else that's all about types other than the ones SQLite supports...