From 23b9dfe3514ac77ac3458109355e2b64f4a05a72 Mon Sep 17 00:00:00 2001 From: Andrey Ovcharov Date: Tue, 7 Aug 2018 17:21:20 +0200 Subject: [PATCH 1/4] Added aggregations to response if requested --- elasticmock/fake_elasticsearch.py | 15 ++++++++++++++- setup.py | 6 +++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index dd1e5aa..909f072 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -9,7 +9,6 @@ from elasticmock.utilities import get_random_id - PY3 = sys.version_info[0] == 3 if PY3: unicode = str @@ -182,6 +181,20 @@ def search(self, index=None, doc_type=None, body=None, params=None): hits.append(match) result['hits']['hits'] = hits + # build aggregations + if 'aggs' in body: + aggregations = {} + + for aggregation, definition in body['aggs'].items(): + aggregations[aggregation] = { + "doc_count_error_upper_bound": 0, + "sum_other_doc_count": 0, + "buckets": [] + } + + if aggregations: + result['aggregations'] = aggregations + return result @query_params('consistency', 'parent', 'refresh', 'replication', 'routing', diff --git a/setup.py b/setup.py index 9521b1f..ceb4b3e 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='ElasticMock', version=__version__, - url='https://github.com/vrcmarcos/elasticmock', + url='https://github.com/snakeye/elasticmock', license='MIT', author='Marcos Cardoso', author_email='vrcmarcos@gmail.com', @@ -17,8 +17,8 @@ include_package_data=True, platforms='any', install_requires=[ - 'elasticsearch<=1.9.0', - 'mock<=1.0.1' + 'elasticsearch', + 'mock' ], classifiers=[ 'Environment :: Web Environment', From 3bcbc4ab3b99670f12f1149b4ab0bc7cdd47f6a6 Mon Sep 17 00:00:00 2001 From: Artem Skoretskiy Date: Thu, 16 Aug 2018 10:22:48 +0400 Subject: [PATCH 2/4] Fixed doc_type check doc_type to `search` method is list, while `document['_type']` is a string. --- elasticmock/fake_elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 909f072..e30c94d 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -156,7 +156,7 @@ def search(self, index=None, doc_type=None, body=None, params=None): matches = [] for searchable_index in searchable_indexes: for document in self.__documents_dict[searchable_index]: - if doc_type is not None and document.get('_type') != doc_type: + if doc_type is not None and document.get('_type') not in doc_type: # value in a list of allowed doc_types continue matches.append(document) From 3cd13b2803f0619a3e40a660b58354f2249de624 Mon Sep 17 00:00:00 2001 From: Andrey Ovcharov Date: Sun, 7 Oct 2018 09:34:45 +0200 Subject: [PATCH 3/4] Added check for body is not None --- elasticmock/fake_elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 5118979..0c6c413 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -185,7 +185,7 @@ def search(self, index=None, doc_type=None, body=None, params=None): result['hits']['hits'] = hits # build aggregations - if 'aggs' in body: + if body is not None and 'aggs' in body: aggregations = {} for aggregation, definition in body['aggs'].items(): From 5c45c1354186d49e14a5c97257fb6512ff66eadc Mon Sep 17 00:00:00 2001 From: Andrey Ovcharov Date: Sun, 7 Oct 2018 09:48:53 +0200 Subject: [PATCH 4/4] Added test for aggregations --- tests/test_elasticmock.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_elasticmock.py b/tests/test_elasticmock.py index c668433..ff1eadc 100644 --- a/tests/test_elasticmock.py +++ b/tests/test_elasticmock.py @@ -242,6 +242,13 @@ def test_doc_type_can_be_list(self): result = self.es.search(doc_type=doc_types[:2]) self.assertEqual(count_per_doc_type * 2, result.get('hits').get('total')) + def test_usage_of_aggregations(self): + self.es.index(index='index', doc_type='document', body={'genre': 'rock'}) + + body = {"aggs": {"genres": {"terms": {"field": "genre"}}}} + result = self.es.search(index='index', body=body) + + self.assertTrue('aggregations' in result) if __name__ == '__main__': unittest.main()