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)
Admin tools
Openmock ships three interactive admin tools for exploring fake state outside of tests.
Streamlit web UI (requires pip install openmock[web]):
openmock
Tkinter desktop GUI (no extra dependencies — uses Python's built-in tkinter):
openmock gui
FastAPI REST Bridge (requires pip install openmock[rest]):
openmock serve
These tools open a six-tab interface or an HTTP facade covering indices, search sandbox, cluster stats, CAT output, fake security (users/roles), and ingest pipelines. State in these tools is separate from your test processes.
Notes:
- The mocked search method evaluates the query against indexed documents and returns matching results, not all documents. Use
match_allif you want everything. - 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
Run the same pytest suite against a disposable Docker OpenSearch instead of the in-memory fake:
uv run python scripts/opensearch_docker.py test
If you want to manage the container yourself, the backend seam is driven by
OPENMOCK_TEST_BACKEND=real and OPENMOCK_REAL_OPENSEARCH_URL=http://localhost:9200.
The default remains the in-memory mock backend.
The collected tests are split into two marker groups:
parityfor live-backend parity coverage,mock_backendfor tests that are specific to the in-memory fake.
Examples:
uv run python -m pytest tests -m parity
uv run python -m pytest tests -m mock_backend
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openmock-3.2.0.tar.gz.
File metadata
- Download URL: openmock-3.2.0.tar.gz
- Upload date:
- Size: 39.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e08c0590665d89763fd750aaa144e107aca30bd9aba00d54ec6b00d727b8bfd
|
|
| MD5 |
47e10182e8b3246038cbd920d92871c0
|
|
| BLAKE2b-256 |
f685e740ab4e9e5f6796fd0b9395f759a3fa27544a3d2ef88f379ba5b863336e
|
Provenance
The following attestation bundles were made for openmock-3.2.0.tar.gz:
Publisher:
publish_to_pypi.yml on matthewdeanmartin/openmock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openmock-3.2.0.tar.gz -
Subject digest:
6e08c0590665d89763fd750aaa144e107aca30bd9aba00d54ec6b00d727b8bfd - Sigstore transparency entry: 1344650010
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/openmock@6bb6b50d1ed80cbca054cf471af21f7e0fc99ae9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@6bb6b50d1ed80cbca054cf471af21f7e0fc99ae9 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file openmock-3.2.0-py3-none-any.whl.
File metadata
- Download URL: openmock-3.2.0-py3-none-any.whl
- Upload date:
- Size: 45.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8000d9f331732ed88c5876c0a256d4e0134f36f9400646492b3f28f83b5fb8ff
|
|
| MD5 |
2f52031772445277603d6512475d7d40
|
|
| BLAKE2b-256 |
4ab60ee886907820899ea375c1e750c353689ad30bb77155b7f2f343a9d5a778
|
Provenance
The following attestation bundles were made for openmock-3.2.0-py3-none-any.whl:
Publisher:
publish_to_pypi.yml on matthewdeanmartin/openmock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openmock-3.2.0-py3-none-any.whl -
Subject digest:
8000d9f331732ed88c5876c0a256d4e0134f36f9400646492b3f28f83b5fb8ff - Sigstore transparency entry: 1344650161
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/openmock@6bb6b50d1ed80cbca054cf471af21f7e0fc99ae9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@6bb6b50d1ed80cbca054cf471af21f7e0fc99ae9 -
Trigger Event:
workflow_dispatch
-
Statement type: