This repository was archived by the owner on Aug 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +37
-5
lines changed
Expand file tree Collapse file tree 4 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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' ])
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments