Skip to content

Commit 2181844

Browse files
Add support for custom aggregations in Search API
1 parent c2397be commit 2181844

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

cloudinary/search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import base64
21
import json
32

43
import cloudinary
@@ -14,7 +13,7 @@ class Search(object):
1413

1514
_KEYS_WITH_UNIQUE_VALUES = {
1615
'sort_by': lambda x: next(iter(x)),
17-
'aggregate': None,
16+
'aggregate': lambda agg: agg["type"] if isinstance(agg, dict) and "type" in agg else agg,
1817
'with_field': None,
1918
'fields': None,
2019
}

test/test_search.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from test.test_api import MOCK_RESPONSE, NEXT_CURSOR
1414
from test.test_config import CLOUD_NAME, API_KEY, API_SECRET
1515

16+
CUSTOM_AGGREGATION = {"type": "bytes",
17+
"ranges": [{"key": "tiny", "to": 500}, {"key": "medium", "from": 501, "to": 1999},
18+
{"key": "big", "from": 2000}]}
19+
1620
TEST_TAG = 'search_{}'.format(TEST_TAG)
1721
UNIQUE_TAG = 'search_{}'.format(UNIQUE_TAG)
1822

@@ -100,6 +104,11 @@ def test_should_add_aggregations_arguments_as_array_as_dict(self):
100104
query = Search().aggregate('format').aggregate('size_category').as_dict()
101105
self.assertEqual(query, {"aggregate": ["format", "size_category"]})
102106

107+
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
108+
def test_should_add_custom_aggregations_arguments_as_array_as_dict(self):
109+
query = Search().aggregate('format').aggregate('size_category').aggregate(CUSTOM_AGGREGATION).as_dict()
110+
self.assertEqual(query, {"aggregate": ["format", "size_category", CUSTOM_AGGREGATION]})
111+
103112
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
104113
def test_should_add_with_field_as_dict(self):
105114

@@ -204,14 +213,16 @@ def test_should_include_context_tags_and_image_metadata(self):
204213
@patch(URLLIB3_REQUEST)
205214
def test_should_not_duplicate_values(self, mocker):
206215
mocker.return_value = MOCK_RESPONSE
207-
216+
override_custom_aggregation = dict(CUSTOM_AGGREGATION, ranges=[])
208217
Search() \
209218
.sort_by('created_at', 'asc') \
210219
.sort_by('public_id', 'asc') \
211220
.sort_by('created_at') \
212221
.aggregate('format') \
213222
.aggregate('format') \
214223
.aggregate(['resource_type', 'type']) \
224+
.aggregate(override_custom_aggregation) \
225+
.aggregate(CUSTOM_AGGREGATION) \
215226
.with_field('context') \
216227
.with_field('context') \
217228
.with_field('tags') \
@@ -228,7 +239,7 @@ def test_should_not_duplicate_values(self, mocker):
228239
{'created_at': 'desc'},
229240
{'public_id': 'asc'},
230241
],
231-
'aggregate': ['format', 'resource_type', 'type'],
242+
'aggregate': ['format', 'resource_type', 'type', CUSTOM_AGGREGATION],
232243
'with_field': ['context', 'tags'],
233244
'fields': ['tags', 'context', 'metadata'],
234245
})

0 commit comments

Comments
 (0)