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

Commit 46a20ab

Browse files
authored
Merge pull request #386 from cloudant/361-fix-context-manager-no-doc-id
Fixed document context manager when document id does not exist
2 parents 8ef8387 + 0191573 commit 46a20ab

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [IMPROVED] Added support for IAM API key in `cloudant_bluemix` method.
77
- [IMPROVED] Verified library operation on Python 3.6.3.
88
- [IMPROVED] Shortened length of client URLs by removing username and password.
9+
- [FIXED] Case where `Document` context manager would throw instead of creating a new document if no `_id` was provided.
910

1011
# 2.8.1 (2018-02-16)
1112

docs/getting_started.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,16 @@ context manager.
430430
431431
my_database = client.create_database('my_database')
432432
433-
# Performs a fetch upon entry and a save upon exit of this block
434-
with Document(my_database, 'julia30') as doc:
435-
doc['name'] = 'Julia'
436-
doc['age'] = 30
437-
doc['pets'] = ['cat', 'dog', 'frog']
433+
# Upon entry into the document context, fetches the document from the
434+
# remote database, if it exists. Upon exit from the context, saves the
435+
# document to the remote database with changes made within the context
436+
# or creates a new document.
437+
with Document(database, 'julia006') as document:
438+
# If document exists, it's fetched from the remote database
439+
# Changes are made locally
440+
document['name'] = 'Julia'
441+
document['age'] = 6
442+
# The document is saved to the remote database
438443
439444
# Display a Document
440445
print(my_database['julia30'])

src/cloudant/document.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ def __enter__(self):
332332
except HTTPError as error:
333333
if error.response.status_code != 404:
334334
raise
335+
except CloudantDocumentException as error:
336+
if error.status_code != 101:
337+
raise
335338

336339
return self
337340

tests/unit/document_tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,29 @@ def test_document_context_manager(self):
540540
self.assertTrue(doc['_rev'].startswith('2-'))
541541
self.assertEqual(self.db['julia006'], doc)
542542

543+
def test_document_context_manager_no_doc_id(self):
544+
"""
545+
Test that the __enter__ and __exit__ methods perform as expected
546+
with no document id when initiated through a document context manager
547+
"""
548+
with Document(self.db) as doc:
549+
doc['_id'] = 'julia006'
550+
doc['name'] = 'julia'
551+
doc['age'] = 6
552+
self.assertTrue(doc['_rev'].startswith('1-'))
553+
self.assertEqual(self.db['julia006'], doc)
554+
555+
def test_document_context_manager_doc_create(self):
556+
"""
557+
Test that the document context manager will create a doc if it does
558+
not yet exist.
559+
"""
560+
with Document(self.db, 'julia006') as doc:
561+
doc['name'] = 'julia'
562+
doc['age'] = 6
563+
self.assertTrue(doc['_rev'].startswith('1-'))
564+
self.assertEqual(self.db['julia006'], doc)
565+
543566
def test_setting_id(self):
544567
"""
545568
Ensure that proper processing occurs when setting the _id

0 commit comments

Comments
 (0)