Skip to main content

Python OpenSearch Mock for test purposes

Project description

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 Downloads

Installation

pip install openmock

Usage

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

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:

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:
    {
        'status_code': 500,
        'error': 'Internal Server Error'
    }
    

Code example

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

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:

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)

        # Retrieve 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:
    suggestion_body = {
        'suggestion-string': {
            'text': 'test_text',
            'term': {
                'field': 'string'
            }
        },
        'suggestion-id': {
            'text': 1234567,
            'term': {
                'field': 'id'
            }
        }
    }
    
    • Suggestion Response:
    {
        '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.

pytest test

Won't catch pytest tests.

python -m unittest

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

tox

Changelog

See CHANGELOG.md

License

MIT with normalize_host.py being Apache 2 from Elasticsearch.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openmock-3.1.5.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openmock-3.1.5-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file openmock-3.1.5.tar.gz.

File metadata

  • Download URL: openmock-3.1.5.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for openmock-3.1.5.tar.gz
Algorithm Hash digest
SHA256 7d78c520a64b381d366a275fc7b44c8e48a34c2659f70b473df05333c0b6a59f
MD5 82ccaff4eeae68085b41fd26721b221f
BLAKE2b-256 0e26dfcfdd029cfa443a019e54539977813ac8f5728aa40a48b1edcd3bf4b570

See more details on using hashes here.

Provenance

The following attestation bundles were made for openmock-3.1.5.tar.gz:

Publisher: publish_to_pypi.yml on matthewdeanmartin/openmock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openmock-3.1.5-py3-none-any.whl.

File metadata

  • Download URL: openmock-3.1.5-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for openmock-3.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6f31ff614256435cb7af37f055085be43f287521078788d71f58e80f8434e704
MD5 b120d7940d1a2272aed949ad4dd569da
BLAKE2b-256 d007e655962a98b0a1a106d363af8f0e38b92a39d115a6340b7e4a9d7fa5511c

See more details on using hashes here.

Provenance

The following attestation bundles were made for openmock-3.1.5-py3-none-any.whl:

Publisher: publish_to_pypi.yml on matthewdeanmartin/openmock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page