Python Elasticsearch Mock for test purposes
pip install ElasticMock
To use ElasticMock, decorate your test method with @elasticmock decorator:
from unittest import TestCase
from elasticmock import elasticmock
class TestClass(TestCase):
@elasticmock
def test_should_return_something_from_elasticsearch(self):
self.assertIsNotNone(some_function_that_uses_elasticsearch())
- 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 Elasticsearch.suggest response. Atention: 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 } ], }
python setup.py test
- Fix installation issue (Thanks @tdhopper)
- Fix 1.3.4 release (Thanks @infinite-Joy)
- Added aggregations to response if requested (Thanks @snakeye)
- Implementing new methods for scrolling (Thanks @tcatrain)
- Search: doc_type can be a list (Thanks @garncarz)
- Exclude tests package (Thanks @jmlw)
- [Make the FakeElasticsearch init signature match the one from Elasticsearc]((#10) (Thanks @xrmx)
- Improve search and count (Thanks @frivoire)
- elasticmock: Python 3 fixes (Thanks @barseghyanartur)
- test: Add information on testing (Thanks @barseghyanartur)
- README.md: Fixed typo (Thanks @bowlofstew)
- elasticmock: Allow the same arguments to the mock that elasticsearch.Elasticsearch allows (Thanks @mattbreeden)
- FakeElasticSearch: Mocked count method (Thanks @TheoResources)
- FakeElasticSearch: Mocked suggest method
- elasticmock: Changing the cleanup older FakeElasticSearch's instances order
- FakeElasticSearch.index: Changing the method signature to correctly overrides the Elasticsearch.index method
- FakeElasticSearch: Mocked delete method
- setup.py: Fixed GitHub link
- elasticmock: Created @elasticmock decorator
- FakeElasticSearch: Mocked exists, get, get_source, index, info, search and ping method