[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ Added transaction rollback when one or more widgets errors occur in the edit view. Includes a functional test.

Garrett Smith garrett at mojave-corp.com
Wed Jul 21 09:41:04 EDT 2004


Log message for revision 26649:
  Added transaction rollback when one or more widgets errors occur in the edit view. Includes a functional test.


Changed:
  U   Zope3/trunk/src/zope/app/form/browser/editview.py
  A   Zope3/trunk/src/zope/app/form/browser/ftests/test_editview.py


-=-
Modified: Zope3/trunk/src/zope/app/form/browser/editview.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/editview.py	2004-07-21 13:06:32 UTC (rev 26648)
+++ Zope3/trunk/src/zope/app/form/browser/editview.py	2004-07-21 13:41:04 UTC (rev 26649)
@@ -16,6 +16,7 @@
 $Id$
 """
 from datetime import datetime
+from transaction import get_transaction
 
 from zope.schema import getFieldNamesInOrder
 from zope.publisher.interfaces.browser import IBrowserRequest
@@ -106,6 +107,7 @@
             except WidgetsError, errors:
                 self.errors = errors
                 status = _("An error occured.")
+                get_transaction().abort()
             else:
                 setUpEditWidgets(self, self.schema, source=self.adapted,
                                  ignoreStickyValues=True, 

Added: Zope3/trunk/src/zope/app/form/browser/ftests/test_editview.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/ftests/test_editview.py	2004-07-21 13:06:32 UTC (rev 26648)
+++ Zope3/trunk/src/zope/app/form/browser/ftests/test_editview.py	2004-07-21 13:41:04 UTC (rev 26649)
@@ -0,0 +1,90 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Editview tests
+
+$Id: test_checkboxwidget.py 26267 2004-07-08 23:06:19Z garrett $
+"""
+import unittest
+from persistent import Persistent
+from transaction import get_transaction
+
+from zope.interface import Interface
+from zope.interface import implements
+
+from zope.schema import TextLine
+
+from support import *
+from zope.app.traversing.api import traverse
+
+from zope.app.tests.functional import BrowserTestCase
+
+
+class IFoo(Interface):
+
+    optional_text = TextLine(required=False)
+    required_text = TextLine(required=True)
+
+
+registerEditForm(IFoo)
+
+
+class Foo(Persistent):
+
+    implements(IFoo)
+
+
+defineSecurity(Foo, IFoo)
+
+
+class Test(BrowserTestCase):
+
+
+    def test_rollback_on_error(self):
+        """Tests rollback when a widget error occurs.
+        
+        When one or more errors are generated by input widgets, the current
+        transaction should be rolledback to ensure object integrity.
+        """
+        self.getRootFolder()['foo'] = Foo()
+        self.getRootFolder()['foo'].required_text = u'initial required'
+        self.getRootFolder()['foo'].optional_text = u'initial optional'
+        get_transaction().commit()
+
+        # submit form with legal value for optional_text and invalid for 
+        # required_text
+        response = self.publish('/foo/edit.html', form={
+            'field.optional_text': u'',
+            'field.required_text': u'',
+            'UPDATE_SUBMIT': ''})
+        self.assertEqual(response.getStatus(), 200)
+
+        # confirm that one errors exists
+        self.assert_(patternExists(
+            'There are <strong>1</strong> input errors.', response.getBody()))
+
+        # confirm that foo was not modified
+        foo = traverse(self.getRootFolder(), 'foo')
+        self.assertEquals(foo.required_text, u'initial required')
+        self.assertEquals(foo.optional_text, u'initial optional')
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(Test))
+    return suite
+
+if __name__=='__main__':
+    unittest.main(defaultTest='test_suite')
+
+



More information about the Zope3-Checkins mailing list