openmock - OpenSearch Mock for Python Testing
raw JSON → 3.2.0 verified Fri May 01 auth: no python
A Python library for mocking OpenSearch (Elasticsearch) operations in unit tests. Provides a lightweight in-memory mock that simulates OpenSearch behaviors without needing a real cluster. Current version: 3.2.0, requires Python >=3.10. Release cadence irregular.
pip install openmock Common errors
error AttributeError: module 'openmock' has no attribute 'OpenMock' ↓
cause Using an older version of openmock (<3.0) where the import path was different, or a typo.
fix
Upgrade to latest version: pip install openmock --upgrade. Then use: from openmock import OpenMock
error opensearchpy.exceptions.ConnectionError: ConnectionError(('Connection refused', ...)) caused by: ConnectionError(('Connection refused', ...)) ↓
cause OpenMock not activated. The OpenSearch client is trying to connect to a real cluster instead of the mock.
fix
Wrap your code with OpenMock() context manager or decorator.
error AssertionError: Expected mocked call not found ↓
cause Using openmock with opensearchpy's mock assertions incorrectly, or the mock is not set up for that specific call.
fix
Ensure you are using the correct API and that the call happens within the OpenMock context. Verify the method and parameters match exactly.
Warnings
breaking Version 3.x dropped support for Python <3.10 and changed the import from 'openmock' (previously 'openmock.OpenMock' was sometimes imported incorrectly). Ensure your tests run on Python >=3.10. ↓
fix Upgrade Python to >=3.10 and use 'from openmock import OpenMock'.
gotcha OpenMock does not persist state between different with-blocks or across separate test cases. Each context manager starts with a fresh mock cluster. ↓
fix Place all related OpenSearch operations within a single with block, or use the decorator form '@OpenMock()' for a whole test function.
gotcha The mock only supports a subset of OpenSearch APIs. Advanced features like snapshot/restore, scroll, or compatibility with specific OpenSearch plugins may not be available. ↓
fix Check the openmock documentation for the list of supported APIs. For unsupported operations, consider mocking manually or using a real test cluster.
Imports
- OpenMock wrong
from openmock import MockOpenSearchcorrectfrom openmock import OpenMock
Quickstart
from openmock import OpenMock
from opensearchpy import OpenSearch
with OpenMock():
client = OpenSearch()
# index a document
client.index(index='my-index', body={'title': 'Test'})
# search
result = client.search(index='my-index', body={'query': {'match_all': {}}})
print(result['hits']['hits'][0]['_source']['title']) # Output: Test