{"id":14956788,"url":"https://github.com/schireson/pytest-mock-resources","last_synced_at":"2025-04-07T08:14:41.599Z","repository":{"id":37536539,"uuid":"164021717","full_name":"schireson/pytest-mock-resources","owner":"schireson","description":"Pytest Fixtures that let you actually test against external resource (Postgres, Mongo, Redshift...) dependent code.","archived":false,"fork":false,"pushed_at":"2024-10-01T20:20:48.000Z","size":1120,"stargazers_count":181,"open_issues_count":7,"forks_count":19,"subscribers_count":10,"default_branch":"main","last_synced_at":"2024-10-29T14:21:09.372Z","etag":null,"topics":["docker","hacktoberfest","knownip","library","mongo","mongodb","postgres","pytest","pytest-fixtures","python","redshift","tidepod"],"latest_commit_sha":null,"homepage":"https://pytest-mock-resources.readthedocs.io/en/latest/quickstart.html","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/schireson.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-03T20:51:50.000Z","updated_at":"2024-10-01T06:37:22.000Z","dependencies_parsed_at":"2024-04-03T17:41:16.790Z","dependency_job_id":"afa0f6b9-019c-4b4c-900f-6e79f1337222","html_url":"https://github.com/schireson/pytest-mock-resources","commit_stats":{"total_commits":214,"total_committers":16,"mean_commits":13.375,"dds":0.5607476635514019,"last_synced_commit":"4a92dc2546eeb3ef3b86cbe21a67a7abd8c21984"},"previous_names":[],"tags_count":64,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schireson%2Fpytest-mock-resources","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schireson%2Fpytest-mock-resources/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schireson%2Fpytest-mock-resources/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schireson%2Fpytest-mock-resources/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schireson","download_url":"https://codeload.github.com/schireson/pytest-mock-resources/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615377,"owners_count":20967184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","hacktoberfest","knownip","library","mongo","mongodb","postgres","pytest","pytest-fixtures","python","redshift","tidepod"],"created_at":"2024-09-24T13:13:31.690Z","updated_at":"2025-04-07T08:14:41.581Z","avatar_url":"https://github.com/schireson.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschireson%2Fpytest-mock-resources","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschireson%2Fpytest-mock-resources","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschireson%2Fpytest-mock-resources/lists"}