postgresql测试工具及其扩展

testgres的Python项目详细描述


[![构建状态](https://travis-ci.org/postgrespro/testgres.svg?branch=master)(https://travis ci.org/postgrespro/testgres)
[![codecov](https://codecov.io/gh/postgrespro/testgres/branch/master/graph/badge.svg)(https://codecov.io/gh/postgrespro/testgres)
[![PYPI版本](https://badge.fury.io/py/testgres.svg)](https://badge.fury.io/py/testgres)

[文档](https://postgrespro.github.io/testgres/)

同时支持python 2.7和3.3+。





35;环境

>;注:由默认testgres运行"path"提供的"initdb"、"pg_ctl"、"psql"。


有几种方法可以指定自定义postgres安装:

*导出指向"pg_config"可执行文件的"pg_config"环境变量;
*导出带有可执行文件的目录的"pg_bin"环境变量。


示例:

``bash
export pg_bin=$home/pg_10/bin
python my_tests.py
```


` testgres`:

`` python
创建一个具有随机名称、端口、etc
使用testgres.get_new_node()作为节点:

节点停止及其文件将被删除
```

runnig查询有四种api方法:

|
——STRDER)。|
` node.safe_psql(query,…)``与'psql()相同,只是它只返回'stdout'。如果在执行期间发生错误,将引发异常。|
` node.execute(query,…)`使用'psycopg2'或'pg8000'连接到PostgreSQL(取决于系统中安装了哪一个),并返回包含数据的二维数组。|
` node.connect(dbname,…)`返回能够在单个事务中运行多个查询的连接包装器(`node connection`)。|

最后一个是最强大的:您可以使用'begin(隔离级别)`,`commit()`和'rollback()`:
``python
将node.connect()用作con:
con.begin('serializable')
print(con.execute('select%s',(1))
con.rollback()
```



\在运行任何测试之前执行"configure_testgres(node_cleanup_full=false)。

>;注意:上下文管理器(又称"with")自动调用"stop()"和"cleanup()"。

"testgres"支持[python日志记录](https://docs.python.org/3.6/library/logging.html),
这意味着您可以将多个节点的日志聚合到一个文件中:

``python
import logging

并创建两个不同的节点
testgres.configure_testgres(enable_python_logging=true)
node1=testgres.get_new_node().init().start()
node2=testgres.get_new_node().init().start()

#禁用日志记录
testgres.configure_testgres(enable_python_logging=false)
````

查看"tests/test_simple.py"文件以获取日志记录的完整示例
配置。



复制

创建备份并启动新副本相当容易:

``python
使用testgres.get_new_node('master')作为主节点:
master.init().start()

replica
replica=backup.spawn_replica('replica').start()

还可以使用'pgbench':

``python
with testgres.get_new_node('master')作为master运行基准:
=master.pgbench_init(scale=2).pgbench_run(time=10)
print(res)
`````



自定义配置

扩展"testgres"提供的默认配置通常很有用。

"testgres"具有"default_conf()函数,有助于控制一些基本的
选项。"append_conf()"函数可用于将自定义
行添加到配置行:

``python
ext_conf="shared_preload_libraries='postgres_fdw'


做些什么…

重置主配置文件
master.default配置(fsync=true,
allow_streaming=true)

添加一个新的配置行
master.append_conf('postgresql.conf',ext_conf)
```

注意,"default戋conf()"由"init()"函数调用;它们都覆盖配置文件,这意味着它们应该在"append戋conf()"之前调用。



authors

[ildar musin](https://github.com/zilder)<;i.musin(at)postgres professional lt d.,俄罗斯
[dmitry ivanov](https://github.com/funbringer)<;d.ivanov(at)postgresprofessional ltd.,俄罗斯
[ildus kurbangaliev](https://github.com/ildus)<;i.kurbangaliev(at)postgresprofessional ltd;俄罗斯Postgres专业有限公司

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
尝试通过java驱动程序连接时,mongodb服务器上的SSLhandshake失败   使用PlayFramework的Azure网站中的java Logback   java在另一个ArrayList中使用ArrayList处理复杂的JSON响应   java无法在另一台机器上运行eclipse tomcat中的war文件   java GZIPOutputStream有什么替代方案吗?   java Nashorn调试在Nashorn中运行的javascript   java文本短信未发送,即使toast显示已发送   java Hibernatesearch 5.0 spatial不确定是否在散列中存储lat/lon   java我想创建一个带有文本视图的计数器   java安卓:如何正确地同步资源   java使用mockito。当不知道方法调用的参数时   firebase Java使用HTTP v1发送错误字符的中文通知   java Hibernate无法映射到表?   java使用对象映射器解析复杂JSON   java Selenium Grid 2并行测试用例执行   java所有项目在列表视图中重复