创建一个临时、本地的PostgreSQL数据库集群和服务器用于unittesting,并在使用后进行清理
项目描述
pgtest 
创建一个临时、本地的PostgreSQL数据库集群和服务器,专门用于unittesting,并在使用后进行清理。
PGTest(username='postgres', port=None, log_file=None, no_cleanup=False,
copy_cluster=None, base_dir=None, pg_ctl=None, max_connections=5)
Args:
username - str, username for default database superuser
port - int, port to connect on; you must ensure that the port is unused
log_file - str, path to place the log file
no_cleanup - bool, don't clean up dirs after PGTest.close() is called
copy_cluster - str, copies cluster from this path
base_dir - str, path to the base directory to init the cluster
pg_ctl - str, path to the pg_ctl executable to use
max_connections - int, maximum number of connections to the cluster
Attributes:
PGTest.port - int, port number bound by PGTest
PGTest.cluster - str, cluster directory generated by PGTest
PGTest.username - str, username used by PGTest. Default is 'postgres'
PGTest.log_file - str, path to postgres log file
PGTest.pg_ctl - str, path to pg_ctl executable
PGTest.url - str, url for default postgres database on the cluster
PGTest.dsn - dict, dictionary containing dsn key-value pairs for the
default postgres database on the cluster
Methods:
close() - Closes this instance of PGTest, cleans up directories
作为实例使用
>>> from pgtest.pgtest import PGTest
>>> import psycopg2
>>> pg = PGTest()
Server started: postgresql://postgres@localhost:47251/postgres
>>> pg.port
47251
>>> pg.cluster
'/tmp/tmpiDtBjs/data'
>>> pg.username
'postgres'
>>> pg.log_file
'/tmp/tmpiDtBjs/pgtest_log.txt'
>>> pg.pg_ctl
u'/usr/lib/postgresql/9.4/bin/pg_ctl'
>>> pg.url
'postgresql://postgres@localhost:47251/postgres'
>>> pg.dsn
{'user': 'postgres', 'host': 'localhost',
'port': 47251, 'database': 'postgres'}
>>> # Connect with other db driver here, e.g. psql, psycopg2,
>>> # sqlalchemy etc
>>> psycopg2.connect(**pg.dsn)
>>> pg.close()
Server stopped
或者作为上下文使用
>>> from pgtest.pgtest import PGTest
>>> import psycopg2
>>> with PGTest() as pg:
... # connect to db with psycopg/sqlalchemy etc
... psycopg2.connect(**pg.dsn)
... # Do other database actions here
>>> # When the context exits, the db cluster and service is deleted unless specified
在setUp()
和tearDown()
方法中使用unittest
import unittest
from pgtest.pgtest import PGTest
class TestThirdPartyDrivers(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.pg = pgtest.PGTest()
cls.base_dir = cls.pg._base_dir
@classmethod
def tearDownClass(cls):
cls.pg.close()
def test_something(self):
self.assertTrue(isinstance(self.pg, PGTest)
项目详情
下载文件
下载适用于您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
pgtest-1.3.2.tar.gz (9.3 kB 查看哈希值)
构建发行版
pgtest-1.3.2-py2.py3-none-any.whl (9.1 kB 查看哈希值)
关闭
pgtest-1.3.2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | edbb5ed5f5c46ae6f6a58eeffcf9f2ef1467bfee4eb753f0bca4ab2cb302ee91 |
|
MD5 | ec7271b8ed7c51bf0a98b2a93ebbe851 |
|
BLAKE2b-256 | 5241ff29129bc427bbdc6cbe4559a4830a4f4d71739ce33c728b4fa610f5b12e |
关闭
pgtest-1.3.2-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a80a5b6080c36a207cdbbc44c45c829f721b0e643dd9b821bd31b8f26849c36b |
|
MD5 | cb0b0a0040614562dee115a13cfe59a4 |
|
BLAKE2b-256 | d3477b58a3599232c80d1e87facaf24c67a826eed7342a12d5b6e7f8b7735163 |