[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser/tests - SchemaTestObject.py:1.7 WidgetTest.py:1.3 testBrowserWidget.py:1.3 testFormView.py:1.12 testListWidget.py:1.4 testMultiCheckboxWidget.py:1.4 testMultiListWidget.py:1.4 testRadioWidget.py:1.4

Martijn Faassen m.faassen@vet.uu.nl
Wed, 4 Sep 2002 09:44:55 -0400


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

Modified Files:
	SchemaTestObject.py WidgetTest.py testBrowserWidget.py 
	testFormView.py testListWidget.py testMultiCheckboxWidget.py 
	testMultiListWidget.py testRadioWidget.py 
Log Message:
Refactored Forms (and Schema).

  * A FormView now specifies a single schema explicitly as the 'schema'
    (class) attribute. A schema is simply an interface with Field attributes
    on it.

  * The FormView code now uses the Schema package more for its own
    implementation, instead of trying to reimplement things itself.

  * got rid of the 'id' attribute for fields. Fields now already know what
    name they have, because of the Interface package setting __name__.
    Use getName() to get the field name.



=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py 1.6 => 1.7 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py:1.6	Thu Jul 25 18:09:30 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/SchemaTestObject.py	Wed Sep  4 09:44:24 2002
@@ -43,27 +43,23 @@
     pass
 
 
-class STestObject(Interface):
+class ITestObject(Interface):
     """A simple Schema."""
 
     id = Schema.Int(
-        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",
@@ -72,7 +68,7 @@
     
 class TestObject(object):
     """A very simple content object."""
-    __implements__ = STestObject
+    __implements__ = ITestObject
 
     def __init__(self, id, title, creator, data=None):
         self.id = id
@@ -84,6 +80,7 @@
 class Edit(FormView):
     """A simple Edit View"""
     form = ViewPageTemplateFile('testEditForm.pt')
+    schema = ITestObject
     custom_widgets = {'id': CustomWidget(Widget.TextWidget,
                                          converter=StrToIntConverter()),
                       'creator': CustomWidget(Widget.TextWidget,


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/WidgetTest.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/WidgetTest.py:1.2	Wed Jul 17 12:54:15 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/WidgetTest.py	Wed Sep  4 09:44:24 2002
@@ -24,8 +24,6 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
-    
     def __init__(self, context):
         self._context  = context
 


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testBrowserWidget.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testBrowserWidget.py:1.2	Wed Jul 17 12:54:15 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testBrowserWidget.py	Wed Sep  4 09:44:24 2002
@@ -20,7 +20,8 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
+    def getName(self):
+        return 'foo'
     
 
 class BrowserWidgetTest(unittest.TestCase):


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py 1.11 => 1.12 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py:1.11	Wed Jul 24 06:53:48 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testFormView.py	Wed Sep  4 09:44:24 2002
@@ -46,7 +46,7 @@
 
     def testGetFields(self):
         fields = []
-        schema = SchemaTestObject.STestObject
+        schema = SchemaTestObject.ITestObject
         for name in schema.names(1):
             fields.append(schema.getDescriptionFor(name))
         fields.sort()
@@ -66,38 +66,38 @@
 
 
     def testGetWidgetForField(self):
-        field = SchemaTestObject.STestObject.getDescriptionFor('id')
+        field = SchemaTestObject.ITestObject.getDescriptionFor('id')
         widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
         result = self._form.getWidgetForField(field)
         self._compareWidgets(widget, result)
 
-        field = SchemaTestObject.STestObject.getDescriptionFor('data')
+        field = SchemaTestObject.ITestObject.getDescriptionFor('data')
         widget = FileWidget(field, SchemaTestObject.TestBrowserRequest({}))
         result = self._form.getWidgetForField(field)
         self._compareWidgets(widget, result)
 
 
-    def testGetWidgetForFieldId(self):
-        field = SchemaTestObject.STestObject.getDescriptionFor('id')
+    def testGetWidgetForFieldName(self):
+        field = SchemaTestObject.ITestObject.getDescriptionFor('id')
         widget = TextWidget(field, SchemaTestObject.TestBrowserRequest({}))
-        result = self._form.getWidgetForFieldId('id')
+        result = self._form.getWidgetForFieldName('id')
         self._compareWidgets(widget, result)
 
-        field = SchemaTestObject.STestObject.getDescriptionFor('data')
+        field = SchemaTestObject.ITestObject.getDescriptionFor('data')
         widget = FileWidget(field, SchemaTestObject.TestBrowserRequest({}))
-        result = self._form.getWidgetForFieldId('data')
+        result = self._form.getWidgetForFieldName('data')
         self._compareWidgets(widget, result)
 
-        self.assertRaises(KeyError, self._form.getWidgetForFieldId, 'foo')
+        self.assertRaises(KeyError, self._form.getWidgetForFieldName, 'foo')
 
     
     def testRenderField(self):
-        field = SchemaTestObject.STestObject.getDescriptionFor('id')
+        field = SchemaTestObject.ITestObject.getDescriptionFor('id')
         self.assertEqual(
             '<input name="field_id" type="text" value="5" size="20"  />',
             self._form.renderField(field))
 
-        field = SchemaTestObject.STestObject.getDescriptionFor('creator')
+        field = SchemaTestObject.ITestObject.getDescriptionFor('creator')
         self.assertEqual('<input name="field_creator" type="text" '
                          'value="strichter@yahoo.com" size="30"  />',
                          self._form.renderField(field))
@@ -107,21 +107,19 @@
         data = self._form.getAllRawFieldData()
         result = {'data': StringIO('Data'), 'id': '1', 'title': 'Test New',
                   'creator': 'srichter@cbu.edu'}
-        for field in data.keys():
-            if field.id == 'data':
-                self.assertEqual(result[field.id].read(), data[field].read())
+        for name, value in data.iteritems():
+            if name == 'data':
+                self.assertEqual(result[name].read(), value.read())
             else:
-                self.assertEqual(result[field.id], data[field])
-
+                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 field in data.keys():
-            self.assertEqual(result[field.id], data[field])
-
+        for name, value in data.iteritems():
+            self.assertEqual(result[name], value)
 
     def testValidateAllFieldData(self):
         data = self._form.getAllRawFieldData()
@@ -134,20 +132,18 @@
         data = self._form.convertAllFieldData(data)
         self._form.storeAllDataInContext(data)
         obj = self._form.context
-        for field in data.keys():
-            self.assertEqual(data[field], getattr(obj, field.id))
+        for name, value in data.iteritems():
+            self.assertEqual(value, getattr(obj, name))
 
-
-    def testSaveValuesInContect(self):
+    def testSaveValuesInContext(self):
         data = self._form.getAllRawFieldData()
         data = self._form.convertAllFieldData(data)
         # The StrinIO must be reloaded.
         self.setUp()
         self._form.saveValuesInContext()
         obj = self._form.context
-        for field in data.keys():
-            self.assertEqual(data[field], getattr(obj, field.id))
-
+        for name, value in data.iteritems():
+            self.assertEqual(value, getattr(obj, name))
 
 def test_suite():
     return makeSuite(TestFormView)


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testListWidget.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testListWidget.py:1.3	Wed Jul 24 06:53:48 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testListWidget.py	Wed Sep  4 09:44:24 2002
@@ -21,9 +21,11 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
     items = [('foo', 'Foo'), ('bar', 'Bar')]
 
+    def getName(self):
+        return 'foo'
+    
     def get(self, name):
         return getattr(self, name)
 


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiCheckboxWidget.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiCheckboxWidget.py:1.3	Wed Jul 24 06:53:48 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiCheckboxWidget.py	Wed Sep  4 09:44:24 2002
@@ -21,9 +21,11 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
     items = [('foo1', 'Foo'), ('bar1', 'Bar')]
 
+    def getName(self):
+        return 'foo'
+    
     def get(self, name):
         return getattr(self, name)
 


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiListWidget.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiListWidget.py:1.3	Wed Jul 24 06:53:48 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testMultiListWidget.py	Wed Sep  4 09:44:24 2002
@@ -21,9 +21,11 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
     items = [('foo', 'Foo'), ('bar', 'Bar')]
 
+    def getName(self):
+        return 'foo'
+    
     def get(self, name):
         return getattr(self, name)
 


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testRadioWidget.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testRadioWidget.py:1.3	Wed Jul 24 06:53:48 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/tests/testRadioWidget.py	Wed Sep  4 09:44:24 2002
@@ -21,9 +21,11 @@
 
 class Field:
     """Field Stub """
-    id = 'foo'
     items = [('foo1', 'Foo'), ('bar1', 'Bar')]
 
+    def getName(self):
+        return 'foo'
+    
     def get(self, name):
         return getattr(self, name)