From 1b48e1a337a36beaa73f0f79976dd536c13e7935 Mon Sep 17 00:00:00 2001 From: grgilad Date: Thu, 9 Sep 2021 14:28:22 +0300 Subject: [PATCH] Added check for no other aggs apart from date_histogram + test --- Makefile | 2 +- elasticmock/fake_elasticsearch.py | 23 ++++++++------- setup.py | 2 +- tests/fake_elasticsearch/test_search.py | 38 +++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 7d4852c..ce5ec1d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -ELASTICMOCK_VERSION='1.8.2' +ELASTICMOCK_VERSION='1.8.3' install: pip3 install -r requirements.txt diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 9a3a7f1..725b9e7 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -803,18 +803,19 @@ def make_bucket(bucket_key, bucket): "doc_count": len(bucket), } - for metric_key, metric_definition in aggregation["aggs"].items(): - metric_type_str = list(metric_definition)[0] - metric_type = MetricType.get_metric_type(metric_type_str) - attr = metric_definition[metric_type_str]["field"] - data = [doc[attr] for doc in bucket] - - if metric_type == MetricType.CARDINALITY: - value = len(set(data)) - else: - raise NotImplementedError(f"Metric type '{metric_type}' not implemented") + if aggregation.get("aggs"): + for metric_key, metric_definition in aggregation["aggs"].items(): + metric_type_str = list(metric_definition)[0] + metric_type = MetricType.get_metric_type(metric_type_str) + attr = metric_definition[metric_type_str]["field"] + data = [doc[attr] for doc in bucket] + + if metric_type == MetricType.CARDINALITY: + value = len(set(data)) + else: + raise NotImplementedError(f"Metric type '{metric_type}' not implemented") - out[metric_key] = {"value": value} + out[metric_key] = {"value": value} return out agg_sources = aggregation["composite"]["sources"] diff --git a/setup.py b/setup.py index 5612cfb..8d19d9e 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ import setuptools -__version__ = '1.8.2' +__version__ = '1.8.3' # read the contents of your readme file from os import path diff --git a/tests/fake_elasticsearch/test_search.py b/tests/fake_elasticsearch/test_search.py index 8659051..edf36a5 100644 --- a/tests/fake_elasticsearch/test_search.py +++ b/tests/fake_elasticsearch/test_search.py @@ -420,3 +420,41 @@ def test_bucket_aggregation_date_histogram(self): for x, y in zip(expected, actual): self.assertDictEqual(x["key"], y["key"]) self.assertEqual(x["doc_count"], y["doc_count"]) + + def test_bucket_aggregation_date_histogram_without_other_aggs(self): + start_date = datetime.datetime(2021, 12, 1, 15) + data = [ + {"data_x": 1, "data_y": "a", "timestamp": start_date}, + {"data_x": 1, "data_y": "a", "timestamp": start_date}, + {"data_x": 1, "data_y": "a", "timestamp": start_date - datetime.timedelta(hours=1)}, + {"data_x": 1, "data_y": "b", "timestamp": start_date - datetime.timedelta(hours=1)}, + {"data_x": 1, "data_y": "b", "timestamp": start_date - datetime.timedelta(hours=1)}, + ] + for body in data: + self.es.index(index='index_for_search', doc_type=DOC_TYPE, body=body) + + response = self.es.search( + index="index_for_search", + doc_type=DOC_TYPE, + body={ + "query": {"match_all": {}}, + "aggs": { + "stats": { + "composite": { + "sources": [{"histo": {"date_histogram": {"field": "timestamp"}}}], + "size": 10000, + }, + } + }, + }, + ) + + expected = [ + {"key": {"histo": '2021-12-01T14:00:00'}, "doc_count": 3}, + {"key": {"histo": '2021-12-01T15:00:00'}, "doc_count": 2}, + ] + actual = response["aggregations"]["stats"]["buckets"] + print(actual) + for x, y in zip(expected, actual): + self.assertDictEqual(x["key"], y["key"]) + self.assertEqual(x["doc_count"], y["doc_count"])