[Zope3-checkins] CVS: Zope3/src/zope/app/browser/services/tests - test_field_widget.py:1.4

Steve Alexander steve@cat-box.net
Sun, 12 Jan 2003 16:22:19 -0500


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

Modified Files:
	test_field_widget.py 
Log Message:
added component location field and widget, for specifying a component
either by a dotted module name, or by a path.


=== Zope3/src/zope/app/browser/services/tests/test_field_widget.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/browser/services/tests/test_field_widget.py:1.3	Thu Jan  9 12:28:42 2003
+++ Zope3/src/zope/app/browser/services/tests/test_field_widget.py	Sun Jan 12 16:22:16 2003
@@ -22,7 +22,6 @@
 from zope.interface import Interface
 from zope.app.services.service import ServiceManager
 from zope.publisher.browser import TestRequest
-from zope.app.browser.services.field import ComponentPathWidget
 
 class FakeComponentPath:
 
@@ -32,9 +31,12 @@
         self.context = context
         self.type = type
 
-    def validate(self, value): pass
+    def validate(self, value):
+        pass
 
     __name__ = 'X'
+    title = 'fake component field'
+    required = True
 
 class I1(Interface):  pass
 
@@ -46,9 +48,16 @@
 class D:
     __implements__ = I2
 
-class Test(PlacefulSetup, TestCase):
+instanceOfComponentC = C()
 
-    def test(self):
+class BaseTest(PlacefulSetup, TestCase):
+
+    def createWidget(self, field, request):
+        from zope.app.browser.services.field import ComponentPathWidget
+        return ComponentPathWidget(field, request)
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
         self.buildFolders()
         self.rootFolder.setServiceManager(ServiceManager())
         default = traverse(self.rootFolder, '++etc++Services/Packages/default')
@@ -59,46 +68,135 @@
         default.setObject('d2', D())
         default.setObject('d3', D())
 
-        request = TestRequest()
+        self.request = TestRequest()
+        self.defaultpackage = default
+
 
-        fake = FakeComponentPath(default, I1)
-        widget = ComponentPathWidget(fake, request)
+class TestComponentPathWidget(BaseTest):
+
+    def test(self):
+        fake = FakeComponentPath(self.defaultpackage, I1)
+        widget = self.createWidget(fake, self.request)
 
         expected = (
-            '<select name="field.X">\n'
-            '<option></option>\n'
-            '<option>/++etc++Services/Packages/default/c1</option>\n'
-            '<option>/++etc++Services/Packages/default/c2</option>\n'
-            '<option>/++etc++Services/Packages/default/c3</option>\n'
+            '<select name="field.X">'
+            '<option></option>'
+            '<option>/++etc++Services/Packages/default/c1</option>'
+            '<option>/++etc++Services/Packages/default/c2</option>'
+            '<option>/++etc++Services/Packages/default/c3</option>'
             '</select>'
             )
 
         self.assertEqual(widget(), expected)
 
-        request.form['field.X'] = u'/++etc++Services/Packages/default/c2'
+        self.request.form['field.X'] = u'/++etc++Services/Packages/default/c2'
 
         expected = (
-            '<select name="field.X">\n'
-            '<option></option>\n'
-            '<option>/++etc++Services/Packages/default/c1</option>\n'
-            '<option selected>/++etc++Services/Packages/default/c2</option>\n'
-            '<option>/++etc++Services/Packages/default/c3</option>\n'
+            '<select name="field.X">'
+            '<option></option>'
+            '<option>/++etc++Services/Packages/default/c1</option>'
+            '<option selected>/++etc++Services/Packages/default/c2</option>'
+            '<option>/++etc++Services/Packages/default/c3</option>'
             '</select>'
             )
 
         self.assertEqual(widget(), expected)
 
     def test_convert(self):
-        request = TestRequest()
         fake = FakeComponentPath(None, I1)
-        widget = ComponentPathWidget(fake, request)
+        widget = self.createWidget(fake, self.request)
         self.assertEqual(widget._convert(u''), None)
         self.assertEqual(widget._convert(u'/a'), u'/a')
 
+class TestComponentLocationWidget(BaseTest):
+
+    def createWidget(self, field, request):
+        from zope.app.browser.services.field import ComponentLocationWidget
+        return ComponentLocationWidget(field, request)
+
+    def test(self):
+        fake = FakeComponentPath(self.defaultpackage, I1)
+        widget = self.createWidget(fake, self.request)
+
+        expected = (
+            'path: '
+            '<select name="field.X.p">'
+            '<option></option>'
+            '<option>/++etc++Services/Packages/default/c1</option>'
+            '<option>/++etc++Services/Packages/default/c2</option>'
+            '<option>/++etc++Services/Packages/default/c3</option>'
+            '</select>'
+            '<br>'
+            'dotted name: '
+            '<input type="text" name="field.X.d" value="">'
+            )
+
+        self.assertEqual(widget(), expected)
+        self.failIf(widget.haveData())
+        self.assertEqual(widget.hidden(), '')
+
+        self.request.form['field.X.p'] = (
+                u'/++etc++Services/Packages/default/c2')
+
+        expected = (
+            'path: '
+            '<select name="field.X.p">'
+            '<option></option>'
+            '<option>/++etc++Services/Packages/default/c1</option>'
+            '<option selected>/++etc++Services/Packages/default/c2</option>'
+            '<option>/++etc++Services/Packages/default/c3</option>'
+            '</select>'
+            '<br>'
+            'dotted name: '
+            '<input type="text" name="field.X.d" value="">'
+            )
+
+        self.assertEqual(widget(), expected)
+        self.failUnless(widget.haveData())
+        self.assertEqual(widget.hidden(),
+                         '<input type="hidden" name="field.X.p" value="'
+                         '/++etc++Services/Packages/default/c2'
+                         '" />'
+                         )
+
+        self.request.form['field.X.d'] = (
+                u'zope.app.browser.services.tests.test_field_widget'
+                u'.instanceOfComponentC')
+        self.failIf(widget.haveData())
+
+        from zope.app.interfaces.form import WidgetInputError
+        self.assertRaises(WidgetInputError, widget.hidden)
+
+        del self.request.form['field.X.p']
+        self.failUnless(widget.haveData())
+
+        expected = (
+            'path: '
+            '<select name="field.X.p">'
+            '<option></option>'
+            '<option>/++etc++Services/Packages/default/c1</option>'
+            '<option>/++etc++Services/Packages/default/c2</option>'
+            '<option>/++etc++Services/Packages/default/c3</option>'
+            '</select>'
+            '<br>'
+            'dotted name: '
+            '<input type="text" name="field.X.d" value="'
+            'zope.app.browser.services.tests.test_field_widget'
+            '.instanceOfComponentC'
+            '">'
+            )
+        self.assertEqual(widget(), expected)
+        self.assertEqual(widget.hidden(),
+                         '<input type="hidden" name="field.X.d" value="'
+                         'zope.app.browser.services.tests.test_field_widget'
+                         '.instanceOfComponentC'
+                         '" />'
+                         )
 
 def test_suite():
     return TestSuite((
-        makeSuite(Test),
+        makeSuite(TestComponentPathWidget),
+        makeSuite(TestComponentLocationWidget),
         ))
 
 if __name__=='__main__':