@@ -622,6 +622,47 @@ def test_document_context_manager_no_doc_id(self):
622622 self .assertTrue (doc ['_rev' ].startswith ('1-' ))
623623 self .assertEqual (self .db ['julia006' ], doc )
624624
625+ def test_document_context_manager_creation_failure_on_error (self ):
626+ """
627+ Test that the document context manager skips document creation if there
628+ is an error.
629+ """
630+ with self .assertRaises (ZeroDivisionError ), Document (self .db , 'julia006' ) as doc :
631+ doc ['name' ] = 'julia'
632+ doc ['age' ] = 6
633+ raise ZeroDivisionError ()
634+
635+ doc = Document (self .db , 'julia006' )
636+ try :
637+ doc .fetch ()
638+ except requests .HTTPError as err :
639+ self .assertEqual (err .response .status_code , 404 )
640+ else :
641+ self .fail ('Above statement should raise a HTTPError.' )
642+
643+ def test_document_context_manager_update_failure_on_error (self ):
644+ """
645+ Test that the document context manager skips document update if there
646+ is an error.
647+ """
648+ # Create the document.
649+ doc = Document (self .db , 'julia006' )
650+ doc ['name' ] = 'julia'
651+ doc ['age' ] = 6
652+ doc .save ()
653+
654+ # Make a document update and then raise an error.
655+ with self .assertRaises (ZeroDivisionError ), Document (self .db , 'julia006' ) as doc :
656+ doc ['age' ] = 7
657+ raise ZeroDivisionError ()
658+
659+ # Assert the change persists locally.
660+ self .assertEqual (doc ['age' ], 7 )
661+
662+ # Assert the document has not been saved to remote server.
663+ self .assertTrue (doc ['_rev' ].startswith ('1-' ))
664+ self .assertEqual (self .db ['julia006' ]['age' ], 6 )
665+
625666 def test_document_context_manager_doc_create (self ):
626667 """
627668 Test that the document context manager will create a doc if it does
0 commit comments