There were some proposed solutions to the question "How to test SQL statements in an application" -
- Using RAM memory - I can't change the configuration of staging environment where testing happens.
- Using H2 - Not very compatible even in PostgreSQL mode
- Use the same database to run the tests.
- Using in-memory mode - PostgreSQL doesn't have one.
The third one was viable and I looked into Test Containers which is actually a beautiful solution but a relatively new one. As a result, our company is sceptical of adopting it.
We use Mybatis to access PostgreSQL.
Another way would be to recreate entire schema and populate required tables before tests. Here is the problem, I could create and delete schema with tables with the same name. To avoid name collision I'd have to change schema's name, as a result, even queries should be renamed which is not at all preferred. Is there a way to do this without changing queries but pointing them to the dummy schema.
ALTER ROLE test_role SET search_path = test_schema;, assuming your queries (or function definitions) don't have schema explicitly specified.