[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_extent.py:1.6

Casey Duncan casey at zope.com
Sun Feb 15 23:44:58 EST 2004


Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv16072/tests

Modified Files:
	test_extent.py 
Log Message:
Implement Extent class.


=== Packages/pypes/pypes/tests/test_extent.py 1.5 => 1.6 ===
--- Packages/pypes/pypes/tests/test_extent.py:1.5	Wed Feb 11 00:46:42 2004
+++ Packages/pypes/pypes/tests/test_extent.py	Sun Feb 15 23:44:58 2004
@@ -19,9 +19,11 @@
 import types
 from sets import Set
 from zope.interface import Interface, implements
+from zope.interface.exceptions import DoesNotImplement
 from zope.interface.verify import verifyObject
 from pypes.interfaces import IExtentMap, IExtentService
 from pypes.tests.common import TestConnection, sort
+from BTrees.OOBTree import OOBTree, OOTreeSet
 
 class FooClass:
     pass
@@ -206,9 +208,9 @@
         del self._objs[ident]
         del obj._pypes_id_
 
-class TestExtent:
+class DummyExtent:
     
-    def __init__(self, key, keyset, setmap):
+    def __init__(self, keyset, setmap, key):
         self.key = key
         self._keys = keyset
         self._idsets = setmap
@@ -237,7 +239,7 @@
         self.identity = DummyIdentityService(self.conn)
         addService(self.conn, 'identity', self.identity)
         self.cemap = ClassExtentMap(self.conn)
-        self.cemap._extent_factory = TestExtent
+        self.cemap._extent_factory = DummyExtent
     
     def _makeObj(self, cls, *args, **kw):
         obj = cls(*args, **kw)
@@ -315,6 +317,189 @@
         for ex in extents:
             self.assertEqual(len(ex), 0)
              
+class TestExtent(unittest.TestCase):
+    
+    def setUp(self):
+        from pypes.extent import classKey
+        self.instances = {}
+        self.objs = []
+        for cls in [FooClass, BarClass, BazClass, SplatClass]:
+            key = classKey(cls)
+            self.instances[key] = Set()
+            for i in range(5):
+                obj = cls()
+                self.instances[key].add(obj)
+                self.objs.append(obj)
+                
+    def makeExtent(self, *classes):
+        from pypes.extent import classKey, Extent
+        class_keys = OOTreeSet([classKey(cls) for cls in classes])
+        objs = [ob for ob in self.objs if ob.__class__ in classes]
+        return Extent(class_keys, self.instances, classes[0]), objs
+    
+    def testBaseInterface(self):
+        from pypes.interfaces import IExtent
+        extent, nil = self.makeExtent(FooClass)
+        self.failUnless(verifyObject(IExtent, extent))
+        
+    def testCanonicalInterface(self):
+        from pypes.extent import Extent
+        from pypes.interfaces import ICanonicalExtent
+        extent = Extent(OOTreeSet(), self.instances, FooClass)
+        self.failUnless(verifyObject(ICanonicalExtent, extent))
+    
+    def testNotCanonicalInterface(self):
+        from pypes.extent import Extent
+        from pypes.interfaces import ICanonicalExtent, IExtent
+        extent = Extent(OOTreeSet(), self.instances)
+        self.assertRaises(
+            DoesNotImplement, verifyObject, ICanonicalExtent, extent)
+        self.failUnless(verifyObject(IExtent, extent))
+    
+    def testLen(self):
+        extent, objs = self.makeExtent(FooClass)
+        self.assertEqual(len(extent), len(objs))
+        extent, objs = self.makeExtent(BarClass, BazClass, SplatClass)
+        self.assertEqual(len(extent), len(objs))
+    
+    def testIter(self):
+        extent, objs = self.makeExtent(FooClass)
+        self.assertEqual(sort(list(extent)), sort(objs))
+        extent, objs = self.makeExtent(BarClass, BazClass, SplatClass)
+        self.assertEqual(sort(list(extent)), sort(objs))
+
+    def testContains(self):
+        extent, objs = self.makeExtent(FooClass)
+        for ob in objs:
+            self.failUnless(ob in extent)
+        extent, objs = self.makeExtent(BarClass, BazClass, SplatClass)
+        for ob in objs:
+            self.failUnless(ob in extent)
+    
+    def testUnionDisjointExtents(self):
+        foo_extent, foos = self.makeExtent(FooClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass)
+        union = foo_extent.union(bar_extent)
+        self.failIf(union is foo_extent)
+        self.failIf(union is bar_extent)
+        self.assertEqual(sort(list(union)), sort(foos + bars))
+    
+    def testUnionOverlappingExtents(self):
+        foo_extent, foos = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass, SplatClass)
+        union = foo_extent.union(bar_extent)
+        self.failIf(union is foo_extent)
+        self.failIf(union is bar_extent)
+        self.assertEqual(sort(list(union)), sort(self.objs))
+    
+    def testOrUnion(self):
+        foo_extent, foos = self.makeExtent(FooClass)
+        splat_extent, splats = self.makeExtent(SplatClass)
+        union = foo_extent | splat_extent
+        self.failIf(union is foo_extent)
+        self.failIf(union is splat_extent)
+        self.assertEqual(sort(list(union)), sort(foos + splats))
+    
+    def testDiffDisjointExtents(self):
+        foo_extent, foos = self.makeExtent(FooClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass)
+        diff = foo_extent.difference(bar_extent)
+        self.failIf(diff is foo_extent)
+        self.failIf(diff is bar_extent)
+        self.assertEqual(sort(list(diff)), sort(foos))
+    
+    def testDiffOverlappingExtents(self):
+        foo_extent, nil = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass, SplatClass)
+        diff = foo_extent.difference(bar_extent)
+        self.failIf(diff is foo_extent)
+        self.failIf(diff is bar_extent)
+        nil, foos = self.makeExtent(FooClass)
+        self.assertEqual(sort(list(diff)), sort(foos))
+        
+    def testDiffSameExtents(self):
+        foo_extent1, nil = self.makeExtent(FooClass, BarClass)
+        foo_extent2, nil = self.makeExtent(FooClass, BarClass)
+        diff = foo_extent1.difference(foo_extent2)
+        self.failIf(diff is foo_extent1)
+        self.failIf(diff is foo_extent2)
+        self.failIf(diff)
+        diff = foo_extent2.difference(foo_extent1)
+        self.failIf(diff is foo_extent1)
+        self.failIf(diff is foo_extent2)
+        self.failIf(diff)        
+        
+    def testMinusDiff(self):
+        foo_extent, nil = self.makeExtent(FooClass, BarClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass)
+        diff = foo_extent - bar_extent
+        self.failIf(diff is foo_extent)
+        self.failIf(diff is bar_extent)
+        nil, foos = self.makeExtent(FooClass)
+        self.assertEqual(sort(list(diff)), sort(foos))
+    
+    def testIntersectDisjointExtents(self):
+        foo_extent, foos = self.makeExtent(FooClass)
+        bar_extent, bars = self.makeExtent(BarClass, BazClass)
+        sect = foo_extent.intersection(bar_extent)
+        self.failIf(sect is foo_extent)
+        self.failIf(sect is bar_extent)
+        self.failIf(sect)
+    
+    def testIntersectOverlappingExtents(self):
+        foo_extent, foos = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, nil = self.makeExtent(BarClass, BazClass)
+        sect = foo_extent.intersection(bar_extent)
+        self.failIf(sect is foo_extent)
+        self.failIf(sect is bar_extent)
+        nil, bars = self.makeExtent(BarClass)
+        self.assertEqual(sort(list(sect)), sort(bars))
+    
+    def testAndIntersect(self):
+        foo_extent, foos = self.makeExtent(FooClass, SplatClass)
+        splat_extent, splats = self.makeExtent(SplatClass)
+        sect = foo_extent & splat_extent
+        self.failIf(sect is foo_extent)
+        self.failIf(sect is splat_extent)
+        self.assertEqual(sort(list(sect)), sort(splats))
+        
+    def testInvert(self):
+        foo_extent, nil = self.makeExtent(FooClass)
+        bar_extent, nil = self.makeExtent(BarClass, BazClass, SplatClass)
+        inverse = ~foo_extent
+        self.assertEqual(inverse, bar_extent)
+        self.failIf(inverse is foo_extent)
+        self.failIf(inverse is bar_extent)
+        self.assertEqual(sort(list(inverse)), sort(list(bar_extent)))
+        self.assertEqual(~inverse, foo_extent)
+    
+    def testIsSuperAndSubSet(self):
+        foo_extent, nil = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, nil = self.makeExtent(BarClass, FooClass)
+        self.failUnless(bar_extent.issubset(foo_extent))
+        self.failIf(bar_extent.issuperset(foo_extent))
+        self.failUnless(foo_extent.issuperset(bar_extent))
+        self.failIf(foo_extent.issubset(bar_extent))
+    
+    def testIsNotSuperAndSubset(self):
+        foo_extent, nil = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, nil = self.makeExtent(BarClass, BazClass)
+        self.failIf(bar_extent.issubset(foo_extent))
+        self.failIf(bar_extent.issuperset(foo_extent))
+        self.failIf(foo_extent.issuperset(bar_extent))
+        self.failIf(foo_extent.issubset(bar_extent))
+    
+    def testEquivilanceIsSuperAndSubset(self):
+        foo_extent, nil = self.makeExtent(FooClass, BarClass, SplatClass)
+        bar_extent, nil = self.makeExtent(SplatClass, BarClass, FooClass)
+        self.assertEqual(foo_extent, bar_extent)
+        self.failUnless(bar_extent.issubset(foo_extent))
+        self.failUnless(bar_extent.issuperset(foo_extent))
+        self.failUnless(foo_extent.issuperset(bar_extent))
+        self.failUnless(foo_extent.issubset(bar_extent))        
+        
+
+# XXX add tests for extent to idset operations
         
 if __name__ == '__main__':
     unittest.main()




More information about the Zope-CVS mailing list