[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser/tests - testConfigure.zcml:1.1 testEditForm.pt:1.1 SchemaTestObject.py:1.2 testFormView.py:1.6
Stephan Richter
srichter@cbu.edu
Tue, 16 Jul 2002 19:42:59 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/Views/Browser/tests
In directory cvs.zope.org:/tmp/cvs-serv22472/lib/python/Zope/App/Forms/Views/Browser/tests
Modified Files:
SchemaTestObject.py testFormView.py
Added Files:
testConfigure.zcml testEditForm.pt
Log Message:
Insert worked some more on Forms and their tests. I am not done yet, but my
last checkin broke stuff, so I am making this checkin to have the tests
pass.
=== Added File Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testConfigure.zcml ===
<zopeConfigure
xmlns='http://namespaces.zope.org/zope'
xmlns:browser='http://namespaces.zope.org/browser'
package="Zope.App.Forms.Views.Browser"
>
<!-- Test view for folders -->
<browser:view
permission="Zope.View"
for="Zope.App.OFS.Content.Folder.Folder.IFolder."
factory=".SchemaTestObject.EditFactory">
<browser:page name="testSchemaFormView.html" attribute="form" />
</browser:view>
</zopeConfigure>
=== Added File Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testEditForm.pt ===
<html>
<head>
<title>Simple Edit Form</title>
</head>
<body>
<form action="." method="post">
<table>
<tal:block repeat="field view/getFields">
<tr>
<th tal:content="field/title">Title:</th>
<td tal:content="view/renderField">
<input size="20" />
</td>
</tr>
</tal:block>
</table>
<input type="submit" name="action:method" value="Save" />
</form>
</body>
</html>
=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py 1.1 => 1.2 ===
$Id$
"""
import Schema
+
+from Zope.Publisher.HTTP.tests.TestRequest import TestRequest
+from Zope.Publisher.Browser.IBrowserView import IBrowserView
+from Zope.App.PageTemplate import ViewPageTemplateFile
+
from Zope.App.Forms.Views.Browser import Widget
+from Zope.App.Forms.Widget import CustomWidget
+from Zope.App.Forms.Views.Browser.FormView import FormView
+
+
+class TestBrowserRequest(TestRequest):
+ """Since we have IBrowserViews, we need a request that works
+ for IBrowserView."""
+ def __init__(self, form):
+ super(TestBrowserRequest, self).__init__()
+ self.form = form
+
+ def getPresentationType(self):
+ return IBrowserView
-class Email(Schema.String):
+
+class Email(Schema.Str):
+ """A simple customized field."""
pass
-class STestObject(Schema):
- id = Schema.String(
- id="id",
- title="Id",
- required=1)
-
- title = Schema.String(
- id="title",
- title="Title",
- required=0)
-
- data = Schema.String(
- id="data",
- title="Data",
- description="Data stored by the object",
- required=0)
-
- creator = Email(
- id="creator",
- title="Creator",
- description="Email of the creator of the content object",
- default="foo@bar.com",
- required=1)
-
-
-class TestObject:
-
- __schema__ = Schema
-
- id = None
- title = "Test Object"
- data = ''
- creator = ''
+class STestObject(Schema.Schema):
+ """A simple Schema."""
+
+ id = Schema.Str(
+ id="id",
+ title="Id",
+ required=1)
+
+ title = Schema.Str(
+ id="title",
+ title="Title",
+ required=0)
+
+ data = Schema.Str(
+ id="data",
+ title="Data",
+ description="Data stored by the object",
+ required=0)
+
+ creator = Email(
+ id="creator",
+ title="Creator",
+ description="Email of the creator of the content object",
+ default="foo@bar.com",
+ required=1)
+
+
+class TestObject(object):
+ """A very simple content object."""
+ __implements__ = STestObject
+
+ def __init__(self, id, title, creator, data=None):
+ self.id = id
+ self.title = title
+ self.creator = creator
+ self.data = data
class Edit(FormView):
+ """A simple Edit View"""
+ form = ViewPageTemplateFile('testEditForm.pt')
+ custom_widgets = {'creator': CustomWidget(Widget.TextWidget,
+ displayWidth=30),
+ 'data': CustomWidget(Widget.FileWidget)}
+
+
+def EditFactory(context=None, request=None):
+
+ if request is None:
+ request = TestBrowserRequest({})
- schema = STestObject
- custom_widgets = {'creator': Widget.TextWidget(displaywidth=30),
- 'data': Widget.FileWidget()}
+ object = TestObject(id=1, title="Test", creator="srichter@cbu.edu",
+ data="Some data")
+ return Edit(object, request)
=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py 1.5 => 1.6 ===
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""XXX short summary goes here.
-
-XXX longer description goes here.
-
+"""
$Id$
"""
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
-
-from Interface import Interface
from Zope.ComponentArchitecture import getService
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
-from Zope.Publisher.Browser.IBrowserView import IBrowserView
-from Zope.Publisher.HTTP.tests.TestRequest import TestRequest
-from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.Publisher.Browser.IBrowserView import IBrowserView
from Zope.App.Forms.Views.Browser.FormView import FormView
-from Zope.App.Forms.Views.Browser.TextWidget import TextWidget
-from Zope.App.Forms.Views.Browser.CheckboxWidget import CheckboxWidget
+from Schema.IField import IStr
+from Zope.App.Forms.Views.Browser.Widget import TextWidget, FileWidget
-import Schema
-from Schema.IField import IStr, IBool
-from Schema import _Schema # XXX to wire things up, should fix this
-
-#############################################################################
-# If your tests change any global registries, then uncomment the
-# following import and include CleanUp as a base class of your
-# test. It provides a setUp and tearDown that clear global data that
-# has registered with the test cleanup framework. Don't use this
-# tests outside the Zope package.
+import SchemaTestObject
-from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
-
-#############################################################################
-class ITestSchema(Interface):
- foo = Schema.Str(title="Foo")
- bar = Schema.Bool(title="Bar")
-
-class TestBrowserRequest(TestRequest):
- """Since we have IBrowserViews, we need a request that works
- for IBrowserView.
- """
- def getPresentationType(self):
- return IBrowserView
-
class TestFormView(TestCase, PlacelessSetup):
def setUp(self):
PlacelessSetup.setUp(self)
viewService = self.getViewService()
- viewService.provideView(IStr, 'normal', IBrowserView,
- [TextWidget])
- viewService.provideView(IBool, 'normal', IBrowserView,
- [CheckboxWidget])
+ viewService.provideView(IStr, 'widget', IBrowserView, [TextWidget])
+ request = SchemaTestObject.TestBrowserRequest(
+ {'field_id': 1, 'field_title': 'Test New',
+ 'field_creator': 'srichter@cbu.edu', 'field_data': 'Data'})
+ self._form = SchemaTestObject.EditFactory(request=request)
def getViewService(self):
return getService(None, 'Views')
+
+ def testGetFields(self):
+ fields = []
+ schema = SchemaTestObject.STestObject
+ for name in schema.names(1):
+ fields.append(schema.getDescriptionFor(name))
+ self.assertEqual(fields, self._form.getFields())
+
+
+ def _compareWidgets(self, widget1, widget2):
+ self.assertEqual(widget1.__class__, widget2.__class__)
+ for prop in widget1.propertyNames:
+ self.assertEqual(widget1.getValue(prop), widget2.getValue(prop))
+ for prop in widget2.propertyNames:
+ self.assertEqual(widget2.getValue(prop), widget1.getValue(prop))
+
+
+ def testGetWidgetForField(self):
+ field = SchemaTestObject.STestObject.getDescriptionFor('id')
+ widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
+ result = self._form.getWidgetForField(field)
+ self._compareWidgets(widget, result)
+
+ field = SchemaTestObject.STestObject.getDescriptionFor('data')
+ widget = FileWidget(field, SchemaTestObject.TestBrowserRequest({}))
+ result = self._form.getWidgetForField(field)
+ self._compareWidgets(widget, result)
+
+
+ def testGetWidgetForFieldId(self):
+ field = SchemaTestObject.STestObject.getDescriptionFor('id')
+ widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
+ result = self._form.getWidgetForFieldId('id')
+ self._compareWidgets(widget, result)
+
+ field = SchemaTestObject.STestObject.getDescriptionFor('data')
+ widget = FileWidget(field, SchemaTestObject.TestBrowserRequest({}))
+ result = self._form.getWidgetForFieldId('data')
+ self._compareWidgets(widget, result)
+
+ self.assertRaises(KeyError, self._form.getWidgetForFieldId, 'foo')
+
- def test_getWidgetsForSchema(self):
- # viewService = self.getViewService()
- # view = FormView(None, TestBrowserRequest())
- # widgets = view.getWidgetsForSchema(ITestSchema, 'normal')
- # # XXX order is undefined. Fix this when we have order.
- # self.assert_(isinstance(widgets[0], TextWidget) or
- # isinstance(widgets[1], TextWidget))
- # self.assert_(isinstance(widgets[0], CheckboxWidget) or
- # isinstance(widgets[1], CheckboxWidget))
- pass
+ def testRenderField(self):
+ field = SchemaTestObject.STestObject.getDescriptionFor('id')
+ self.assertEqual(
+ '<input name="field_id" type="text" value="1" size="20" />',
+ self._form.renderField(field))
+
+ field = SchemaTestObject.STestObject.getDescriptionFor('creator')
+ self.assertEqual('<input name="field_creator" type="text" '
+ 'value="srichter@cbu.edu" size="30" />',
+ self._form.renderField(field))
+
def test_suite():
return TestSuite((