openmock


Nameopenmock JSON
Version 2.3.6 PyPI version JSON
download
home_pagehttps://github.com/matthewdeanmartin/openmock
SummaryPython OpenSearch Mock for test purposes
upload_time2023-12-12 12:54:48
maintainer
docs_urlNone
authorMarcos Cardoso
requires_python>=3.9,<4
licenseMIT
keywords opensearch mocking testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Openmock

Mock/fake of opensearch library, allows you to mock opensearch-py

Fork of Python Elasticsearch(TM) Mock. Sometimes the developers who work with elasticsearch (TM),
don't really have any input in choice of host and need to get work done.

![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/pypi/openmock) [![Downloads](https://pepy.tech/badge/openmock/month)](https://pepy.tech/project/openmock/month)

## Installation

```shell
pip install openmock
```

## Usage

To use Openmock, decorate your test method with **@openmock** decorator:

```python
from unittest import TestCase

from openmock import openmock


class TestClass(TestCase):

    @openmock
    def test_should_return_something_from_opensearch(self):
        self.assertIsNotNone(some_function_that_uses_opensearch())
```

### Custom Behaviours

You can also force the behaviour of the OpenSearch instance by importing the `openmock.behaviour` module:

```python
from unittest import TestCase

from openmock import behaviour


class TestClass(TestCase):

    ...

    def test_should_return_internal_server_error_when_simulate_server_error_is_true(self):
        behaviour.server_failure.enable()
        ...
        behaviour.server_failure.disable()
```

You can also disable all behaviours by calling `behaviour.disable_all()` (Consider put this in your `def tearDown(self)` method)

#### Available Behaviours

- `server_failure`: Will make all calls to OpenSearch returns the following error message:
  ```python
  {
      'status_code': 500,
      'error': 'Internal Server Error'
  }
  ```

## Code example

Let's say you have a prod code snippet like this one:

```python
import opensearchpy

class FooService:

    def __init__(self):
        self.es = opensearchpy.OpenSearch(hosts=[{'host': 'localhost', 'port': 9200}])

    def create(self, index, body):
        es_object = self.es.index(index, body)
        return es_object.get('_id')

    def read(self, index, id):
        es_object = self.es.get(index, id)
        return es_object.get('_source')

```

Then you should be able to test this class by mocking OpenSearch using the following test class:

```python
from unittest import TestCase
from openmock import openmock
from foo.bar import FooService

class FooServiceTest(TestCase):

    @openmock
    def should_create_and_read_object(self):
        # Variables used to test
        index = 'test-index'
        expected_document = {
            'foo': 'bar'
        }

        # Instantiate service
        service = FooService()

        # Index document on OpenSearch
        id = service.create(index, expected_document)
        self.assertIsNotNone(id)

        # Retrive document from OpenSearch
        document = service.read(index, id)
        self.assertEquals(expected_document, document)

```

## Notes:

- The mocked **search** method returns **all available documents** indexed on the index with the requested document type.
- The mocked **suggest** method returns the exactly suggestions dictionary passed as body serialized in OpenSearch.suggest response. **Attention:** If the term is an *int*, the suggestion will be `python term + 1`. If not, the suggestion will be formatted as `python {0}_suggestion.format(term) `.
  Example:
  - **Suggestion Body**:
  ```python
  suggestion_body = {
      'suggestion-string': {
          'text': 'test_text',
          'term': {
              'field': 'string'
          }
      },
      'suggestion-id': {
          'text': 1234567,
          'term': {
              'field': 'id'
          }
      }
  }
  ```
  - **Suggestion Response**:
  ```python
  {
      'suggestion-string': [
          {
              'text': 'test_text',
              'length': 1,
              'options': [
                  {
                      'text': 'test_text_suggestion',
                      'freq': 1,
                      'score': 1.0
                  }
              ],
              'offset': 0
          }
      ],
      'suggestion-id': [
          {
              'text': 1234567,
              'length': 1,
              'options': [
                  {
                      'text': 1234568,
                      'freq': 1,
                      'score': 1.0
                  }
              ],
              'offset': 0
          }
      ],
  }
  ```

## Testing

Preferred for testing one version of python.

```bash
pytest test
```

Won't catch pytest tests.

```bash
python -m unittest
```

We are trying to support a full matrix of openmock versions and python versions 3.6+. This is slow.

```bash
tox
```

## Changelog

#### 2.1.0:

- Update function (Thanks!)
- tox runs against full matrix
- Range queries (Thanks!)

#### 2.0.0:

- Fork from elasticmock

## License

MIT with normalize_host.py being Apache 2 from Elasticsearch.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/matthewdeanmartin/openmock",
    "name": "openmock",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4",
    "maintainer_email": "",
    "keywords": "opensearch,mocking,testing",
    "author": "Marcos Cardoso",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/f7/b9/190baf66cfb1ad9a02fb2f605aef4cf9915d48c8732bd03c5eba911372c4/openmock-2.3.6.tar.gz",
    "platform": null,
    "description": "# Openmock\n\nMock/fake of opensearch library, allows you to mock opensearch-py\n\nFork of Python Elasticsearch(TM) Mock. Sometimes the developers who work with elasticsearch (TM),\ndon't really have any input in choice of host and need to get work done.\n\n![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/pypi/openmock) [![Downloads](https://pepy.tech/badge/openmock/month)](https://pepy.tech/project/openmock/month)\n\n## Installation\n\n```shell\npip install openmock\n```\n\n## Usage\n\nTo use Openmock, decorate your test method with **@openmock** decorator:\n\n```python\nfrom unittest import TestCase\n\nfrom openmock import openmock\n\n\nclass TestClass(TestCase):\n\n    @openmock\n    def test_should_return_something_from_opensearch(self):\n        self.assertIsNotNone(some_function_that_uses_opensearch())\n```\n\n### Custom Behaviours\n\nYou can also force the behaviour of the OpenSearch instance by importing the `openmock.behaviour` module:\n\n```python\nfrom unittest import TestCase\n\nfrom openmock import behaviour\n\n\nclass TestClass(TestCase):\n\n    ...\n\n    def test_should_return_internal_server_error_when_simulate_server_error_is_true(self):\n        behaviour.server_failure.enable()\n        ...\n        behaviour.server_failure.disable()\n```\n\nYou can also disable all behaviours by calling `behaviour.disable_all()` (Consider put this in your `def tearDown(self)` method)\n\n#### Available Behaviours\n\n- `server_failure`: Will make all calls to OpenSearch returns the following error message:\n  ```python\n  {\n      'status_code': 500,\n      'error': 'Internal Server Error'\n  }\n  ```\n\n## Code example\n\nLet's say you have a prod code snippet like this one:\n\n```python\nimport opensearchpy\n\nclass FooService:\n\n    def __init__(self):\n        self.es = opensearchpy.OpenSearch(hosts=[{'host': 'localhost', 'port': 9200}])\n\n    def create(self, index, body):\n        es_object = self.es.index(index, body)\n        return es_object.get('_id')\n\n    def read(self, index, id):\n        es_object = self.es.get(index, id)\n        return es_object.get('_source')\n\n```\n\nThen you should be able to test this class by mocking OpenSearch using the following test class:\n\n```python\nfrom unittest import TestCase\nfrom openmock import openmock\nfrom foo.bar import FooService\n\nclass FooServiceTest(TestCase):\n\n    @openmock\n    def should_create_and_read_object(self):\n        # Variables used to test\n        index = 'test-index'\n        expected_document = {\n            'foo': 'bar'\n        }\n\n        # Instantiate service\n        service = FooService()\n\n        # Index document on OpenSearch\n        id = service.create(index, expected_document)\n        self.assertIsNotNone(id)\n\n        # Retrive document from OpenSearch\n        document = service.read(index, id)\n        self.assertEquals(expected_document, document)\n\n```\n\n## Notes:\n\n- The mocked **search** method returns **all available documents** indexed on the index with the requested document type.\n- The mocked **suggest** method returns the exactly suggestions dictionary passed as body serialized in OpenSearch.suggest response. **Attention:** If the term is an *int*, the suggestion will be `python term + 1`. If not, the suggestion will be formatted as `python {0}_suggestion.format(term) `.\n  Example:\n  - **Suggestion Body**:\n  ```python\n  suggestion_body = {\n      'suggestion-string': {\n          'text': 'test_text',\n          'term': {\n              'field': 'string'\n          }\n      },\n      'suggestion-id': {\n          'text': 1234567,\n          'term': {\n              'field': 'id'\n          }\n      }\n  }\n  ```\n  - **Suggestion Response**:\n  ```python\n  {\n      'suggestion-string': [\n          {\n              'text': 'test_text',\n              'length': 1,\n              'options': [\n                  {\n                      'text': 'test_text_suggestion',\n                      'freq': 1,\n                      'score': 1.0\n                  }\n              ],\n              'offset': 0\n          }\n      ],\n      'suggestion-id': [\n          {\n              'text': 1234567,\n              'length': 1,\n              'options': [\n                  {\n                      'text': 1234568,\n                      'freq': 1,\n                      'score': 1.0\n                  }\n              ],\n              'offset': 0\n          }\n      ],\n  }\n  ```\n\n## Testing\n\nPreferred for testing one version of python.\n\n```bash\npytest test\n```\n\nWon't catch pytest tests.\n\n```bash\npython -m unittest\n```\n\nWe are trying to support a full matrix of openmock versions and python versions 3.6+. This is slow.\n\n```bash\ntox\n```\n\n## Changelog\n\n#### 2.1.0:\n\n- Update function (Thanks!)\n- tox runs against full matrix\n- Range queries (Thanks!)\n\n#### 2.0.0:\n\n- Fork from elasticmock\n\n## License\n\nMIT with normalize_host.py being Apache 2 from Elasticsearch.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python OpenSearch Mock for test purposes",
    "version": "2.3.6",
    "project_urls": {
        "Bug Tracker": "https://github.com/matthewdeanmartin/openmock/issues",
        "Change Log": "https://github.com/matthewdeanmartin/openmock/blob/main/CHANGES.md",
        "Documentation": "https://github.com/matthewdeanmartin/openmock",
        "Homepage": "https://github.com/matthewdeanmartin/openmock",
        "Repository": "https://github.com/matthewdeanmartin/openmock"
    },
    "split_keywords": [
        "opensearch",
        "mocking",
        "testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a85600c9595846167f4a44446889d6c15d07a0062238201a47691ce3e1e9d7fe",
                "md5": "ec65dc7bccfe2611a3add5127a33d3f0",
                "sha256": "37f1ca31474d907c38215d5d8cb957cc6a19e17eec33c66131316e0d6a7d4a53"
            },
            "downloads": -1,
            "filename": "openmock-2.3.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ec65dc7bccfe2611a3add5127a33d3f0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4",
            "size": 18624,
            "upload_time": "2023-12-12T12:54:47",
            "upload_time_iso_8601": "2023-12-12T12:54:47.142865Z",
            "url": "https://files.pythonhosted.org/packages/a8/56/00c9595846167f4a44446889d6c15d07a0062238201a47691ce3e1e9d7fe/openmock-2.3.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7b9190baf66cfb1ad9a02fb2f605aef4cf9915d48c8732bd03c5eba911372c4",
                "md5": "ad9be40f7bdfb609c7260c784cbe1aa8",
                "sha256": "954bf7cdd0973227fbeee22ded95beffbe9ac32329572df02210e37faf38476f"
            },
            "downloads": -1,
            "filename": "openmock-2.3.6.tar.gz",
            "has_sig": false,
            "md5_digest": "ad9be40f7bdfb609c7260c784cbe1aa8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4",
            "size": 15561,
            "upload_time": "2023-12-12T12:54:48",
            "upload_time_iso_8601": "2023-12-12T12:54:48.910668Z",
            "url": "https://files.pythonhosted.org/packages/f7/b9/190baf66cfb1ad9a02fb2f605aef4cf9915d48c8732bd03c5eba911372c4/openmock-2.3.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-12 12:54:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "matthewdeanmartin",
    "github_project": "openmock",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "openmock"
}
        
Elapsed time: 0.15496s