[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form/tests - test_objectwidget.py:1.1 test_browserwidget.py:1.13 test_editview.py:1.12 test_sequencewidget.py:1.3

Richard Jones richard@commonground.com.au
Sun, 13 Jul 2003 02:47:54 -0400


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

Modified Files:
	test_browserwidget.py test_editview.py test_sequencewidget.py 
Added Files:
	test_objectwidget.py 
Log Message:
Implemented Object field types, and ObjectWidget to go with it.
Object fields have Fields on them, and when included in a form view, their
sub-fields fully participate in the form generate and editing. You'd use
this kinda thing for generating an Address field.

The change required the removal of the apply_update method on EditView. It is
replaced by the calling of applyChanges on each widget (facilitated by the 
applyWidgetsChanges function of zope.app.form.utility).

To make this sane, the ObjectWidget must be used via CustomWidget so we can
indicate which factory is to be called to generate the container for the
Object's fields. ObjectWidget and SequenceWidget also allow overriding of
the widgets used to render their sub-fields.

If this is all confusing, then see the new widgets.txt help file in the
zope/app/browser/form/ directory, and it may or may not help :)


=== Added File Zope3/src/zope/app/browser/form/tests/test_objectwidget.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: test_objectwidget.py,v 1.1 2003/07/13 06:47:18 richard Exp $
"""

import unittest

from zope.interface import Interface, implements
from zope.component.view import provideView
from zope.schema.interfaces import ITextLine, ValidationError
from zope.publisher.interfaces.browser import IBrowserPresentation
from zope.publisher.browser import TestRequest
from zope.schema import Object, TextLine
from zope.app.browser.form.widget import TextWidget, ObjectWidget
from zope.app.component.metaconfigure import resolveInterface

from zope.app.browser.form.tests.test_browserwidget import BrowserWidgetTest

class ITestContact(Interface):
    name = TextLine()
    email = TextLine()
class TestContact:
    implements(ITestContact)

class ObjectWidgetTest(BrowserWidgetTest):
    _FieldFactory = Object
    def _WidgetFactory(self, context, request, **kw):
        kw.update({'factory': TestContact})
        return ObjectWidget(context, request, **kw)

    def setUpContent(self):
        provideView(ITextLine, 'edit', IBrowserPresentation, [TextWidget])

        class ITestContent(Interface):
            foo = self._FieldFactory(ITestContact, title = u"Foo Title")
        class TestObject:
            implements(ITestContent)

        self.content = TestObject()
        self.field = ITestContent['foo']
        self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl')
        self.request.form['field.foo'] = u'Foo Value'
        self._widget = self._WidgetFactory(self.field, self.request)

    def test_haveData(self):
        # doesn't work with subfields
        pass

    def testRender(self):
        # doesn't work with subfields
        pass

    def setUp(self):
        BrowserWidgetTest.setUp(self)
        self.field = Object(ITestContact, __name__=u'foo')
        provideView(ITextLine, 'edit', IBrowserPresentation, [TextWidget])

    def test_applyChanges(self):
        self.request.form['field.foo.name'] = u'Foo Name'
        self.request.form['field.foo.email'] = u'foo@foo.test'
        widget = self._WidgetFactory(self.field, self.request)

        self.assertEqual(widget.applyChanges(self.content), True)
        self.assertEqual(hasattr(self.content, 'foo'), True)
        self.assertEqual(isinstance(self.content.foo, TestContact), True)
        self.assertEqual(self.content.foo.name, u'Foo Name')
        self.assertEqual(self.content.foo.email, u'foo@foo.test')

    def test_applyChangesNoChange(self):
        self.content.foo = TestContact()
        self.content.foo.name = u'Foo Name'
        self.content.foo.email = u'foo@foo.test'

        self.request.form['field.foo.name'] = u'Foo Name'
        self.request.form['field.foo.email'] = u'foo@foo.test'
        widget = self._WidgetFactory(self.field, self.request)
        widget.setData(self.content.foo)

        self.assertEqual(widget.applyChanges(self.content), False)
        self.assertEqual(hasattr(self.content, 'foo'), True)
        self.assertEqual(isinstance(self.content.foo, TestContact), True)
        self.assertEqual(self.content.foo.name, u'Foo Name')
        self.assertEqual(self.content.foo.email, u'foo@foo.test')

    def test_new(self):
        request = TestRequest()
        widget = ObjectWidget(self.field, request, TestContact)
        self.assertEquals(int(widget.haveData()), 0)
        check_list = (
            'input', 'name="field.foo.name"',
            'input', 'name="field.foo.email"'
        )
        self.verifyResult(widget(), check_list)

    def test_edit(self):
        request = TestRequest(form={
            'field.foo.name': u'fred',
            'field.foo.email': u'fred@fred.com'
            })
        widget = ObjectWidget(self.field, request, TestContact)
        self.assertEquals(int(widget.haveData()), 1)
        o = widget.getData()
        self.assertEquals(hasattr(o, 'name'), 1)
        self.assertEquals(o.name, u'fred')
        self.assertEquals(o.email, u'fred@fred.com')
        check_list = (
            'input', 'name="field.foo.name"', 'value="fred"',
            'input', 'name="field.foo.email"', 'value="fred@fred.com"',
        )
        self.verifyResult(widget(), check_list)

def test_suite():
    return unittest.makeSuite(ObjectWidgetTest)

if __name__=='__main__':
    unittest.main(defaultTest='test_suite')

# vim: set filetype=python ts=4 sw=4 et si




=== Zope3/src/zope/app/browser/form/tests/test_browserwidget.py 1.12 => 1.13 ===
--- Zope3/src/zope/app/browser/form/tests/test_browserwidget.py:1.12	Thu Jun  5 10:23:05 2003
+++ Zope3/src/zope/app/browser/form/tests/test_browserwidget.py	Sun Jul 13 02:47:18 2003
@@ -16,6 +16,7 @@
 $Id$
 """
 
+from zope.interface import Interface, implements
 from zope.app.browser.form.widget import BrowserWidget
 from zope.app.interfaces.form import ConversionError
 from zope.app.interfaces.form import WidgetInputError, MissingInputError
@@ -33,17 +34,25 @@
 class BrowserWidgetTest(PlacelessSetup,
                         support.VerifyResults,
                         unittest.TestCase):
-
     _FieldFactory = Text
     _WidgetFactory = BrowserWidget
 
-    def setUp(self):
-        PlacelessSetup.setUp(self)
-        field = self._FieldFactory(__name__ = 'foo', title = u"Foo Title")
+    def setUpContent(self):
+        class ITestContent(Interface):
+            foo = self._FieldFactory(title = u"Foo Title")
+        class TestObject:
+            implements(ITestContent)
+
+        self.content = TestObject()
+        field = ITestContent['foo']
         request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl')
         request.form['field.foo'] = u'Foo Value'
         self._widget = self._WidgetFactory(field, request)
 
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        self.setUpContent()
+
     def test_required(self):
         self._widget.context.required = False
         self.failIf(self._widget.required)
@@ -145,6 +154,8 @@
         self.assertEqual(self._widget.getData(optional=1), None)
         self.assertEqual(self._widget.getData(), None)
 
+    def test_applyChanges(self):
+        self.assertEqual(self._widget.applyChanges(self.content), True)
 
     def test_haveData(self):
         self.failUnless(self._widget.haveData())


=== Zope3/src/zope/app/browser/form/tests/test_editview.py 1.11 => 1.12 ===
--- Zope3/src/zope/app/browser/form/tests/test_editview.py:1.11	Fri Jul 11 21:29:01 2003
+++ Zope3/src/zope/app/browser/form/tests/test_editview.py	Sun Jul 13 02:47:18 2003
@@ -33,11 +33,12 @@
     foo = TextLine(title=u"Foo")
     bar = TextLine(title=u"Bar")
     a   = TextLine(title=u"A")
-    b   = TextLine(title=u"B", min_length=0)
+    b   = TextLine(title=u"B", min_length=0, required=False)
     getbaz, setbaz = accessors(TextLine(title=u"Baz"))
 
 class EV(EditView):
     schema = I
+    object_factories = []
 
 class C:
     implements(I)
@@ -91,79 +92,6 @@
             [w.name for w in v.widgets()],
             ['test.foo', 'test.bar', 'test.a', 'test.b', 'test.getbaz']
             )
-
-    def test_apply_update_no_data(self):
-        c = C()
-        request = TestRequest()
-        v = EV(c, request)
-        d = {}
-        d['foo'] = u'c foo'
-        d['bar'] = u'c bar'
-        d['getbaz'] = u'c baz'
-        self.failUnless(v.apply_update(d))
-        self.assertEqual(c.foo, u'c foo')
-        self.assertEqual(c.bar, u'c bar')
-        self.assertEqual(c.a  , u'c a')
-        self.assertEqual(c.b  , u'c b')
-        self.assertEqual(c.getbaz(), u'c baz')
-        self.failIf(getEvents())
-
-    def test_apply_update(self):
-        c = C()
-        request = TestRequest()
-        v = EV(c, request)
-        d = {}
-        d['foo'] = u'd foo'
-        d['bar'] = u'd bar'
-        d['getbaz'] = u'd baz'
-        d['b'] = u''
-        self.failIf(v.apply_update(d))
-        self.assertEqual(c.foo, u'd foo')
-        self.assertEqual(c.bar, u'd bar')
-        self.assertEqual(c.a  , u'c a')
-        self.assertEqual(c.b  , u'')
-        self.assertEqual(c.getbaz(), u'd baz')
-        self.failUnless(getEvents(filter=lambda event: event.object == c))
-
-    def test_apply_update_changed(self):
-        class EVc(EV):
-            _changed = 0
-            def changed(self):
-                self._changed += 1
-        
-        c = C()
-        request = TestRequest()
-        v = EVc(c, request)
-        oldchanged = v._changed
-        d = {}
-        d['foo'] = u'd foo'
-        d['bar'] = u'd bar'
-        d['getbaz'] = u'd baz'
-        self.failIf(v.apply_update(d))
-        self.assertEqual(c.foo, u'd foo')
-        self.assertEqual(c.bar, u'd bar')
-        self.assertEqual(c.a  , u'c a')
-        self.assertEqual(c.b  , u'c b')
-        self.assertEqual(c.getbaz(), u'd baz')
-        self.failUnless(getEvents(filter=lambda event: event.object == c))
-
-        # make sure that changed was called
-        self.assertEqual(v._changed, oldchanged + 1)
-        self.failUnless(v.apply_update(d))
-        self.assertEqual(v._changed, oldchanged + 1)
-
-    def test_apply_update_w_adapter(self):
-        c = Foo()
-        request = TestRequest()
-        v = BarV(c, request)
-        d = {}
-        d['bar'] = u'd bar'
-        self.failIf(v.apply_update(d))
-        self.assertEqual(c.foo, u'd bar')
-
-        # We should not get events whan an adapter is used. That's the
-        # adapter's job.
-        self.failIf(getEvents())
 
     def test_fail_wo_adapter(self):
         c = Foo()


=== Zope3/src/zope/app/browser/form/tests/test_sequencewidget.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/browser/form/tests/test_sequencewidget.py:1.2	Fri Jul 11 22:47:11 2003
+++ Zope3/src/zope/app/browser/form/tests/test_sequencewidget.py	Sun Jul 13 02:47:18 2003
@@ -1,5 +1,19 @@
-__rcs_id__ = '$Id'
-__version__ = '$Revision$'[11:-2]
+##############################################################################
+#
+# 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.0 (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.
+#
+##############################################################################
+"""
+$Id$
+"""
 
 import unittest
 
@@ -15,16 +29,13 @@
 
 from zope.app.browser.form.tests.test_browserwidget import BrowserWidgetTest
 
-
 class SequenceWidgetTest(BrowserWidgetTest):
     def _FieldFactory(self, **kw):
         kw.update({'__name__': u'foo', 'value_type': TextLine(__name__=u'bar')})
         return Tuple(**kw)
     _WidgetFactory = TupleSequenceWidget
 
-    def verifyResult(self, result, check_list, inorder=False):
-        pass
-    def verifyResultMissing(self, result, check_list):
+    def testRender(self):
         pass
 
     def setUp(self):
@@ -71,7 +82,7 @@
         self.assertEquals(int(widget.haveData()), 1)
         self.assertRaises(ValidationError, widget.getData)
         check_list = (
-            'checkbox', 'field.foo.remove_0', 'input', 'field.foo.0.bar'
+            'checkbox', 'field.foo.remove_0', 'input', 'field.foo.0.bar',
             'submit', 'submit', 'field.foo.add'
         )
         self.verifyResult(widget(), check_list, inorder=True)
@@ -127,8 +138,8 @@
         self.assertEquals(widget.getData(), (u'existing',))
         check_list = (
             'input', 'field.foo.0.bar', 'existing',
-            'input', 'field.foo.0.bar', 'value=""',
-            'submit', 'submit', 'field.foo.add'
+            'input', 'field.foo.1.bar', 'value=""',
+            'submit', 'field.foo.add'
         )
         s = widget()
         self.verifyResult(s, check_list, inorder=True)
@@ -143,6 +154,18 @@
         s = widget()
         self.assertEquals(s.find('field.foo.add'), -1)
 
+    def test_anonymousfield(self):
+        self.field = Tuple(__name__=u'foo', value_type=TextLine())
+        request = TestRequest()
+        widget = TupleSequenceWidget(self.field, request)
+        widget.setData((u'existing',))
+        s = widget()
+        check_list = (
+            'input', '"field.foo.0."', 'existing',
+            'submit', 'submit', 'field.foo.add'
+        )
+        s = widget()
+        self.verifyResult(s, check_list, inorder=True)
 
 def test_suite():
     return unittest.makeSuite(SequenceWidgetTest)