{ "info": { "author": "Brian Sutherland", "author_email": "brian@vanguardistas.net", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Topic :: Database", "Topic :: Software Development :: Testing" ], "description": "Test PostgreSQL Databases\n=========================\n\nEasy creation of PostgreSQL databases (and clusters) for unit testing.\n\nDirty Databases\n---------------\n\nTest databases take a long time to create. In general you need to be a little\ncareful when you decide to delete/recreate a test database fixture.\n\nAlso, there seems to be no robust way in PostgreSQL of figuring out if a\ndatabase was committed to or not.\n\nSo van.pg has no choice but to place the responsibility on the you to notify\nit when a database is dirty. If this isn't done properly, test isolation will\nbe compromised. It's not ideal, but the best we can do.\n\nOne exception is if you consistently use the ``transaction`` package\n(http://pypi.python.org/pypi/transaction) to manage database commits. In this\ncase you can ask for the resource to be dirtied whenever a transaction is\ncommitted.\n\nIntegration with ``testresources``\n----------------------------------\n\nThe typical way to use these fixtures is via ``testresources``\n(http://pypi.python.org/pypi/testresources/):\n\n >>> from testresources import ResourcedTestCase\n >>> from van.pg import DatabaseManager\n >>> import psycopg2\n\n >>> def init_db(db):\n ... conn = psycopg2.connect(host=db.host, database=db.database)\n ... cur = conn.cursor()\n ... cur.execute(\"CREATE TABLE foo (bar INTEGER);\")\n ... conn.commit()\n ... conn.close()\n\n >>> class MyTest(ResourcedTestCase):\n ... \n ... resources = [('db', DatabaseManager(initialize_sql=init_db))]\n ... \n ... def runTest(self):\n ... conn = psycopg2.connect(host=self.db.host, database=self.db.database)\n ... cur = conn.cursor()\n ... cur.execute(\"INSERT INTO foo VALUES (1);\")\n ... conn.commit()\n ... cur = conn.cursor()\n ... cur.execute(\"SELECT * FROM foo\")\n ... self.assertEqual(cur.fetchall(), [(1, )])\n ... # NOTE: must close connections or dropping databases fails\n ... conn.close()\n ... self.db.dirtied() # we changed the DB, so it needs re-loading\n \nActually run the test:\n\n >>> from unittest import TextTestRunner\n >>> import sys\n >>> runner = TextTestRunner(stream=sys.stdout)\n >>> runner.run(MyTest()) # doctest: +ELLIPSIS\n .\n ...\n OK\n ...\n\nUsing template databases\n------------------------\n\nIf you need to recreate the same database many times, it can be faster to let\nPostgreSQL copy the database from a template database. You can do this by having one DatabaseManager serve as the template for another:\n\n >>> template_db = DatabaseManager(initialize_sql=init_db)\n >>> class MyTest2(MyTest):\n ... resources = [('db', DatabaseManager(template=template_db))]\n\n >>> runner.run(MyTest2()) # doctest: +ELLIPSIS\n .\n ...\n OK\n ...\n\n``transaction`` integration\n---------------------------\n\nIf the keyword argumen ``dirty_on_commit`` is True, a DatabaseManager will mark\nthe database as dirtied after every successfull commit made through the\n``transaction`` module. This means each test which dirties the database does not\nhave to manually notify it.\n\n >>> man = DatabaseManager(dirty_on_commit=True)\n\nIf you use this feature, you need to depend on the transaction\n(http://pypi.python.org/pypi/transaction) package yourself.\n\nUsing an existing database\n--------------------------\n\nBy default, van.pg creates a new PostgreSQL cluster in a temporary directory\nand launches a PostgreSQL daemon. This works most of the time, but is not very\nfast.\n\nIf you have an already running PostgreSQL cluster, you can tell van.pg to use\nit by setting the environment variable VAN_PG_HOST. For example, to run\nvan.pg's tests against a local PostgreSQL server with it's sockets in\n/tmp/pgcluster do:\n\n $ VAN_PG_HOST=/tmp/pgcluster python setup.py test\n\nWARNING: any databases starting with test_db in the target database are likely\nto be dropped.\n\nClosing Connections\n-------------------\n\nBe careful to properly close all connections to the database once your test is\ndone with it. PostgreSQL doesn't allow dropping databases while there are open\nconnections. This will cause van.pg to error when trying to drop the test\ndatabase.\n\nProgramatically creating a cluster\n----------------------------------\n\nAt a lower level, you can also programmatically manipulate your own PostgreSQL cluster.\n\nInitialize the Cluster:\n \n >>> from van.pg import Cluster\n >>> cluster = Cluster()\n >>> cluster.initdb()\n\nWhich creates a database in a temporary directory:\n\n >>> import os\n >>> dbdir = cluster.dbdir\n >>> 'PG_VERSION' in os.listdir(dbdir)\n True\n \nStart it:\n\n >>> cluster.start()\n\nCreate/Test a database:\n\n >>> dbname = cluster.createdb()\n \n\nWe can connect to the database:\n\n >>> import psycopg2\n >>> conn = psycopg2.connect(database=dbname, host=cluster.dbdir)\n >>> cur = conn.cursor()\n\nTwiddle the database to make sure we can do the basics:\n \n >>> cur.execute(\"CREATE TABLE x (y int)\")\n >>> cur.execute(\"INSERT INTO x VALUES (1)\")\n >>> conn.commit()\n >>> cur.execute(\"SELECT * from x\")\n >>> cur.fetchall()[0][0]\n 1\n\nStop the cluster daemon:\n\n >>> conn.close()\n >>> cluster.stop()\n \nStart it again:\n\n >>> cluster.start()\n >>> conn = psycopg2.connect(database=dbname, host=cluster.dbdir)\n >>> cur = conn.cursor()\n >>> cur.execute(\"SELECT * from x\")\n >>> cur.fetchall()[0][0]\n 1\n\nAnd cleanup:\n\n >>> conn.close()\n >>> cluster.cleanup()\n >>> cluster.dbdir is None\n True\n >>> os.path.exists(dbdir)\n False\n\nDevelopment\n-----------\n\nDevelopment takes place on GitHub:\n\n http://github.com/jinty/van.pg\n\nCHANGES\n=======\n\n2.0 (unreleased)\n----------------\n\n* Support Python 3.2.\n\n* Drop Python 2.5 support.\n\n* Add tox.ini for testing against multiple python versions.\n\n* Run PostgreSQL as a subprocess rather than as a daemon (via pg_ctl).\n\n* Re-organize code to improve reuse and test coverage.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": null, "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "van.pg", "package_url": "https://pypi.org/project/van.pg/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/van.pg/", "project_urls": { "Download": "UNKNOWN", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/van.pg/2.0/", "requires_dist": null, "requires_python": null, "summary": "Tools to programmatically manage PostgreSQL clusters as Python test fixtures.", "version": "2.0" }, "last_serial": 642313, "releases": { "1.0": [], "1.2": [ { "comment_text": "", "digests": { "md5": "cdb3fbf02943071aa522c803c4da02f2", "sha256": "d3ac81938e57a0d6f77c01cd5881794bb663b8b756ea33580d2b0404eba4bef9" }, "downloads": -1, "filename": "van.pg-1.2.tar.gz", "has_sig": false, "md5_digest": "cdb3fbf02943071aa522c803c4da02f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6675, "upload_time": "2011-05-09T19:00:51", "url": "https://files.pythonhosted.org/packages/19/b2/bd9326c8c3e45f06d878ddb093355ddc1c711731a1119611acb86a70ea4e/van.pg-1.2.tar.gz" } ], "1.3": [ { "comment_text": "", "digests": { "md5": "21a14bf41aa018bd14b61fd477481124", "sha256": "f026830f90ebae31ee129b98f99b46eaf0ed722bc5be8c08cc18aad1c633b928" }, "downloads": -1, "filename": "van.pg-1.3.tar.gz", "has_sig": false, "md5_digest": "21a14bf41aa018bd14b61fd477481124", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8845, "upload_time": "2011-05-09T19:11:22", "url": "https://files.pythonhosted.org/packages/55/56/c90ee97408ac6a9fe567ea78fbaba304fcf215745360336132e4036eeb98/van.pg-1.3.tar.gz" } ], "1.4": [ { "comment_text": "", "digests": { "md5": "fd0f1b76c99ea207b3d0fa5be61789b8", "sha256": "ceb2f30c2e2be8c4dd21c7f0205ff41a3edf7618dc4a2f07639d6bf3a40665c3" }, "downloads": -1, "filename": "van.pg-1.4.tar.gz", "has_sig": false, "md5_digest": "fd0f1b76c99ea207b3d0fa5be61789b8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9101, "upload_time": "2011-05-16T10:49:57", "url": "https://files.pythonhosted.org/packages/65/47/7f82e9442fbed81e6676e4b6ab88aa29ca774d96c447cec1bfb70bcae57a/van.pg-1.4.tar.gz" } ], "2.0": [ { "comment_text": "", "digests": { "md5": "fb918a91731cce7dd4cf507688eaa4bd", "sha256": "4791483b5c0eb315f13f14c4459d01121796f9e03200aa6a62f72f7b38e296fe" }, "downloads": -1, "filename": "van.pg-2.0.tar.gz", "has_sig": false, "md5_digest": "fb918a91731cce7dd4cf507688eaa4bd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9824, "upload_time": "2012-05-13T21:51:15", "url": "https://files.pythonhosted.org/packages/db/6a/e6b8f52a0a72a86fa5824aef1ad5542bfae52287ab2c0ab7aa5a3d5941e7/van.pg-2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fb918a91731cce7dd4cf507688eaa4bd", "sha256": "4791483b5c0eb315f13f14c4459d01121796f9e03200aa6a62f72f7b38e296fe" }, "downloads": -1, "filename": "van.pg-2.0.tar.gz", "has_sig": false, "md5_digest": "fb918a91731cce7dd4cf507688eaa4bd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9824, "upload_time": "2012-05-13T21:51:15", "url": "https://files.pythonhosted.org/packages/db/6a/e6b8f52a0a72a86fa5824aef1ad5542bfae52287ab2c0ab7aa5a3d5941e7/van.pg-2.0.tar.gz" } ] }