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

Commit

Permalink
Merge pull request #386 from cloudant/361-fix-context-manager-no-doc-id
Browse files Browse the repository at this point in the history
Fixed document context manager when document id does not exist
  • Loading branch information
emlaver authored Jun 5, 2018
2 parents 8ef8387 + 0191573 commit 46a20ab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [IMPROVED] Added support for IAM API key in `cloudant_bluemix` method.
- [IMPROVED] Verified library operation on Python 3.6.3.
- [IMPROVED] Shortened length of client URLs by removing username and password.
- [FIXED] Case where `Document` context manager would throw instead of creating a new document if no `_id` was provided.

# 2.8.1 (2018-02-16)

Expand Down
15 changes: 10 additions & 5 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,16 @@ context manager.
my_database = client.create_database('my_database')
# Performs a fetch upon entry and a save upon exit of this block
with Document(my_database, 'julia30') as doc:
doc['name'] = 'Julia'
doc['age'] = 30
doc['pets'] = ['cat', 'dog', 'frog']
# Upon entry into the document context, fetches the document from the
# remote database, if it exists. Upon exit from the context, saves the
# document to the remote database with changes made within the context
# or creates a new document.
with Document(database, 'julia006') as document:
# If document exists, it's fetched from the remote database
# Changes are made locally
document['name'] = 'Julia'
document['age'] = 6
# The document is saved to the remote database
# Display a Document
print(my_database['julia30'])
Expand Down
3 changes: 3 additions & 0 deletions src/cloudant/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ def __enter__(self):
except HTTPError as error:
if error.response.status_code != 404:
raise
except CloudantDocumentException as error:
if error.status_code != 101:
raise

return self

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/document_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,29 @@ def test_document_context_manager(self):
self.assertTrue(doc['_rev'].startswith('2-'))
self.assertEqual(self.db['julia006'], doc)

def test_document_context_manager_no_doc_id(self):
"""
Test that the __enter__ and __exit__ methods perform as expected
with no document id when initiated through a document context manager
"""
with Document(self.db) as doc:
doc['_id'] = 'julia006'
doc['name'] = 'julia'
doc['age'] = 6
self.assertTrue(doc['_rev'].startswith('1-'))
self.assertEqual(self.db['julia006'], doc)

def test_document_context_manager_doc_create(self):
"""
Test that the document context manager will create a doc if it does
not yet exist.
"""
with Document(self.db, 'julia006') as doc:
doc['name'] = 'julia'
doc['age'] = 6
self.assertTrue(doc['_rev'].startswith('1-'))
self.assertEqual(self.db['julia006'], doc)

def test_setting_id(self):
"""
Ensure that proper processing occurs when setting the _id
Expand Down

0 comments on commit 46a20ab

Please sign in to comment.