Package: openmock
OpenmockMock/fake of opensearch library, allows you to mock opensearch-pyFork 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.InstallationpipinstallopenmockUsageTo use Openmock, decorate your test method with@openmockdecorator:fromunittestimportTestCasefromopenmockimportopenmockclassTestClass(TestCase):@openmockdeftest_should_return_something_from_opensearch(self):self.assertIsNotNone(some_function_that_uses_opensearch())Custom BehavioursYou can also force the behaviour of the OpenSearch instance by importing theopenmock.behaviourmodule:fromunittestimportTestCasefromopenmockimportbehaviourclassTestClass(TestCase):...deftest_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 callingbehaviour.disable_all()(Consider put this in yourdef tearDown(self)method)Available Behavioursserver_failure: Will make all calls to OpenSearch returns the following error message:{'status_code':500,'error':'Internal Server Error'}Code exampleLet's say you have a prod code snippet like this one:importopensearchpyclassFooService:def__init__(self):self.es=opensearchpy.OpenSearch(hosts=[{'host':'localhost','port':9200}])defcreate(self,index,body):es_object=self.es.index(index,body)returnes_object.get('_id')defread(self,index,id):es_object=self.es.get(index,id)returnes_object.get('_source')Then you should be able to test this class by mocking OpenSearch using the following test class:fromunittestimportTestCasefromopenmockimportopenmockfromfoo.barimportFooServiceclassFooServiceTest(TestCase):@openmockdefshould_create_and_read_object(self):# Variables used to testindex='test-index'expected_document={'foo':'bar'}# Instantiate serviceservice=FooService()# Index document on OpenSearchid=service.create(index,expected_document)self.assertIsNotNone(id)# Retrive document from OpenSearchdocument=service.read(index,id)self.assertEquals(expected_document,document)Notes:The mockedsearchmethod returnsall available documentsindexed on the index with the requested document type.The mockedsuggestmethod returns the exactly suggestions dictionary passed as body serialized in OpenSearch.suggest response.Attention:If the term is anint, the suggestion will bepython 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}],}TestingPreferred for testing one version of python.pytesttestWon't catch pytest tests.python-munittestWe are trying to support a full matrix of openmock versions and python versions 3.6+. This is slow.toxChangelog2.1.0:Update function (Thanks!)tox runs against full matrixRange queries (Thanks!)2.0.0:Fork from elasticmockLicenseMIT with normalize_host.py being Apache 2 from Elasticsearch.