Package: hearsay-elasticmock
ElasticMockPython Elasticsearch Mock for test purposesInstallationpipinstallElasticMockUsageTo use ElasticMock, decorate your test method with@elasticmockdecorator:fromunittestimportTestCasefromelasticmockimportelasticmockclassTestClass(TestCase):@elasticmockdeftest_should_return_something_from_elasticsearch(self):self.assertIsNotNone(some_function_that_uses_elasticsearch())Custom BehavioursYou can also force the behaviour of the ElasticSearch instance by importing theelasticmock.behaviourmodule:fromunittestimportTestCasefromelasticmockimportbehaviourclassTestClass(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 ElasticSearch 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:importelasticsearchclassFooService:def__init__(self):self.es=elasticsearch.Elasticsearch(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')Than you should be able to test this class by mocking ElasticSearch using the following test class:fromunittestimportTestCasefromelasticmockimportelasticmockfromfoo.barimportFooServiceclassFooServiceTest(TestCase):@elasticmockdefshould_create_and_read_object(self):# Variables used to testindex='test-index'expected_document={'foo':'bar'}# Instantiate serviceservice=FooService()# Index document on ElasticSearchid=service.create(index,expected_document)self.assertIsNotNone(id)# Retrive dpcument from ElasticSearchdocument=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 Elasticsearch.suggest response.Atention: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}],}Testingpythonsetup.pytestChangelog1.5.1:Fix tests for es > 7(Thanks@chesstrian)1.5.0:FakeElasticSearch: MockedindicespropertyFakeIndicesClient: Mockedcreate,exists,refreshanddeletemethodsFakeElasticSearch: MockedclusterpropertyFakeClusterClient: Mockedhealthmethod1.4.0Fix es.index regression issueAdd 'Force Server Failure' feature as requestedReformat code to be compliant with PEP8Add support to Python 3.81.3.7Adding fix for updating existing doc using index(Thanks@adityaghosh)Added bulk method(Thanks@charl-van-niekerk)Add default value to doc_type in index method as it is by default set to '_doc'(Thanks@mohantyashish109)Add support for Python 3.7(Thanks@asherf)1.3.6Fix installation issue(Thanks@tdhopper)1.3.5Fix 1.3.4 release(Thanks@infinite-Joy)1.3.4Added aggregations to response if requested(Thanks@snakeye)Implementing new methods for scrolling(Thanks@tcatrain)1.3.3Search: doc_type can be a list(Thanks@garncarz)Exclude tests package(Thanks@jmlw)Make the FakeElasticsearchinitsignature match the one from Elasticsearch(Thanks@xrmx)Improve search and count(Thanks@frivoire)1.3.2elasticmock: Python 3 fixes (Thanks@barseghyanartur)test: Add information on testing (Thanks@barseghyanartur)README.md: Fixed typo (Thanks@bowlofstew)1.3.1elasticmock: Allow the same arguments to the mock that elasticsearch.Elasticsearch allows (Thanks@mattbreeden)1.3.0:FakeElasticSearch: Mockedcountmethod (Thanks@TheoResources)1.2.0:FakeElasticSearch: Mockedsuggestmethod1.1.1:elasticmock: Changing the cleanup older FakeElasticSearch's instances orderFakeElasticSearch.index: Changing the method signature to correctly overrides the Elasticsearch.index method1.1.0:FakeElasticSearch: Mockeddeletemethod1.0.1:setup.py: Fixed GitHub link1.0.0:elasticmock: Created@elasticmockdecoratorFakeElasticSearch: Mockedexists,get,get_source,index,info,searchandpingmethod