Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit a105ae7

Browse files
emlaverricellis
authored andcommitted
Fix paging logic to check 'id' field exists for grouped view queries
Added CHANGES entry Dict members aren't checked by `hasattr`. Use `get` instead and keep the value so we don't have to fetch it twice.
1 parent f7d0cb0 commit a105ae7

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [REMOVED] Removed Python 2 compatibility from the supported environments.
33
- [FIXED] Fixed the documentation for `bookmarks`.
44
- [FIXED] Also exit `follow_replication` for `failed` state.
5+
- [FIXED] Fixed result paging for grouped view queries.
56

67
# 2.14.0 (2020-08-17)
78

src/cloudant/result.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,10 @@ def _iterator(self, response):
397397

398398
# if we are in a view, keys could be duplicate so we
399399
# need to start from the right docid
400-
if last['id']:
400+
last_doc_id = last.get('id')
401+
if last_doc_id is not None:
401402
response = self._call(startkey=last['key'],
402-
startkey_docid=last['id'])
403+
startkey_docid=last_doc_id)
403404
# reduce result keys are unique by definition
404405
else:
405406
response = self._call(startkey=last['key'])

tests/unit/database_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,34 @@ def test_retrieve_view_results(self):
408408
self.assertIsInstance(rslt, Result)
409409
self.assertEqual(rslt[:1], rslt['julia099'])
410410

411+
def test_retrieve_grouped_view_result_with_page_size(self):
412+
"""
413+
Test retrieving Result wrapped output from a design document grouped view
414+
that uses a custom page size
415+
416+
The view used here along with group=True will generate rows of
417+
data where each key will be grouped into groups of 2. Such as:
418+
{'key': 0, 'value': 2},
419+
{'key': 1, 'value': 2},
420+
...
421+
"""
422+
map_func = 'function(doc) {\n emit(Math.floor(doc.age / 2), 1); \n}'
423+
data = {'_id': '_design/ddoc01','views': {'view01': {"map": map_func, "reduce": "_count"}}}
424+
self.db.create_document(data)
425+
self.populate_db_with_documents(5)
426+
427+
rslt = self.db.get_view_result(
428+
'_design/ddoc01',
429+
'view01',
430+
group=True,
431+
page_size=1)
432+
self.assertIsInstance(rslt, Result)
433+
i = 0
434+
for row in rslt:
435+
self.assertIsNotNone(row)
436+
self.assertEqual(row['key'], i)
437+
i += 1
438+
411439
def test_retrieve_raw_view_results(self):
412440
"""
413441
Test retrieving raw output from a design document view

0 commit comments

Comments
 (0)