pytest-mock-resources


Namepytest-mock-resources JSON
Version 2.10.1 PyPI version JSON
download
home_pagehttps://github.com/schireson/pytest-mock-resources
SummaryA pytest plugin for easily instantiating reproducible mock resources.
upload_time2024-03-06 14:44:29
maintainer
docs_urlNone
authorOmar Khan
requires_python>=3.7,<4
licenseMIT
keywords pytest sqlalchemy docker fixture mock
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            ![CircleCI](https://img.shields.io/circleci/build/gh/schireson/pytest-mock-resources/master)
[![codecov](https://codecov.io/gh/schireson/pytest-mock-resources/branch/master/graph/badge.svg)](https://codecov.io/gh/schireson/pytest-mock-resources)
[![Documentation
Status](https://readthedocs.org/projects/pytest-mock-resources/badge/?version=latest)](https://pytest-mock-resources.readthedocs.io/en/latest/?badge=latest)

## Introduction

Code which depends on external resources such a databases (postgres, redshift, etc) can be difficult
to write automated tests for. Conventional wisdom might be to mock or stub out the actual database
calls and assert that the code works correctly before/after the calls.

However take the following, _simple_ example:

```python
def serialize(users):
    return [
        {
            'user': user.serialize(),
            'address': user.address.serialize(),
            'purchases': [p.serialize() for p in user.purchases],
        }
        for user in users
    ]

def view_function(session):
    users = session.query(User).join(Address).options(selectinload(User.purchases)).all()
    return serialize(users)
```

Sure, you can test `serialize`, but whether the actual **query** did the correct thing _truly_
requires that you execute the query.

## The Pitch

Having tests depend upon a **real** postgres instance running somewhere is a pain, very fragile, and
prone to issues across machines and test failures.

Therefore `pytest-mock-resources` (primarily) works by managing the lifecycle of docker containers
and providing access to them inside your tests.

As such, this package makes 2 primary assumptions:

- You're using `pytest` (hopefully that's appropriate, given the package name)
- For many resources, `docker` is required to be available and running (or accessible through remote
  docker).

If you aren't familiar with Pytest Fixtures, you can read up on them in the [Pytest
documentation](https://docs.pytest.org/en/latest/fixture.html).

In the above example, your test file could look something like

```python
from pytest_mock_resources import create_postgres_fixture
from models import ModelBase

pg = create_postgres_fixture(ModelBase, session=True)

def test_view_function_empty_db(pg):
  response = view_function(pg)
  assert response == ...

def test_view_function_user_without_purchases(pg):
  pg.add(User(...))
  pg.flush()

  response = view_function(pg)
  assert response == ...

def test_view_function_user_with_purchases(pg):
  pg.add(User(..., purchases=[Purchase(...)]))
  pg.flush()

  response = view_function(pg)
  assert response == ...
```

## Existing Resources (many more possible)

- SQLite

  ```python
  from pytest_mock_resources import create_sqlite_fixture
  ```

- Postgres

  ```python
  from pytest_mock_resources import create_postgres_fixture
  ```

- Redshift

  **note** Uses postgres under the hood, but the fixture tries to support as much redshift
  functionality as possible (including redshift's `COPY`/`UNLOAD` commands).

  ```python
  from pytest_mock_resources import create_redshift_fixture
  ```

- Mongo

  ```python
  from pytest_mock_resources import create_mongo_fixture
  ```

- Redis

  ```python
  from pytest_mock_resources import create_redis_fixture
  ```

- MySQL

  ```python
  from pytest_mock_resources import create_mysql_fixture
  ```

- Moto

  ```python
  from pytest_mock_resources import create_moto_fixture
  ```

## Features

General features include:

- Support for "actions" which pre-populate the resource you're mocking before the test
- [Async fixtures](https://pytest-mock-resources.readthedocs.io/en/latest/async.html)
- Custom configuration for container/resource startup

## Installation

```bash
# Basic fixture support i.e. SQLite
pip install "pytest-mock-resources"

# General, docker-based fixture support
pip install "pytest-mock-resources[docker]"

# Mongo fixture support, installs `pymongo`
pip install "pytest-mock-resources[mongo]"

# Moto fixture support, installs non-driver extras specific to moto support
pip install "pytest-mock-resources[moto]"

# Redis fixture support, Installs `redis` client
pip install "pytest-mock-resources[redis]"

# Redshift fixture support, installs non-driver extras specific to redshift support
pip install "pytest-mock-resources[redshift]"
```

Additionally there are number of **convenience** extras currently provided
for installing drivers/clients of specific features. However in most cases,
you **should** already be installing the driver/client used for that fixture
as as first-party dependency of your project.

As such, we recommend against using these extras, and instead explcitly depending
on the package in question in your own project's 1st party dependencies.

```bash
# Installs psycopg2/psycopg2-binary driver
pip install "pytest-mock-resources[postgres-binary]"
pip install "pytest-mock-resources[postgres]"

# Installs asyncpg driver
pip install "pytest-mock-resources[postgres-async]"

# Installs pymysql driver
pip install "pytest-mock-resources[mysql]"
```

## Possible Future Resources

- Rabbit Broker
- AWS Presto

Feel free to file an [issue](https://github.com/schireson/pytest-mock-resources/issues) if you find
any bugs or want to start a conversation around a mock resource you want implemented!

## Python 2

Releases in the 1.x series were supportive of python 2. However starting from 2.0.0, support for
python 2 was dropped. We may accept bugfix PRs for the 1.x series, however new development and
features will not be backported.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/schireson/pytest-mock-resources",
    "name": "pytest-mock-resources",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4",
    "maintainer_email": "",
    "keywords": "pytest,sqlalchemy,docker,fixture,mock",
    "author": "Omar Khan",
    "author_email": "oakhan3@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0c/ab/9f7ecba7f8df17cb10bfa678b810950b2e84787a6a31f8acb5a81020ff42/pytest_mock_resources-2.10.1.tar.gz",
    "platform": null,
    "description": "![CircleCI](https://img.shields.io/circleci/build/gh/schireson/pytest-mock-resources/master)\n[![codecov](https://codecov.io/gh/schireson/pytest-mock-resources/branch/master/graph/badge.svg)](https://codecov.io/gh/schireson/pytest-mock-resources)\n[![Documentation\nStatus](https://readthedocs.org/projects/pytest-mock-resources/badge/?version=latest)](https://pytest-mock-resources.readthedocs.io/en/latest/?badge=latest)\n\n## Introduction\n\nCode which depends on external resources such a databases (postgres, redshift, etc) can be difficult\nto write automated tests for. Conventional wisdom might be to mock or stub out the actual database\ncalls and assert that the code works correctly before/after the calls.\n\nHowever take the following, _simple_ example:\n\n```python\ndef serialize(users):\n    return [\n        {\n            'user': user.serialize(),\n            'address': user.address.serialize(),\n            'purchases': [p.serialize() for p in user.purchases],\n        }\n        for user in users\n    ]\n\ndef view_function(session):\n    users = session.query(User).join(Address).options(selectinload(User.purchases)).all()\n    return serialize(users)\n```\n\nSure, you can test `serialize`, but whether the actual **query** did the correct thing _truly_\nrequires that you execute the query.\n\n## The Pitch\n\nHaving tests depend upon a **real** postgres instance running somewhere is a pain, very fragile, and\nprone to issues across machines and test failures.\n\nTherefore `pytest-mock-resources` (primarily) works by managing the lifecycle of docker containers\nand providing access to them inside your tests.\n\nAs such, this package makes 2 primary assumptions:\n\n- You're using `pytest` (hopefully that's appropriate, given the package name)\n- For many resources, `docker` is required to be available and running (or accessible through remote\n  docker).\n\nIf you aren't familiar with Pytest Fixtures, you can read up on them in the [Pytest\ndocumentation](https://docs.pytest.org/en/latest/fixture.html).\n\nIn the above example, your test file could look something like\n\n```python\nfrom pytest_mock_resources import create_postgres_fixture\nfrom models import ModelBase\n\npg = create_postgres_fixture(ModelBase, session=True)\n\ndef test_view_function_empty_db(pg):\n  response = view_function(pg)\n  assert response == ...\n\ndef test_view_function_user_without_purchases(pg):\n  pg.add(User(...))\n  pg.flush()\n\n  response = view_function(pg)\n  assert response == ...\n\ndef test_view_function_user_with_purchases(pg):\n  pg.add(User(..., purchases=[Purchase(...)]))\n  pg.flush()\n\n  response = view_function(pg)\n  assert response == ...\n```\n\n## Existing Resources (many more possible)\n\n- SQLite\n\n  ```python\n  from pytest_mock_resources import create_sqlite_fixture\n  ```\n\n- Postgres\n\n  ```python\n  from pytest_mock_resources import create_postgres_fixture\n  ```\n\n- Redshift\n\n  **note** Uses postgres under the hood, but the fixture tries to support as much redshift\n  functionality as possible (including redshift's `COPY`/`UNLOAD` commands).\n\n  ```python\n  from pytest_mock_resources import create_redshift_fixture\n  ```\n\n- Mongo\n\n  ```python\n  from pytest_mock_resources import create_mongo_fixture\n  ```\n\n- Redis\n\n  ```python\n  from pytest_mock_resources import create_redis_fixture\n  ```\n\n- MySQL\n\n  ```python\n  from pytest_mock_resources import create_mysql_fixture\n  ```\n\n- Moto\n\n  ```python\n  from pytest_mock_resources import create_moto_fixture\n  ```\n\n## Features\n\nGeneral features include:\n\n- Support for \"actions\" which pre-populate the resource you're mocking before the test\n- [Async fixtures](https://pytest-mock-resources.readthedocs.io/en/latest/async.html)\n- Custom configuration for container/resource startup\n\n## Installation\n\n```bash\n# Basic fixture support i.e. SQLite\npip install \"pytest-mock-resources\"\n\n# General, docker-based fixture support\npip install \"pytest-mock-resources[docker]\"\n\n# Mongo fixture support, installs `pymongo`\npip install \"pytest-mock-resources[mongo]\"\n\n# Moto fixture support, installs non-driver extras specific to moto support\npip install \"pytest-mock-resources[moto]\"\n\n# Redis fixture support, Installs `redis` client\npip install \"pytest-mock-resources[redis]\"\n\n# Redshift fixture support, installs non-driver extras specific to redshift support\npip install \"pytest-mock-resources[redshift]\"\n```\n\nAdditionally there are number of **convenience** extras currently provided\nfor installing drivers/clients of specific features. However in most cases,\nyou **should** already be installing the driver/client used for that fixture\nas as first-party dependency of your project.\n\nAs such, we recommend against using these extras, and instead explcitly depending\non the package in question in your own project's 1st party dependencies.\n\n```bash\n# Installs psycopg2/psycopg2-binary driver\npip install \"pytest-mock-resources[postgres-binary]\"\npip install \"pytest-mock-resources[postgres]\"\n\n# Installs asyncpg driver\npip install \"pytest-mock-resources[postgres-async]\"\n\n# Installs pymysql driver\npip install \"pytest-mock-resources[mysql]\"\n```\n\n## Possible Future Resources\n\n- Rabbit Broker\n- AWS Presto\n\nFeel free to file an [issue](https://github.com/schireson/pytest-mock-resources/issues) if you find\nany bugs or want to start a conversation around a mock resource you want implemented!\n\n## Python 2\n\nReleases in the 1.x series were supportive of python 2. However starting from 2.0.0, support for\npython 2 was dropped. We may accept bugfix PRs for the 1.x series, however new development and\nfeatures will not be backported.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A pytest plugin for easily instantiating reproducible mock resources.",
    "version": "2.10.1",
    "project_urls": {
        "Homepage": "https://github.com/schireson/pytest-mock-resources",
        "Repository": "https://github.com/schireson/pytest-mock-resources"
    },
    "split_keywords": [
        "pytest",
        "sqlalchemy",
        "docker",
        "fixture",
        "mock"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "356f971c110188d3dee9c75eb741f1a63a29b52f4fc744d77d64283002f04422",
                "md5": "b489af36b9e5350140b1f6d65b0c2a04",
                "sha256": "51969550bfe43ccf09f66b9454274e967b6faf05ca15c445707fab7be5cd9901"
            },
            "downloads": -1,
            "filename": "pytest_mock_resources-2.10.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b489af36b9e5350140b1f6d65b0c2a04",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4",
            "size": 56683,
            "upload_time": "2024-03-06T14:44:27",
            "upload_time_iso_8601": "2024-03-06T14:44:27.108268Z",
            "url": "https://files.pythonhosted.org/packages/35/6f/971c110188d3dee9c75eb741f1a63a29b52f4fc744d77d64283002f04422/pytest_mock_resources-2.10.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0cab9f7ecba7f8df17cb10bfa678b810950b2e84787a6a31f8acb5a81020ff42",
                "md5": "def09784c5190e48649ac1626305ebe4",
                "sha256": "191ae45405d46a82f652d6cd641cd3105802d9bb395ae750566e3a1611c597f4"
            },
            "downloads": -1,
            "filename": "pytest_mock_resources-2.10.1.tar.gz",
            "has_sig": false,
            "md5_digest": "def09784c5190e48649ac1626305ebe4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4",
            "size": 40899,
            "upload_time": "2024-03-06T14:44:29",
            "upload_time_iso_8601": "2024-03-06T14:44:29.379468Z",
            "url": "https://files.pythonhosted.org/packages/0c/ab/9f7ecba7f8df17cb10bfa678b810950b2e84787a6a31f8acb5a81020ff42/pytest_mock_resources-2.10.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-06 14:44:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "schireson",
    "github_project": "pytest-mock-resources",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "pytest-mock-resources"
}
        
Elapsed time: 0.21430s