pgtestdbpy


Namepgtestdbpy JSON
Version 0.0.1 PyPI version JSON
download
home_page
Summarypgtestdb Python clone
upload_time2024-01-18 16:12:39
maintainer
docs_urlNone
author
requires_python>=3.7
licenseMIT License Copyright (c) 2024 Oliver Russell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords postgres testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pgtestdbpy

_Python clone of [pgtestdb](https://github.com/peterldowns/pgtestdb)._

```bash
pip install pgtestdbpy
```

In summary, it's a couple of helper functions that allow you to quickly clone Postgres databases that you've applied migrations to. In a small number of milliseconds, each test (including tests running in parallel) gets a fresh db with empty tables, all the sequences reset, etc.

In developing this on my mac, for reasons I don't quite understand, running with Postgres in docker (via [colima](https://github.com/abiosoft/colima)) was substantially quicker that running Postgres natively. So I agree with Peter's advice, just copy [this file](docker-compose.yml) and `docker compose up -d db`.

There are two context managers that can be used in conjunction or independently depending on test setup:

- `pgtestdbpy.templates(config, migrators)`:
    - Creates a new user and database for a migrator.
    - Runs the set of migrations.
    - Marks the database as a `TEMPLATE DATABASE` so that it can be cheaply cloned.
    - Yields.
    - Drops the template database and the user.
- `pgtestdbpy.clone(config, migrator)`:
    - Does a `CREATE DATABASE WITH TEMPLATE` (from a template database made above) giving it a unique random name.
    - Yields a Postgres url for it.
    - Drops the database.

Example `conftest.py` usage below, in theory (I haven't tested this) it should be easy to run tests in parallel using the `conn` fixture - each with a separate database instance - and [pytest-xdist](https://github.com/pytest-dev/pytest-xdist) or equivalent. In this example we just

```python
from typing import Iterator
import pgtestdbpy
import psycopg
import pytest

def migrate(url: str) -> None:
    with psycopg.connect(url) as conn:
        conn.execute("CREATE TABLE foo (a INT)")

migrator = pgtestdbpy.Migrator(migrate)
config = pgtestdbpy.Config()

@pytest.fixture(scope="session")
def db() -> Iterator[None]:
    with pgtestdbpy.templates(config, migrator):
        yield

@pytest.fixture()
def conn(db) -> Iterator[pgtestdbpy.PsycoConn]:
    with pgtestdbpy.clone(config, migrator) as url:
        with psycopg.connect(url) as _conn:
            yield _conn
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pgtestdbpy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Oliver Russell <ojhrussell@gmail.com>",
    "keywords": "postgres,testing",
    "author": "",
    "author_email": "Oliver Russell <ojhrussell@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/08/e1/e44b10fa741c44a610a51179b317f1b353e3ea4665a97ef5f4dd1084d2ec/pgtestdbpy-0.0.1.tar.gz",
    "platform": null,
    "description": "# pgtestdbpy\n\n_Python clone of [pgtestdb](https://github.com/peterldowns/pgtestdb)._\n\n```bash\npip install pgtestdbpy\n```\n\nIn summary, it's a couple of helper functions that allow you to quickly clone Postgres databases that you've applied migrations to. In a small number of milliseconds, each test (including tests running in parallel) gets a fresh db with empty tables, all the sequences reset, etc.\n\nIn developing this on my mac, for reasons I don't quite understand, running with Postgres in docker (via [colima](https://github.com/abiosoft/colima)) was substantially quicker that running Postgres natively. So I agree with Peter's advice, just copy [this file](docker-compose.yml) and `docker compose up -d db`.\n\nThere are two context managers that can be used in conjunction or independently depending on test setup:\n\n- `pgtestdbpy.templates(config, migrators)`:\n    - Creates a new user and database for a migrator.\n    - Runs the set of migrations.\n    - Marks the database as a `TEMPLATE DATABASE` so that it can be cheaply cloned.\n    - Yields.\n    - Drops the template database and the user.\n- `pgtestdbpy.clone(config, migrator)`:\n    - Does a `CREATE DATABASE WITH TEMPLATE` (from a template database made above) giving it a unique random name.\n    - Yields a Postgres url for it.\n    - Drops the database.\n\nExample `conftest.py` usage below, in theory (I haven't tested this) it should be easy to run tests in parallel using the `conn` fixture - each with a separate database instance - and [pytest-xdist](https://github.com/pytest-dev/pytest-xdist) or equivalent. In this example we just\n\n```python\nfrom typing import Iterator\nimport pgtestdbpy\nimport psycopg\nimport pytest\n\ndef migrate(url: str) -> None:\n    with psycopg.connect(url) as conn:\n        conn.execute(\"CREATE TABLE foo (a INT)\")\n\nmigrator = pgtestdbpy.Migrator(migrate)\nconfig = pgtestdbpy.Config()\n\n@pytest.fixture(scope=\"session\")\ndef db() -> Iterator[None]:\n    with pgtestdbpy.templates(config, migrator):\n        yield\n\n@pytest.fixture()\ndef conn(db) -> Iterator[pgtestdbpy.PsycoConn]:\n    with pgtestdbpy.clone(config, migrator) as url:\n        with psycopg.connect(url) as _conn:\n            yield _conn\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Oliver Russell  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "pgtestdb Python clone",
    "version": "0.0.1",
    "project_urls": {
        "documentation": "https://github.com/leontrolski/pgtestdbpy",
        "homepage": "https://github.com/leontrolski/pgtestdbpy",
        "repository": "https://github.com/leontrolski/pgtestdbpy.git"
    },
    "split_keywords": [
        "postgres",
        "testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5739c0e9275b683cf8fa15c89e990708d709fc4ae0e7255a06dbe602d3612082",
                "md5": "6acde00424a41e7eac75137557365a0e",
                "sha256": "844dc07ac29e437069446ed98a29ed315ec33373779564e32d2270626ac2cb35"
            },
            "downloads": -1,
            "filename": "pgtestdbpy-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6acde00424a41e7eac75137557365a0e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5018,
            "upload_time": "2024-01-18T16:12:30",
            "upload_time_iso_8601": "2024-01-18T16:12:30.658186Z",
            "url": "https://files.pythonhosted.org/packages/57/39/c0e9275b683cf8fa15c89e990708d709fc4ae0e7255a06dbe602d3612082/pgtestdbpy-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "08e1e44b10fa741c44a610a51179b317f1b353e3ea4665a97ef5f4dd1084d2ec",
                "md5": "2e081fe033503deb5655fc40e23f58dc",
                "sha256": "6860716c1091d007a488e62d0484a1e5081570eab10eed18e70513d0fdb75bee"
            },
            "downloads": -1,
            "filename": "pgtestdbpy-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2e081fe033503deb5655fc40e23f58dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 4544,
            "upload_time": "2024-01-18T16:12:39",
            "upload_time_iso_8601": "2024-01-18T16:12:39.020752Z",
            "url": "https://files.pythonhosted.org/packages/08/e1/e44b10fa741c44a610a51179b317f1b353e3ea4665a97ef5f4dd1084d2ec/pgtestdbpy-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-18 16:12:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "leontrolski",
    "github_project": "pgtestdbpy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pgtestdbpy"
}
        
Elapsed time: 0.18396s