About
=====
``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
Install
=======
Use pip::
$ pip install testing.postgresql
And ``testing.postgresql`` requires PostgreSQL server in your PATH.
Usage
=====
Create PostgreSQL instance using ``testing.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`` 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::
# uses a copy of specified data directory of PostgreSQL.
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()
To make your tests faster
-------------------------
``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
# 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()
If you want to insert fixtures to the cached database, use ``initdb_handler`` option::
# 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)
Requirements
============
* Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
* pg8000 1.10
License
=======
Apache License 2.0
History
=======
1.3.0 (2016-02-03)
-------------------
* Add testing.postgresql.PostgresqlFactory
* Depend on ``testing.common.database`` package
1.2.1 (2015-08-22)
-------------------
* Fix bug:
- Close #3 Fix AttributeError on end of tests
1.2.0 (2015-05-17)
-------------------
* Use `pg8000` for connector to create test database
* Connect to `postgres` to create test database (instead of `template1`)
1.1.2 (2015-04-06)
-------------------
* Fix bugs:
- Do not call os.getpid() on destructor (if not needed)
- Raise detailed RuntimeError if initdb exits non-zero
1.1.1 (2015-01-18)
-------------------
* Disable logging_collector feature (For Fedora)
* Fix bugs:
- MacPorts default path is /opt/local/lib/postgresql*, no dash
1.1.0 (2014-12-20)
-------------------
* Invoke 'postgres' command instead of 'postmaster'
1.0.6 (2014-07-19)
-------------------
* Fix #1 Dirty postmaster shut down
1.0.5 (2014-07-19)
-------------------
* Fix path for PostgreSQL
* Use absolute path for which command
1.0.4 (2014-06-19)
-------------------
* Fix timeout on terminating postgresql
* Support PostgreSQL on /usr/local/bin (cf. FreeBSD ports)
* Fix bugs
1.0.3 (2014-06-11)
-------------------
* Fix ImportError if caught SIGINT on py3
1.0.2 (2013-12-06)
-------------------
* Change behavior: Postgresql#stop() cleans workdir
* Fix caught AttributeError on object deletion
1.0.1 (2013-12-05)
-------------------
* Add @skipIfNotInstalled decorator (alias of skipIfNotFound)
* Suport python 2.6 and 3.2
1.0.0 (2013-12-04)
-------------------
* Add @skipIfNotFound decorator
0.1.0 (2013-11-26)
-------------------
* First release
Raw data
{
"_id": null,
"home_page": "https://github.com/tk0miya/testing.postgresql",
"name": "testing.postgresql",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Takeshi Komiya",
"author_email": "i.tkomiya at gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3a/5b/3bf1323697c4f4f0e8fb5c14d082dc2f005385ea139b19646c0fc9f1dbb7/testing.postgresql-1.3.0.tar.gz",
"platform": "UNKNOWN",
"description": "About\n=====\n``testing.postgresql`` automatically setups a postgresql instance in a temporary directory, and destroys it after testing.\n\n.. image:: https://travis-ci.org/tk0miya/testing.postgresql.svg?branch=master\n :target: https://travis-ci.org/tk0miya/testing.postgresql\n\n.. image:: https://coveralls.io/repos/tk0miya/testing.postgresql/badge.png?branch=master\n :target: https://coveralls.io/r/tk0miya/testing.postgresql?branch=master\n\n.. image:: https://codeclimate.com/github/tk0miya/testing.postgresql/badges/gpa.svg\n :target: https://codeclimate.com/github/tk0miya/testing.postgresql\n\n\nDocumentation\n https://github.com/tk0miya/testing.postgresql\nIssues\n https://github.com/tk0miya/testing.postgresql/issues\nDownload\n https://pypi.python.org/pypi/testing.postgresql\n\nInstall\n=======\nUse pip::\n\n $ pip install testing.postgresql\n\nAnd ``testing.postgresql`` requires PostgreSQL server in your PATH.\n\n\nUsage\n=====\nCreate PostgreSQL instance using ``testing.postgresql.Postgresql``::\n\n import testing.postgresql\n from sqlalchemy import create_engine\n\n # Lanuch new PostgreSQL server\n with testing.postgresql.Postgresql() as postgresql:\n # connect to PostgreSQL\n engine = create_engine(postgresql.url())\n\n # if you use postgresql or other drivers:\n # import psycopg2\n # db = psycopg2.connect(**postgresql.dsn())\n\n #\n # do any tests using PostgreSQL...\n #\n\n # PostgreSQL server is terminated here\n\n\n``testing.postgresql.Postgresql`` executes ``initdb`` and ``postgres`` on instantiation.\nOn deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory.\n\nIf you want a database including tables and any fixtures for your apps,\nuse ``copy_data_from`` keyword::\n\n # uses a copy of specified data directory of PostgreSQL.\n postgresql = testing.postgresql.Postgresql(copy_data_from='/path/to/your/database')\n\n\nFor example, you can setup new PostgreSQL server for each testcases on setUp() method::\n\n import unittest\n import testing.postgresql\n\n class MyTestCase(unittest.TestCase):\n def setUp(self):\n self.postgresql = testing.postgresql.Postgresql()\n\n def tearDown(self):\n self.postgresql.stop()\n\n\nTo make your tests faster\n-------------------------\n\n``testing.postgresql.Postgresql`` invokes ``initdb`` command on every instantiation.\nThat is very simple. But, in many cases, it is very waste that generating brandnew database for each testcase.\n\nTo optimize the behavior, use ``testing.postgresql.PostgresqlFactory``.\nThe factory class is able to cache the generated database beyond the testcases,\nand it reduces the number of invocation of ``initdb`` command::\n\n import unittest\n import testing.postgresql\n\n # Generate Postgresql class which shares the generated database\n Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)\n\n\n def tearDownModule(self):\n # clear cached database at end of tests\n Postgresql.clear_cache()\n\n\n class MyTestCase(unittest.TestCase):\n def setUp(self):\n # Use the generated Postgresql class instead of testing.postgresql.Postgresql\n self.postgresql = Postgresql()\n\n def tearDown(self):\n self.postgresql.stop()\n\nIf you want to insert fixtures to the cached database, use ``initdb_handler`` option::\n\n # create initial data on create as fixtures into the database\n def handler(postgresql):\n conn = psycopg2.connect(**postgresql.dsn())\n cursor = conn.cursor()\n cursor.execute(\"CREATE TABLE hello(id int, value varchar(256))\")\n cursor.execute(\"INSERT INTO hello values(1, 'hello'), (2, 'ciao')\")\n cursor.close()\n conn.commit()\n conn.close()\n\n # Use `handler()` on initialize database\n Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True,\n on_initialized=handler)\n\n\nRequirements\n============\n* Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5\n* pg8000 1.10\n\nLicense\n=======\nApache License 2.0\n\n\nHistory\n=======\n\n1.3.0 (2016-02-03)\n-------------------\n* Add testing.postgresql.PostgresqlFactory\n* Depend on ``testing.common.database`` package\n\n1.2.1 (2015-08-22)\n-------------------\n* Fix bug:\n\n - Close #3 Fix AttributeError on end of tests\n\n1.2.0 (2015-05-17)\n-------------------\n* Use `pg8000` for connector to create test database\n* Connect to `postgres` to create test database (instead of `template1`)\n\n1.1.2 (2015-04-06)\n-------------------\n* Fix bugs:\n\n - Do not call os.getpid() on destructor (if not needed)\n - Raise detailed RuntimeError if initdb exits non-zero\n\n1.1.1 (2015-01-18)\n-------------------\n* Disable logging_collector feature (For Fedora)\n* Fix bugs:\n\n - MacPorts default path is /opt/local/lib/postgresql*, no dash\n\n1.1.0 (2014-12-20)\n-------------------\n* Invoke 'postgres' command instead of 'postmaster'\n\n1.0.6 (2014-07-19)\n-------------------\n* Fix #1 Dirty postmaster shut down\n\n1.0.5 (2014-07-19)\n-------------------\n* Fix path for PostgreSQL\n* Use absolute path for which command\n\n1.0.4 (2014-06-19)\n-------------------\n* Fix timeout on terminating postgresql\n* Support PostgreSQL on /usr/local/bin (cf. FreeBSD ports)\n* Fix bugs\n\n1.0.3 (2014-06-11)\n-------------------\n* Fix ImportError if caught SIGINT on py3\n\n1.0.2 (2013-12-06)\n-------------------\n* Change behavior: Postgresql#stop() cleans workdir\n* Fix caught AttributeError on object deletion\n\n1.0.1 (2013-12-05)\n-------------------\n* Add @skipIfNotInstalled decorator (alias of skipIfNotFound)\n* Suport python 2.6 and 3.2\n\n1.0.0 (2013-12-04)\n-------------------\n* Add @skipIfNotFound decorator\n\n0.1.0 (2013-11-26)\n-------------------\n* First release",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "automatically setups a postgresql instance in a temporary directory, and destroys it after testing",
"version": "1.3.0",
"project_urls": {
"Download": "UNKNOWN",
"Homepage": "https://github.com/tk0miya/testing.postgresql"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1176d614d4bc950d961a73c952e9a2e0956d02d0869a86d3dfad070376863988",
"md5": "34079e16cc477a9396374d1a075ff784",
"sha256": "1b41daeb98dfc8cd4a584bb91e8f5f4ab182993870f95257afe5f1ba6151a598"
},
"downloads": -1,
"filename": "testing.postgresql-1.3.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "34079e16cc477a9396374d1a075ff784",
"packagetype": "bdist_wheel",
"python_version": "2.7",
"requires_python": null,
"size": 8901,
"upload_time": "2016-02-04T13:57:11",
"upload_time_iso_8601": "2016-02-04T13:57:11.857386Z",
"url": "https://files.pythonhosted.org/packages/11/76/d614d4bc950d961a73c952e9a2e0956d02d0869a86d3dfad070376863988/testing.postgresql-1.3.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3a5b3bf1323697c4f4f0e8fb5c14d082dc2f005385ea139b19646c0fc9f1dbb7",
"md5": "51441a021c72c8f85182294249d4ca49",
"sha256": "8e1a69760369a7a8ffe63a66b6d95a5cd82db2fb976e4a8f85ffd24fbfc447d8"
},
"downloads": -1,
"filename": "testing.postgresql-1.3.0.tar.gz",
"has_sig": false,
"md5_digest": "51441a021c72c8f85182294249d4ca49",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11000,
"upload_time": "2016-02-04T13:57:05",
"upload_time_iso_8601": "2016-02-04T13:57:05.396190Z",
"url": "https://files.pythonhosted.org/packages/3a/5b/3bf1323697c4f4f0e8fb5c14d082dc2f005385ea139b19646c0fc9f1dbb7/testing.postgresql-1.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2016-02-04 13:57:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tk0miya",
"github_project": "testing.postgresql",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "testing.postgresql"
}