Skip to content

Commit

Permalink
Added check for no other aggs apart from date_histogram + test
Browse files Browse the repository at this point in the history
  • Loading branch information
grgilad committed Sep 9, 2021
1 parent 2b5fbd6 commit 1b48e1a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ELASTICMOCK_VERSION='1.8.2'
ELASTICMOCK_VERSION='1.8.3'

install:
pip3 install -r requirements.txt
Expand Down
23 changes: 12 additions & 11 deletions elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/fake_elasticsearch/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit 1b48e1a

Please sign in to comment.