cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

KeyError: 'id' on iterating over grouped view #456

Closed beadsland closed 3 years ago

beadsland commented 4 years ago

Bug Description

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

for item in db.get_view_result(ddoc_id, view_name, group=True): print item

2. What you expected to happen

Iterate over all grouped reductions.

3. What actually happened

Iterate over grouped reductions, followed by:

File "/usr/local/lib/python2.7/dist-packages/cloudant/result.py", line 400, in _iterator if last['id']: KeyError: 'id'

Environment details

4. Simple fix

Change the offending line to:

if 'id' in last:

bessbd commented 4 years ago

Hi @beadsland ,

Thank you for this bug report. Can you please provide a more complete steps to reproduce? I'm asking, because the following works for me:

from cloudant.client import CouchDB
client = CouchDB("admin", "password", url='http://127.0.0.1:5984', connect=True)

session = client.session()
db = client['testing']

for item in db.get_view_result("newDD", "new-view", group=True):
    print(item)

client.disconnect()

where

curl http://admin:password@127.0.0.1:5984/testing/_all_docs?include_docs=true |jq
{
  "total_rows": 2,
  "offset": 0,
  "rows": [
    {
      "id": "_design/newDD",
      "key": "_design/newDD",
      "value": {
        "rev": "2-0611118a25d6d8435680e48fdefe1f37"
      },
      "doc": {
        "_id": "_design/newDD",
        "_rev": "2-0611118a25d6d8435680e48fdefe1f37",
        "views": {
          "new-view": {
            "map": "function (doc) {\n  emit(doc._id, 1);\n}",
            "reduce": "_count"
          }
        },
        "language": "javascript"
      }
    },
    {
      "id": "f4e47445a7324cb60c32f5d41e00077a",
      "key": "f4e47445a7324cb60c32f5d41e00077a",
      "value": {
        "rev": "1-4c6114c65e295552ab1019e2b046b10e"
      },
      "doc": {
        "_id": "f4e47445a7324cb60c32f5d41e00077a",
        "_rev": "1-4c6114c65e295552ab1019e2b046b10e",
        "foo": "bar"
      }
    }
  ]
}
emlaver commented 3 years ago

This issue can be reproduced when the amount of documents is greater than the page size. Example using the code block above:

...
for item in db.get_view_result("views101", "diet_count", group=True, page_size=1):
    print(item)

I'll open a PR to check that the id exists on line 400 in cloudant/result.py.