简体   繁体   English

后端 postgres 的测试策略

[英]Test strategy for postgres in the backend

I'd like to test a backend that performs queries agains a Postgres DB.我想测试一个对 Postgres 数据库执行查询的后端。 As far as I understand mocking postgres is not possible (as opposed to MongoDB) and one should query a real database.据我所知,模拟 postgres 是不可能的(与 MongoDB 相对),应该查询一个真实的数据库。

So my first question is, what are the standard strategies to test postgres queries?所以我的第一个问题是,测试 postgres 查询的标准策略是什么?

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.我想其中一个原因是,如果测试客户端与数据库不在同一个网络中,那么 pg_dump 将不得不通过网络获取所有信息才能将其发送回同一台机器。

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?你为什么不创建一个和mydb一样的模板数据库,只是没有数据? 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.如果您的问题是mydb频繁发生架构更改,请使用将所有架构更改同时应用于mydb和模板数据库的部署技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM