testing.postgresql
automatically setups a postgresql instance in a temporary directory, and destroys it after testing.
.. image:: https://travis-ci.org/tk0miya/testing.postgresql.svg?branch=master :target: https://travis-ci.org/tk0miya/testing.postgresql
.. image:: https://coveralls.io/repos/tk0miya/testing.postgresql/badge.png?branch=master :target: https://coveralls.io/r/tk0miya/testing.postgresql?branch=master
.. image:: https://codeclimate.com/github/tk0miya/testing.postgresql/badges/gpa.svg :target: https://codeclimate.com/github/tk0miya/testing.postgresql
Documentation https://github.com/tk0miya/testing.postgresql Issues https://github.com/tk0miya/testing.postgresql/issues Download https://pypi.python.org/pypi/testing.postgresql
Use pip::
$ pip install testing.postgresql
And testing.postgresql
requires PostgreSQL server in your PATH.
Create PostgreSQL instance using testing.postgresql.Postgresql
::
import testing.postgresql from sqlalchemy import create_engine
with testing.postgresql.Postgresql() as 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...
#
testing.postgresql.Postgresql
executes initdb
and postgres
on instantiation.
On deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory.
If you want a database including tables and any fixtures for your apps,
use copy_data_from
keyword::
postgresql = testing.postgresql.Postgresql(copy_data_from='/path/to/your/database')
For example, you can setup new PostgreSQL server for each testcases on setUp() method::
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
invokes initdb
command on every instantiation.
That is very simple. But, in many cases, it is very waste that generating brandnew database for each testcase.
To optimize the behavior, use testing.postgresql.PostgresqlFactory
.
The factory class is able to cache the generated database beyond the testcases,
and it reduces the number of invocation of initdb
command::
import unittest import testing.postgresql
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
def tearDownModule(self):
Postgresql.clear_cache()
class MyTestCase(unittest.TestCase): def setUp(self):
self.postgresql = Postgresql()
def tearDown(self):
self.postgresql.stop()
If you want to insert fixtures to the cached database, use initdb_handler
option::
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()
handler()
on initialize databasePostgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True, on_initialized=handler)
Apache License 2.0
testing.common.database
packageFix bug:
pg8000
for connector to create test databasepostgres
to create test database (instead of template1
)Fix bugs:
Fix bugs: