Test strategy for postgres in the backend
I'd like to test a backend that performs queries agains a Postgres DB. As far as I understand mocking postgres is not possible (as opposed to MongoDB) and one should query a real database.
So my first question is, what are the standard strategies to test postgres queries?
Right now, I'm trying to recreate a database with no data so that I can plug in information for each test and have full control of the output. However, it is not clear how that can be done. So far I've come up with something like:
pg_dump mydb --schema-only | pg_restore testdb
However it is really slow. I suppose one of the reasons is that if the test client is not in the same net as the database, then pg_dump will have to fetch all the information through the net only to send it back to the same machine.
I suppose what I'm looking for would be similar to
Create new database testdb with template mydb
which only copied schema info and which didn't force all connections to be closed.
Your idea of a template database is the best one, I think.
Why don't you create a template database that is the same as mydb
, only without data? If your problem is that mydb
undergoes frequent schema changes, use a deployment technique that applies all schema changes to both mydb
and the template database.
下一篇: Postgres在后端的测试策略