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.
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 aspython {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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openmock-3.0.1.tar.gz
.
File metadata
- Download URL: openmock-3.0.1.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 727a4d47600086c8c9024905e0e4bf1adfedb7bb5b638e67d92ff20e6cf737cc |
|
MD5 | 4185823b3148a8537f083419ef42996b |
|
BLAKE2b-256 | f796e647cac3d235c31ddde2aa38236febb18adb0166e3942920cb9d5a187794 |
File details
Details for the file openmock-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: openmock-3.0.1-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ad00587030dc0c6b5d8e7d8a9358bea4dd341a5a2164811d648b6a27064bef3 |
|
MD5 | 7683a5ef23d2dec8d20cd482c568350a |
|
BLAKE2b-256 | cae397de5031d762aab36be6a286260bee90de2b2fd4a1d6707c4c865ecc0a04 |