[Zope3-checkins] CVS: Zope3/src/zope/app/form/tests - test_utility.py:1.11

Jim Fulton jim@zope.com
Mon, 14 Apr 2003 04:27:17 -0400


Update of /cvs-repository/Zope3/src/zope/app/form/tests
In directory cvs.zope.org:/tmp/cvs-serv5417/form/tests

Modified Files:
	test_utility.py 
Log Message:
Updated the form-genmeration software to support accessors. Mainly
this involved changing the software to use field ``get``, ``query``,
and ``set`` methods rather than attribute access.  Also modified the
tests to test accessor usage.

Fixed adapter support. Now edit views store the adapted context
separately from the context. Also, edit and add views use getApter
rather than queryAdapter to get adapters. The previous uses of
queryAdapter were hiding some logic errors.  Added unit tests!
In the course of adding unit tests, I discovered an issue relating to
event generation. Normally, we generate an ObjectModification event
when we edit something, however, it's not so clear what we should do
in the presense of an adapter. I finally decided that we should not
generate an event if the object was adapted. In that case, the adapter
should generate any necessary events.


=== Zope3/src/zope/app/form/tests/test_utility.py 1.10 => 1.11 ===
--- Zope3/src/zope/app/form/tests/test_utility.py:1.10	Fri Mar  7 16:27:33 2003
+++ Zope3/src/zope/app/form/tests/test_utility.py	Mon Apr 14 04:27:16 2003
@@ -24,7 +24,7 @@
 from zope.publisher.browser import TestRequest
 from zope.publisher.interfaces.browser import IBrowserPresentation
 from zope.interface import Interface
-from zope.schema import Text
+from zope.schema import Text, accessors
 from zope.app.browser.form.widget import TextWidget
 from zope.component.view import provideView, setDefaultViewName
 from zope.schema.interfaces import IText
@@ -36,6 +36,7 @@
 from zope.component.interfaces import IViewFactory
 
 
+
 class I(Interface):
     title = Text(title=u"Title", required = False)
     description = Text(title=u"Description",
@@ -57,6 +58,22 @@
 class C2:
     __implements__ = I2
 
+
+class Ia(Interface):
+    getTitle, setTitle = accessors(Text(title=u"Title", required = False))
+    getDescription, setDescription = accessors(Text(
+        title=u"Description",
+        default = u'No description', required = False)
+                                               )
+
+class Ca:
+    __implements__ = Ia
+
+    def getTitle(self): return self._t
+    def setTitle(self, v): self._t = v
+    def getDescription(self): return self._d
+    def setDescription(self, v): self._d = v
+
 class ViewWithCustomTitleWidgetFactory(BrowserView):
 
     def title(self, context, request):
@@ -273,6 +290,16 @@
         self.assertEqual(view.title(), u'title: ft')
         self.failIf(hasattr(view, 'description'))
 
+    def test_setupEditWidgets_and_accessors(self):
+        c = Ca()
+        c.setTitle(u'ct')
+        c.setDescription(u'cd')
+        request = TestRequest()
+        view = BrowserView(c, request)
+        setUpEditWidgets(view, Ia)
+        self.assertEqual(view.getTitle(), u'getTitle: ct')
+        self.assertEqual(view.getDescription(), u'getDescription: cd')
+
     def test_setupWidgets_bad_field_name(self):
         c = C()
         request = TestRequest()
@@ -506,6 +533,29 @@
 
         self.assertEqual(c2.title, u'ftt')
         self.assertEqual(c2.description, u'fdd')
+
+    def test_getWidgetsDataForContent_accessors(self):
+        c = Ca()
+        request = TestRequest()
+        request.form['field.getTitle'] = u'ft'
+        request.form['field.getDescription'] = u'fd'
+        view = BrowserView(c, request)
+        setUpWidgets(view, Ia, initial=kw(title=u"ttt", description=u"ddd"))
+        getWidgetsDataForContent(view, Ia)
+
+        self.assertEqual(c.getTitle(), u'ft')
+        self.assertEqual(c.getDescription(), u'fd')
+
+        c2 = Ca()
+        request.form['field.getTitle'] = u'ftt'
+        request.form['field.getDescription'] = u'fdd'
+        getWidgetsDataForContent(view, Ia, c2)
+
+        self.assertEqual(c.getTitle(), u'ft')
+        self.assertEqual(c.getDescription(), u'fd')
+
+        self.assertEqual(c2.getTitle(), u'ftt')
+        self.assertEqual(c2.getDescription(), u'fdd')
 
     def testErrors(self):
         c = C2()