自动在临时目录中设置postgresql实例,并在测试后销毁它
项目描述
关于
testing.postgresql 自动在临时目录中设置postgresql实例,并在测试后销毁它。
安装
使用pip
$ pip install testing.postgresql
并且 testing.postgresql 需要在您的PATH中设置PostgreSQL服务器。
用法
使用 testing.postgresql.Postgresql 创建PostgreSQL实例
import testing.postgresql from sqlalchemy import create_engine # Lanuch new PostgreSQL server with testing.postgresql.Postgresql() as postgresql: # connect to PostgreSQL engine = create_engine(postgresql.url()) # if you use postgresql or other drivers: # import psycopg2 # db = psycopg2.connect(**postgresql.dsn()) # # do any tests using PostgreSQL... # # PostgreSQL server is terminated here
testing.postgresql.Postgresql 在实例化时执行 initdb 和 postgres。在删除Postgresql对象时,它终止PostgreSQL实例并删除临时目录。
如果您想要包含表和任何应用程序修复件的数据库,请使用 copy_data_from 关键字
# uses a copy of specified data directory of PostgreSQL. postgresql = testing.postgresql.Postgresql(copy_data_from='/path/to/your/database')
例如,您可以在 setUp() 方法上为每个测试用例设置新的PostgreSQL服务器
import unittest import testing.postgresql class MyTestCase(unittest.TestCase): def setUp(self): self.postgresql = testing.postgresql.Postgresql() def tearDown(self): self.postgresql.stop()
以提高您的测试速度
testing.postgresql.Postgresql 在每次实例化时调用 initdb 命令。这很简单。但在许多情况下,为每个测试用例生成全新的数据库是非常浪费的。
为了优化行为,请使用 testing.postgresql.PostgresqlFactory。该工厂类能够将生成的数据库缓存到测试用例之外,并减少 initdb 命令的调用次数
import unittest import testing.postgresql # Generate Postgresql class which shares the generated database Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True) def tearDownModule(self): # clear cached database at end of tests Postgresql.clear_cache() class MyTestCase(unittest.TestCase): def setUp(self): # Use the generated Postgresql class instead of testing.postgresql.Postgresql self.postgresql = Postgresql() def tearDown(self): self.postgresql.stop()
如果您想要将修复件插入到缓存的数据库中,请使用 initdb_handler 选项
# create initial data on create as fixtures into the database def handler(postgresql): conn = psycopg2.connect(**postgresql.dsn()) cursor = conn.cursor() cursor.execute("CREATE TABLE hello(id int, value varchar(256))") cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')") cursor.close() conn.commit() conn.close() # Use `handler()` on initialize database Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True, on_initialized=handler)
要求
Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
pg8000 1.10
许可证
Apache License 2.0
历史
1.3.0 (2016-02-03)
添加 testing.postgresql.PostgresqlFactory
依赖 testing.common.database 包
1.2.1 (2015-08-22)
修复错误
关闭 #3 修复测试结束时的 AttributeError
1.2.0 (2015-05-17)
使用 pg8000 作为连接器创建测试数据库
连接到 postgres 创建测试数据库(而不是 template1)
1.1.2 (2015-04-06)
修复错误
在析构函数中不调用 os.getpid()(如果不需要的话)
如果 initdb 退出非零,则引发详细的 RuntimeError
1.1.1 (2015-01-18)
禁用 logging_collector 功能(针对 Fedora)
修复错误
MacPorts 默认路径是 /opt/local/lib/postgresql*,没有连字符
1.1.0 (2014-12-20)
调用 'postgres' 命令而不是 'postmaster'
1.0.6 (2014-07-19)
修复 #1 污染的 postmaster 关闭
1.0.5 (2014-07-19)
修复 PostgreSQL 的路径
使用绝对路径执行 which 命令
1.0.4 (2014-06-19)
修复终止 postgresql 时的超时
支持 /usr/local/bin 上的 PostgreSQL(参照 FreeBSD ports)
修复错误
1.0.3 (2014-06-11)
修复在 py3 上捕获 SIGINT 时的 ImportError
1.0.2 (2013-12-06)
更改行为:Postgresql#stop() 清理工作目录
修复对象删除时捕获的 AttributeError
1.0.1 (2013-12-05)
添加 @skipIfNotInstalled 装饰器(skipIfNotFound 的别名)
支持 python 2.6 和 3.2
1.0.0 (2013-12-04)
添加 @skipIfNotFound 装饰器
0.1.0 (2013-11-26)
首次发布
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。
源代码分发
构建分发
测试.postgresql-1.3.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8e1a69760369a7a8ffe63a66b6d95a5cd82db2fb976e4a8f85ffd24fbfc447d8 |
|
MD5 | 51441a021c72c8f85182294249d4ca49 |
|
BLAKE2b-256 | 3a5b3bf1323697c4f4f0e8fb5c14d082dc2f005385ea139b19646c0fc9f1dbb7 |
测试.postgresql-1.3.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1b41daeb98dfc8cd4a584bb91e8f5f4ab182993870f95257afe5f1ba6151a598 |
|
MD5 | 34079e16cc477a9396374d1a075ff784 |
|
BLAKE2b-256 | 1176d614d4bc950d961a73c952e9a2e0956d02d0869a86d3dfad070376863988 |