测试postgres db python [英] testing postgres db python

查看:80
本文介绍了测试postgres db python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何测试我的存储库。



我想确保我确实将带有所有参数的对象保存到数据库中,并且当我执行SQL语句时,确实收到了应有的提示。 / p>

但是,我不能将 CREATE TABLE test_table 放入 setUp 单元测试用例的方法,因为它将被创建多次(并行运行同一测试用例的测试)。因此,只要我在需要使用同一张表的同一个类中创建2个方法,它就不会起作用(表的名称冲突)



相同,我不能放置 CREATE TABLE test_table setUpModule ,因为现在该表创建一次,但是由于运行了测试并行地,没有什么可以阻止将同一对象多次插入到我的表中,这打破了某些字段的唯一性约束。



同样,我不能在每个方法中都创建SCHEMA some_random_schema_name ,因为我需要为给定的数据库全局地 SET search_path TO ...,因此并行运行的每个方法都会受到影响。



我看到的唯一方法是为每个测试创建 CREATE DATABASE ,并使用唯一的名称,并建立一个与每个数据库的单独连接。这看起来非常浪费。还有更好的方法吗?



此外,由于需要测试PostgreSQL,因此无法在内存中使用SQLite。

解决方案

对此的最佳解决方案是使用 testing.postgresql 模块。这会在用户空间中启动一个数据库,然后在运行结束时再次将其删除。您可以将以下内容放入单元测试套件中- setUp setUpClass setUpModule -取决于您想要的持久性:

 导入测试。postgresql

def setUp(self):
self.postgresql = testing.postgresql.Postgresql(port = 7654)
#获取要与psycopg2或等效
print(self.postgresql.url)连接的URL ())

def tearDown(self):
self.postgresql.stop()

如果您希望数据库在测试之间/之后保持不变,则可以使用 base_dir 选项运行它来设置目录-这样可以防止删除数据库关机后:

  name = testdb 
端口= 5678
路径= / tmp / my_test_db
testing.postgresql.Postgresql(name = name,port = port,base_dir = path)

除测试外,它还可以用作上下文管理器,在该管理器中它将自动清理并当退出with块时关闭:

 ,带有testing.postgresql.Postgresql(port = 7654)as psql:
#在这里做某事


I don't understand how to test my repositories.

I want to be sure that I really saved object with all of it parameters into database, and when I execute my SQL statement I really received what I am supposed to.

But, I cannot put "CREATE TABLE test_table" in setUp method of unittest case because it will be created multiple times (tests of the same testcase are runned in parallel). So, as long as I create 2 methods in the same class which needs to work on the same table, it won't work (name clash of tables)

Same, I cannot put "CREATE TABLE test_table" setUpModule, because, now the table is created once, but since tests are runned in parallel, there is nothing which prevents from inserting the same object multiple times into my table, which breakes the unicity constraint of some field.

Same, I cannot "CREATE SCHEMA some_random_schema_name" in every method, because I need to globally "SET search_path TO ..." for a given Database, so every method runned in parallel will be affected.

The only way I see is to create to "CREATE DATABASE" for each test, and with unique name, and establish a invidual connection to each database.. This looks extreeeemly wasteful. Is there a better way?

Also, I cannot use SQLite in memory because I need to test PostgreSQL.

解决方案

The best solution for this is to use the testing.postgresql module. This fires up a db in user-space, then deletes it again at the end of the run. You can put the following in a unittest suite - either in setUp, setUpClass or setUpModule - depending on what persistence you want:

import testing.postgresql

def setUp(self):
    self.postgresql = testing.postgresql.Postgresql(port=7654)
    # Get the url to connect to with psycopg2 or equivalent
    print(self.postgresql.url())

def tearDown(self):
    self.postgresql.stop()

If you want the database to persist between/after tests, you can run it with the base_dir option to set a directory - which will prevent it's removal after shutdown:

name = "testdb"
port = "5678"
path = "/tmp/my_test_db"
testing.postgresql.Postgresql(name=name, port=port, base_dir=path)

Outside of testing it can also be used as a context manager, where it will automatically clean up and shut down when the with block is exited:

with testing.postgresql.Postgresql(port=7654) as psql:
    # do something here

这篇关于测试postgres db python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆