pg-docker


Namepg-docker JSON
Version 0.9.0 PyPI version JSON
download
home_page
Summary
upload_time2023-04-10 22:57:32
maintainer
docs_urlNone
authorYour Name
requires_python>=3.9,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PG Docker

A python package to provide containerized postgres databases in python

**Why would you need this?**

If you are using postgres and want to write tests that run against a real database, then this will make your life easier.

## Installation

Install via pip:
```
pip install pg-docker
```

You will also need to have [docker](https://www.docker.com/).

## Usage

Note: *This package is mainly built with pytest in mind, but you can use the context managers documented below with other testing frameworks as well.*

### Example

With pytest:
```py
import psycopg2


pytest_plugins = ["pg_docker"]


def test_using_a_database(pg_database):
    db_connection = psycopg2.connect(**pg_database.connection_kwargs())
    cursor = db_connection.cursor()
    cursor.execute("SELECT 'hello world!'")

    assert cursor.fetchone() == ("hello world!",)
```

### Usage with pytest

You first need to enable the plugin. To do this add a `conftest.py` to the root directory of your tests and add:
```py
pytest_plugins = ["pg_docker"]
```
You can find more details on how to activate plugins in the [pytest docs](https://docs.pytest.org/en/latest/how-to/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file)

The plugin The following fixtures:

 - `pg_database`: `DatabaseParams` for a clean database.
 - `pg_database_pool`: A `DatabasePool` instance. Use this if you need more than one database in your tests at a time.


### Configuring Database Migrations

Use the below template in your `conftest.py` to configure how your databases are set up. 
```py
def setup_db(pg_params):
    """Add any setup logic for your database in here."""
    pass

@pytest.fixture(scope="session")
def pg_setup_db():
    return setup_db
```
Note: *You might be inclined to edit the above code to nest the setup_db function inside of the fixture function. This will not work, because the fixture result needs to be [pickleable](https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled)!*


### Advanced Usage (and other testing frameworks)

For other use cases you can use the `database_pool` context manager:
```py
with database_pool() as db_pool:
    with db_pool.database as db_params:
        connection = psycopg2.connect(**db_params.connection_kwargs())
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pg-docker",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Your Name",
    "author_email": "you@example.com",
    "download_url": "https://files.pythonhosted.org/packages/a5/cf/e8a81bfe3f02dd5ae7c32239a6559de5c88bc2959c64a9aa8560cbec108b/pg_docker-0.9.0.tar.gz",
    "platform": null,
    "description": "# PG Docker\n\nA python package to provide containerized postgres databases in python\n\n**Why would you need this?**\n\nIf you are using postgres and want to write tests that run against a real database, then this will make your life easier.\n\n## Installation\n\nInstall via pip:\n```\npip install pg-docker\n```\n\nYou will also need to have [docker](https://www.docker.com/).\n\n## Usage\n\nNote: *This package is mainly built with pytest in mind, but you can use the context managers documented below with other testing frameworks as well.*\n\n### Example\n\nWith pytest:\n```py\nimport psycopg2\n\n\npytest_plugins = [\"pg_docker\"]\n\n\ndef test_using_a_database(pg_database):\n    db_connection = psycopg2.connect(**pg_database.connection_kwargs())\n    cursor = db_connection.cursor()\n    cursor.execute(\"SELECT 'hello world!'\")\n\n    assert cursor.fetchone() == (\"hello world!\",)\n```\n\n### Usage with pytest\n\nYou first need to enable the plugin. To do this add a `conftest.py` to the root directory of your tests and add:\n```py\npytest_plugins = [\"pg_docker\"]\n```\nYou can find more details on how to activate plugins in the [pytest docs](https://docs.pytest.org/en/latest/how-to/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file)\n\nThe plugin The following fixtures:\n\n - `pg_database`: `DatabaseParams` for a clean database.\n - `pg_database_pool`: A `DatabasePool` instance. Use this if you need more than one database in your tests at a time.\n\n\n### Configuring Database Migrations\n\nUse the below template in your `conftest.py` to configure how your databases are set up. \n```py\ndef setup_db(pg_params):\n    \"\"\"Add any setup logic for your database in here.\"\"\"\n    pass\n\n@pytest.fixture(scope=\"session\")\ndef pg_setup_db():\n    return setup_db\n```\nNote: *You might be inclined to edit the above code to nest the setup_db function inside of the fixture function. This will not work, because the fixture result needs to be [pickleable](https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled)!*\n\n\n### Advanced Usage (and other testing frameworks)\n\nFor other use cases you can use the `database_pool` context manager:\n```py\nwith database_pool() as db_pool:\n    with db_pool.database as db_params:\n        connection = psycopg2.connect(**db_params.connection_kwargs())\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "",
    "version": "0.9.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0b4144577136a5b8ed0c7ba5a16b3fb721e79c1607f29ba6aa9076d8219f7cfa",
                "md5": "9f6dbfaa4fcc8621a5eaa90a4e49b520",
                "sha256": "91eb4685e615adb2581e6a446dd89fe0ca8fd2a38c47360b3e595ef4ed4823c6"
            },
            "downloads": -1,
            "filename": "pg_docker-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9f6dbfaa4fcc8621a5eaa90a4e49b520",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4.0",
            "size": 5889,
            "upload_time": "2023-04-10T22:57:30",
            "upload_time_iso_8601": "2023-04-10T22:57:30.177268Z",
            "url": "https://files.pythonhosted.org/packages/0b/41/44577136a5b8ed0c7ba5a16b3fb721e79c1607f29ba6aa9076d8219f7cfa/pg_docker-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5cfe8a81bfe3f02dd5ae7c32239a6559de5c88bc2959c64a9aa8560cbec108b",
                "md5": "76f42ba3bac1b544f4b805997ae15dde",
                "sha256": "0b38d544a9db552d0df10a6ac26f43429e9cb1b6b2ed81f1ed48e58cda5f359d"
            },
            "downloads": -1,
            "filename": "pg_docker-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "76f42ba3bac1b544f4b805997ae15dde",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4.0",
            "size": 5353,
            "upload_time": "2023-04-10T22:57:32",
            "upload_time_iso_8601": "2023-04-10T22:57:32.408213Z",
            "url": "https://files.pythonhosted.org/packages/a5/cf/e8a81bfe3f02dd5ae7c32239a6559de5c88bc2959c64a9aa8560cbec108b/pg_docker-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-10 22:57:32",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "pg-docker"
}
        
Elapsed time: 0.13429s