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

Steve Alexander steve@cat-box.net
Fri, 10 Jan 2003 13:46:29 -0500


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

Modified Files:
	test_field.py 
Log Message:
Created a ComponentLocation field to specify a location by path or by
dotted module name.


=== Zope3/src/zope/app/services/tests/test_field.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/services/tests/test_field.py:1.3	Thu Jan  9 12:28:46 2003
+++ Zope3/src/zope/app/services/tests/test_field.py	Fri Jan 10 13:46:27 2003
@@ -21,7 +21,25 @@
 from zope.app.traversing import traverse
 from zope.schema.interfaces import ValidationError
 from zope.interface import Interface
-from zope.app.services.field import ComponentPath
+
+from zope.app.interfaces.services.service import INameResolver
+from zope.component.interfaces import IServiceService
+class NameResolver:
+    __implements__ = INameResolver, IServiceService
+    # I'm lying about implementing IServiceService, but that is needed to get
+    # a NameResolver as a service manager
+    def __init__(self, name=None, component=None):
+        self.lookup = {}
+        if name is not None:
+            self.lookup[name] = component
+    def resolve(self, dotted_name):
+        if self.lookup.has_key(dotted_name):
+            return self.lookup[dotted_name]
+        raise ImportError, dotted_name
+        
+    def getService(self, name):
+        from zope.component import getService
+        return getService(None, name)
 
 class I1(Interface):  pass
 
@@ -31,26 +49,92 @@
 class D:
     pass
 
-class Test(PlacefulSetup, TestCase):
+class TestComponentPath(PlacefulSetup, TestCase):
 
-    def test__validate(self):
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.createObjects()
+        self.createFields()
+
+    def createObjects(self):
         self.buildFolders()
         self.folder1.setObject('c', C())
         self.folder1.setObject('d', D())
 
-        folder2 = traverse(self.rootFolder, 'folder2')
+    def createFields(self):
+        from zope.app.services.field import ComponentPath
 
+        folder2 = traverse(self.rootFolder, 'folder2')
         field = ComponentPath(type=I1)
         field = field.bind(folder2)
+        self.field = field
 
+    def test__validate(self):
+        field = self.field
         field.validate(u'/folder1/c')
 
         self.assertRaises(ValidationError, field.validate, u'/folder1/d')
         self.assertRaises(ValidationError, field.validate, u'/folder1/e')
 
+class TestComponentLocation(TestComponentPath):
+
+    def createObjects(self):
+        TestComponentPath.createObjects(self)
+        self.resolver = NameResolver()
+        self.rootFolder.setServiceManager(self.resolver)
+
+    def createFields(self):
+        from zope.app.services.field import ComponentLocation
+
+        folder2 = traverse(self.rootFolder, 'folder2')
+        field = ComponentLocation(type=I1)
+        field = field.bind(folder2)
+        self.field = field
+        notypefield = ComponentLocation(type=None)
+        notypefield = notypefield.bind(folder2)
+        self.notypefield = notypefield
+
+    def test__validateDottedName(self):
+        field = self.notypefield
+        dotted_name = u'zope.app.whatever.ClassName'
+        some_class = self.__class__
+        self.resolver.lookup[dotted_name] = some_class
+        field._validate(dotted_name)
+        self.assertRaises(ValidationError, field._validate, u'foo.bar.baz')
+
+class TestLocateComponent(PlacefulSetup, TestCase):
+
+    def test_locateComponent(self):
+        from zope.app.services.field import locateComponent
+
+        self.buildFolders()
+        self.folder1.setObject('c', C())
+        self.folder1.setObject('d', D())
+
+        folder2 = traverse(self.rootFolder, 'folder2')
+
+        self.assertEqual(locateComponent(u'/folder1/c', folder2, I1),
+                         traverse(self.rootFolder, '/folder1/c')
+                         )
+        self.assertRaises(ValidationError,
+                          locateComponent, u'/folder1/d', folder2, I1)
+        self.assertRaises(ValidationError,
+                          locateComponent, u'/folder1/e', folder2)
+
+        dotted_name = 'zope.app.whatever.ClassName'
+        some_class = self.__class__
+        resolver = NameResolver(dotted_name, some_class)
+        self.rootFolder.setServiceManager(resolver)
+        self.assertEqual(locateComponent(dotted_name, folder2),
+                         some_class
+                         )
+        self.assertRaises(ValidationError, locateComponent, 'f.b.b', folder2)
+
 def test_suite():
     return TestSuite((
-        makeSuite(Test),
+        makeSuite(TestComponentPath),
+        makeSuite(TestComponentLocation),
+        makeSuite(TestLocateComponent),
         ))
 
 if __name__=='__main__':