cloudant / python-cloudant

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

`some_id in some_db` does not work #318

Closed vvasuki closed 7 years ago

vvasuki commented 7 years ago

Consider the following snippet (where id=u'37c1d704a01a462c8a77d9d4e53d4b11' and doc is retrieved from the database):

    logging.debug(id in self.db)
    logging.debug(doc)

It yields:

DEBUG: 2017-08-08 22:29:03,344 {couchdb_live_test.py:26}: False 
DEBUG: 2017-08-08 22:29:03,344 {couchdb_live_test.py:27}: {u'base_data': u'image', u'portion_class': u'book', u'path': u'english', u'_id': u'37c1d704a01a462c8a77d9d4e53d4b11', u'jsonClass': u'BookPortion'} 

So, database, which sometimes acts like a dict, yields a misleading result when one one does some_id in some_db. This suggests an improvement in the interface, or a prominent warning in the docs.

smithsz commented 7 years ago

Agreed. I've found the caching implementation to be somewhat confusing at times. It's easy to be misled into thinking your local object is always a true representation of what's on the server.

You're better off using the doc.exists() method than relying on __contains__ as this presently only checks against document ids that have been cached.

This is closely related to #116. The proposed idea is that you'd set a remote parameter when constructing any client or database objects. If remote=False then the local cache is used and visa versa.

Lets use #116 to track this work. I'll add some further notes there and close this one out.

Thanks for your feedback. It's very much appreciated.