[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser/tests - SchemaTestObject.py:1.9 testFormView.py:1.14

Jim Fulton jim@zope.com
Sat, 7 Sep 2002 12:19:19 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/Views/Browser/tests
In directory cvs.zope.org:/tmp/cvs-serv19433/lib/python/Zope/App/Forms/Views/Browser/tests

Modified Files:
	SchemaTestObject.py testFormView.py 
Log Message:
More cleanup/refactoring of Schemas and forms. There's more to come,
but I'm checkpointing here.

I:

- Added schema field properties. These are like standard Python
  properies except that they are derived from Schema fields.

- Decomposed Str fields into Bytes fields and Text fields.
  Bytes fields contain 8-bit data and are stored as python strings.
  Text fields contain written human discourse, and are stored as
  unicode. It is invalid to store Python strings in Text fields or
  unicode in Bytes fields.

- Moved converters from schemas to forms, where they are used.

- Widgets are now responsible for:

  - Getting raw data from the request

  - Converting raw data to application data

  - Validating converted data against schema fields

- Began defining an error framework for errors in forms.

- Simplified FormViews to reflect new widget responsibilities.

- Added Bytes, Int and Float widgets and changed some application and
  test code to use them.




=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py 1.8 => 1.9 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py:1.8	Thu Sep  5 14:55:01 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py	Sat Sep  7 12:18:48 2002
@@ -16,7 +16,7 @@
 """
 from Interface import Interface
 import Zope.Schema
-from Zope.Schema.Converter import StrToIntConverter
+from Zope.App.Forms.Converter import StrToIntConverter
 
 from Zope.Publisher.HTTP.tests.TestRequest import TestRequest
 from Zope.Publisher.Browser.IBrowserView import IBrowserView
@@ -38,7 +38,7 @@
         return IBrowserView
 
 
-class Email(Zope.Schema.Str):
+class Email(Zope.Schema.Bytes):
     """A simple customized field."""
     pass
 
@@ -50,11 +50,11 @@
         title="Id",
         required=1)
     
-    title = Zope.Schema.Str(
+    title = Zope.Schema.Bytes(
         title="Title",
         required=0)
     
-    data = Zope.Schema.Str(
+    data = Zope.Schema.Bytes(
         title="Data",
         description="Data stored by the object",
         required=0)
@@ -81,8 +81,7 @@
     """A simple Edit View"""
     form = ViewPageTemplateFile('testEditForm.pt')
     schema = ITestObject
-    custom_widgets = {'id': CustomWidget(Widget.TextWidget,
-                                         converter=StrToIntConverter()),
+    custom_widgets = {'id': Widget.IntWidget,
                       'creator': CustomWidget(Widget.TextWidget,
                                               displayWidth=30),
                       'data': CustomWidget(Widget.FileWidget)}


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py 1.13 => 1.14 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py:1.13	Thu Sep  5 14:55:01 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py	Sat Sep  7 12:18:48 2002
@@ -24,8 +24,9 @@
 from Zope.Publisher.Browser.IBrowserView import IBrowserView
 from Zope.App.Forms.Views.Browser.FormView import FormView
 
-from Zope.Schema.IField import IStr
-from Zope.App.Forms.Views.Browser.Widget import TextWidget, FileWidget
+from Zope.Schema.IField import IBytes
+from Zope.App.Forms.Views.Browser.Widget \
+     import TextWidget, IntWidget, FileWidget
 
 import SchemaTestObject
 
@@ -34,12 +35,16 @@
     def setUp(self):
         PlacelessSetup.setUp(self)
         viewService = self.getViewService()
-        viewService.provideView(IStr, 'widget', IBrowserView, [TextWidget])
+        viewService.provideView(IBytes, 'widget', IBrowserView, [TextWidget])
         request = SchemaTestObject.TestBrowserRequest(
             {'field_id': '1', 'field_title': 'Test New',
              'field_creator': 'srichter@cbu.edu',
              'field_data': StringIO('Data')})
         self._form = SchemaTestObject.EditFactory(request=request)
+        self.__data = {'id': 1,
+                       'title': 'Test New',
+                       'creator': 'srichter@cbu.edu',
+                       'data': 'Data'}
         
     def getViewService(self):
         return getService(None, 'Views')
@@ -67,7 +72,7 @@
 
     def testGetWidgetForField(self):
         field = SchemaTestObject.ITestObject.getDescriptionFor('id')
-        widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
+        widget = IntWidget(field, SchemaTestObject.TestBrowserRequest({}))
         result = self._form.getWidgetForField(field)
         self._compareWidgets(widget, result)
 
@@ -79,7 +84,7 @@
 
     def testGetWidgetForFieldName(self):
         field = SchemaTestObject.ITestObject.getDescriptionFor('id')
-        widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
+        widget = IntWidget(field, SchemaTestObject.TestBrowserRequest({}))
         result = self._form.getWidgetForFieldName('id')
         self._compareWidgets(widget, result)
 
@@ -94,7 +99,7 @@
     def testRenderField(self):
         field = SchemaTestObject.ITestObject.getDescriptionFor('id')
         self.assertEqual(
-            '<input name="field_id" type="text" value="5" size="20"  />',
+            '<input name="field_id" type="text" value="5" size="10"  />',
             self._form.renderField(field))
 
         field = SchemaTestObject.ITestObject.getDescriptionFor('creator')
@@ -103,43 +108,8 @@
                          self._form.renderField(field))
 
 
-    def testGetAllRawFieldData(self):
-        data = self._form.getAllRawFieldData()
-        result = {'data': StringIO('Data'), 'id': '1', 'title': 'Test New',
-                  'creator': 'srichter@cbu.edu'}
-        for name, value in data.iteritems():
-            if name == 'data':
-                self.assertEqual(result[name].read(), value.read())
-            else:
-                self.assertEqual(result[name], value)
-
-    def testConvertAllFieldData(self):
-        data = self._form.getAllRawFieldData()
-        data = self._form.convertAllFieldData(data)
-        result = {'data': 'Data', 'id': 1, 'title': 'Test New',
-                  'creator': 'srichter@cbu.edu'}
-        for name, value in data.iteritems():
-            self.assertEqual(result[name], value)
-
-    def testValidateAllFieldData(self):
-        data = self._form.getAllRawFieldData()
-        data = self._form.convertAllFieldData(data)
-        self.assertEqual(None, self._form.validateAllFieldData(data))
-
-
-    def testStoreAllDataInContext(self):
-        data = self._form.getAllRawFieldData()
-        data = self._form.convertAllFieldData(data)
-        self._form.storeAllDataInContext(data)
-        obj = self._form.context
-        for name, value in data.iteritems():
-            self.assertEqual(value, getattr(obj, name))
-
     def testSaveValuesInContext(self):
-        data = self._form.getAllRawFieldData()
-        data = self._form.convertAllFieldData(data)
-        # The StrinIO must be reloaded.
-        self.setUp()
+        data = self.__data
         self._form.saveValuesInContext()
         obj = self._form.context
         for name, value in data.iteritems():