[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/tests - testWidget.py:1.1 testUtility.py:1.4 testConverter.py:NONE
Jim Fulton
jim@zope.com
Mon, 11 Nov 2002 15:52:58 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/tests
In directory cvs.zope.org:/tmp/cvs-serv19223/tests
Modified Files:
testUtility.py
Added Files:
testWidget.py
Removed Files:
testConverter.py
Log Message:
Changed the getName and getTitle methods to name and title properties.
The name property is just the prefix, a dot and the field name. The
field name is used to generate request variables, although, widgets
using multiple input elements will often need to generate more
specific names.
Added haveData method to test whether there is user input.
Added required widget attribute that simply delegates to the
underlying field required attribute.
Added support for CustomWidgets in automatic widget setup code.
Added fieldNames utility function to get the names of fields defined
by a schema in definition order.
Added haveWidgets data to easily tests whether any widgets have
data. This can be used to determine if a form was submitted or is
being viewed for the first time. It's better to use an explicit
request variable, set by a submit button to test this.
Moved the setPrefix, name, and setData machinery to the generic widget
code.
=== Added File Zope3/lib/python/Zope/App/Forms/tests/testWidget.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.
#
##############################################################################
"""XXX short summary goes here.
XXX longer description goes here.
$Id: testWidget.py,v 1.1 2002/11/11 20:52:57 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.Forms.Widget import Widget, CustomWidget
from Zope.App.Forms.IWidget import IWidget
from Interface.Verify import verifyObject
from Zope.Schema import Text
from Zope.Publisher.Browser.BrowserRequest import TestRequest
from Zope.ComponentArchitecture.IView import IViewFactory
class TestWidget(TestCase):
def test_name(self):
w = Widget(Text(__name__='foo', title=u'Foo title'), TestRequest())
self.assertEqual(w.name, 'field.foo')
def test_setPrefix(self):
w = Widget(Text(__name__='foo', title=u'Foo title'), TestRequest())
w.setPrefix('test')
self.assertEqual(w.name, 'test.foo')
def test_title(self):
w = Widget(Text(__name__='foo', title=u'Foo title'), TestRequest())
self.assertEqual(w.title, 'Foo title')
def test_IWidget(self):
w = Widget(Text(__name__='foo', title=u'Foo title'), TestRequest())
verifyObject(IWidget, w)
# XXX Don't test getValue. It's silly and will go away.
class TestCustomWidget(TestCase):
# XXX this test should be rewritten once we've refactored widget properties
def test(self):
cw = CustomWidget(Widget, width=60)
verifyObject(IViewFactory, cw)
w = cw(Text(__name__='foo', title=u'Foo title'), TestRequest())
self.assertEqual(w.name, 'field.foo')
self.assertEqual(w.width, 60)
verifyObject(IWidget, w)
def test_suite():
return TestSuite((
makeSuite(TestWidget),
makeSuite(TestCustomWidget),
))
if __name__=='__main__':
main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Forms/tests/testUtility.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/tests/testUtility.py:1.3 Tue Oct 29 12:12:37 2002
+++ Zope3/lib/python/Zope/App/Forms/tests/testUtility.py Mon Nov 11 15:52:57 2002
@@ -30,19 +30,22 @@
from Zope.ComponentArchitecture.GlobalViewService \
import provideView, setDefaultViewName
from Zope.Schema.IField import IText
-from Zope.App.Forms.Exceptions import WidgetsError
+from Zope.App.Forms.Exceptions import WidgetsError, MissingInputError
from Zope.App.Forms.Utility import setUpWidget, setUpWidgets, setUpEditWidgets
from Zope.App.Forms.Utility import getWidgetsData, getWidgetsDataForContent
+from Zope.App.Forms.Utility import haveWidgetsData, fieldNames
from Zope.Schema.Exceptions import ValidationError
+from Zope.ComponentArchitecture.IView import IViewFactory
class I(Interface):
title = Text(title=u"Title")
- description = Text(title=u"Description")
+ description = Text(title=u"Description",
+ default = u'No description')
class I2(Interface):
- title = Text(title=u"Title", required=True)
- description = Text(title=u"Description", required=True)
+ title = Text(title = u"Title", required = True)
+ description = Text(title = u"Description", required = True)
class C:
__implements__ = I
@@ -50,6 +53,15 @@
class C2:
__implements__ = I2
+class ViewWithCustomTitleWidgetFactory(BrowserView):
+
+ def title(self, context, request):
+ w = W(context, request)
+ w.custom = 1
+ return w
+
+ title.__implements__ = IViewFactory
+
class W(TextWidget):
def setData(self, v):
@@ -60,20 +72,25 @@
self.prefix = prefix
def __call__(self):
- name = self.getName()
- if name in self.request:
+ name = self.name
+ v = getattr(self, '_data', None)
+ if (v is None) and (name in self.request):
v = self.request[name]
- else:
- v = getattr(self, '_data', None) or ''
- return unicode(name) + u': ' + v
+
+ return unicode(self.context.__name__) + u': ' + (v or '')
def getData(self):
- v = self.request.get(self.getName())
- if not v and self.context.required:
- raise ValidationError("%s required" % self.getName())
+ v = self.request.get(self.name, self)
+ if v is self:
+ if self.context.required:
+ raise ValidationError("%s required" % self.name)
+ v = self.context.default
return v
+ def haveData(self):
+ return self.name in self.request
+
class Test(PlacelessSetup, TestCase):
def setUp(self):
@@ -81,6 +98,18 @@
setDefaultViewName(IText, IBrowserPresentation, 'normal')
provideView(IText, 'normal', IBrowserPresentation, W)
+ def test_fieldNames(self):
+
+ class I3(I2):
+ foo = Text()
+ bar = Text()
+ foo2 = Text()
+
+ self.assertEqual(tuple(fieldNames(I3)),
+ ('title', 'description', 'foo', 'bar', 'foo2'))
+
+
+
def test_setUpWidget(self):
c = C()
request = TestRequest()
@@ -89,10 +118,11 @@
self.assertEqual(view.title(), u'title: ')
self.assertEqual(view.title.getData(), None)
+
def test_setUpWidget_w_request_data(self):
c = C()
request = TestRequest()
- request.form['title'] = u'xxx'
+ request.form['field.title'] = u'xxx'
view = BrowserView(c, request)
setUpWidget(view, 'title', I['title'])
self.assertEqual(view.title(), u'title: xxx')
@@ -101,12 +131,21 @@
def test_setUpWidget_w_request_data_and_initial_data(self):
c = C()
request = TestRequest()
- request.form['title'] = u'xxx'
+ request.form['field.title'] = u'xxx'
view = BrowserView(c, request)
setUpWidget(view, 'title', I['title'], u'yyy')
self.assertEqual(view.title(), u'title: xxx')
self.assertEqual(view.title.getData(), u'xxx')
+ def test_setUpWidget_w_request_data_and_initial_data_force(self):
+ c = C()
+ request = TestRequest()
+ request.form['field.title'] = u'xxx'
+ view = BrowserView(c, request)
+ setUpWidget(view, 'title', I['title'], u'yyy', force=1)
+ self.assertEqual(view.title(), u'title: yyy')
+ self.assertEqual(view.title.getData(), u'xxx')
+
def test_setUpWidget_w_initial_data(self):
c = C()
request = TestRequest()
@@ -131,6 +170,15 @@
self.assertEqual(view.title(), u'title: yyy')
self.assertEqual(view.title.getData(), None)
self.assertEqual(view.title, w)
+
+ def test_setUpWidget_w_Custom_widget(self):
+ c = C()
+ request = TestRequest()
+ view = ViewWithCustomTitleWidgetFactory(c, request)
+ setUpWidget(view, 'title', I['title'], u'yyy')
+ self.assertEqual(view.title(), u'title: yyy')
+ self.assertEqual(view.title.getData(), None)
+ self.assertEqual(view.title.custom, 1)
def test_setupWidgets(self):
c = C()
@@ -157,6 +205,22 @@
self.assertEqual(view.title(), u'title: ttt')
self.assertEqual(view.description(), u'description: ddd')
self.assertEqual(view.title, w)
+
+ def test_setupWidgets_w_initial_data_and_request_data(self):
+ c = C()
+ request = TestRequest()
+ request.form['field.title'] = u'yyy'
+ view = BrowserView(c, request)
+ setUpWidgets(view, I, title=u"ttt", description=u"ddd")
+ self.assertEqual(view.title(), u'title: yyy')
+
+ def test_setupWidgets_w_initial_data_forced_and_request_data(self):
+ c = C()
+ request = TestRequest()
+ request.form['field.title'] = u'yyy'
+ view = BrowserView(c, request)
+ setUpWidgets(view, I, title=u"ttt", description=u"ddd", force=1)
+ self.assertEqual(view.title(), u'title: ttt')
def test_setupEditWidgets_w_custom_widget(self):
c = C()
@@ -170,6 +234,30 @@
self.assertEqual(view.description(), u'description: cd')
self.assertEqual(view.title, w)
+ def test_setupEditWidgets_w_form_data(self):
+ c = C()
+ c.title = u'ct'
+ c.description = u'cd'
+ request = TestRequest()
+ request.form['field.title'] = u'ft'
+ request.form['field.description'] = u'fd'
+ view = BrowserView(c, request)
+ setUpEditWidgets(view, I)
+ self.assertEqual(view.title(), u'title: ft')
+ self.assertEqual(view.description(), u'description: fd')
+
+ def test_setupEditWidgets_w_form_data_force(self):
+ c = C()
+ c.title = u'ct'
+ c.description = u'cd'
+ request = TestRequest()
+ request.form['field.title'] = u'ft'
+ request.form['field.description'] = u'ft'
+ view = BrowserView(c, request)
+ setUpEditWidgets(view, I, force=1)
+ self.assertEqual(view.title(), u'title: ct')
+ self.assertEqual(view.description(), u'description: cd')
+
def test_setupEditWidgets_w_custom_widget_and_prefix(self):
c = C()
c.title = u'ct'
@@ -204,7 +292,7 @@
def test_getSetupWidgets_w_form_data(self):
c = C()
request = TestRequest()
- request.form['title'] = u'ft'
+ request.form['field.title'] = u'ft'
view = BrowserView(c, request)
setUpWidgets(view, I, title=u"ttt", description=u"ddd")
self.assertEqual(view.title(), u'title: ft')
@@ -214,19 +302,44 @@
def test_getWidgetsData(self):
c = C()
request = TestRequest()
- request.form['title'] = u'ft'
- request.form['description'] = u'fd'
+ request.form['field.title'] = u'ft'
+ request.form['field.description'] = u'fd'
view = BrowserView(c, request)
setUpWidgets(view, I, title=u"ttt", description=u"ddd")
self.assertEqual(getWidgetsData(view, I),
{'title': u'ft',
'description': u'fd'})
+ def test_haveWidgetsData(self):
+ c = C()
+ request = TestRequest()
+ view = BrowserView(c, request)
+ setUpWidgets(view, I, title=u"ttt", description=u"ddd")
+ self.failIf(haveWidgetsData(view, I))
+
+ request.form['field.description'] = u'fd'
+ self.failUnless(haveWidgetsData(view, I))
+
+ def test_getWidgetsData_w_default(self):
+ c = C()
+ request = TestRequest()
+ view = BrowserView(c, request)
+ setUpWidgets(view, I, title=u"ttt", description=u"ddd")
+ self.assertEqual(getWidgetsData(view, I, required=0), {})
+
+ self.assertRaises(MissingInputError, getWidgetsData, view, I)
+
+ request.form['field.description'] = u'fd'
+ self.assertEqual(getWidgetsData(view, I, required=0),
+ {'description': u'fd'})
+
+ self.assertRaises(MissingInputError, getWidgetsData, view, I)
+
def test_getWidgetsDataForContent(self):
c = C()
request = TestRequest()
- request.form['title'] = u'ft'
- request.form['description'] = u'fd'
+ request.form['field.title'] = u'ft'
+ request.form['field.description'] = u'fd'
view = BrowserView(c, request)
setUpWidgets(view, I, title=u"ttt", description=u"ddd")
getWidgetsDataForContent(view, I)
@@ -235,8 +348,8 @@
self.assertEqual(c.description, u'fd')
c2 = C()
- request.form['title'] = u'ftt'
- request.form['description'] = u'fdd'
+ request.form['field.title'] = u'ftt'
+ request.form['field.description'] = u'fdd'
getWidgetsDataForContent(view, I, c2)
self.assertEqual(c.title, u'ft')
@@ -250,27 +363,17 @@
c.title = u'old title'
c.description = u'old description'
request = TestRequest()
- request.form['title'] = u'ft'
+ request.form['field.title'] = u'ft'
view = BrowserView(c, request)
setUpWidgets(view, I2, title=u"ttt", description=u"ddd")
- try:
- getWidgetsDataForContent(view, I2)
- except WidgetsError, v:
- self.assertEqual(str(v), "description required")
- else:
- self.assert_(0, "No errors were raised")
-
- self.assertEqual(c.title, u'old title')
+ getWidgetsDataForContent(view, I2)
+ self.assertEqual(c.title, u'ft')
self.assertEqual(c.description, u'old description')
request = TestRequest()
+ c.title = u'old title'
view = BrowserView(c, request)
setUpWidgets(view, I2, title=u"ttt", description=u"ddd")
- try:
- getWidgetsDataForContent(view, I2)
- except WidgetsError, v:
- self.assertEqual(len(v), 2)
-
self.assertEqual(c.title, u'old title')
self.assertEqual(c.description, u'old description')
=== Removed File Zope3/lib/python/Zope/App/Forms/tests/testConverter.py ===