The Adapter Pattern in Go
bitfieldconsulting.com
bitfieldconsulting.com
Imho: Everything which promotes unit testing also promotes maintainable code.
It balloons the size of the code base and as a reward you end up with a ton of mimetic tests that mirror the code and dont actually catch any bugs.
But even there, you can at the very least mock the responses of the database (or other external systems) and check if the data in your response is as expected. Naturally, this works best if you work with structured data or have a well-defined domain. In case of a well defined domain, you can just mock the domain models don't even touch the module which talks to the outside world.
You certainly can but these tests end up mimicking the implementation rather than actually testing the code.
A pure CRUD app - even if it's relatively complicated - should have zero unit tests.
Surely, you have a baseline of verification/validation logic for which unit tests are the best fit. And based on my experience of my last project, if you can't do proper unit testing you likely have the wrong granularity in your methods and classes.
[1]: https://kevin.burke.dev/kevin/fast-parallel-database-tests
I don't want to worry about whether a library is correctly implementing the real behavior of the real thing.
I can understand wanting to mock/fake/stub/whatever stuff that is outside of your control, like a third-party HTTP API where you can't just send real requests just for testing.
But for the situation in the article, those tests just seem too fragile to me. And a lot of effort for the comparatively little confidence added by the test results.
Also spinning one up locally is easy thanks to Docker.
Clean up = throw away
Depends on the bringup cost, although templates help there (create a baseline database, then copy it for each test).
I'm writing a small and simple application and I'm just using an sqlite database for my tests. I have a helper function to create the db, another to add a table, and then a third to delete the db at the end.
So in total 3 functions to setup and teardown a test database. Its super quick because an sqlite db is just a file after all.
I jest. I don't like mocking database methods, they don't give me much confidence. I test the SQL against Postgres in a Docker container.
With a real DB it's not exactly easy to inject "ERROR: deadlock" at every single step.
Abstractions are useful, but they also leak. The wrong abstraction can make many useful things impossible.
Just like ORMs, such interfaces can be useful, but have the cost of limiting your options.