[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_query.py:1.7

Casey Duncan casey at zope.com
Tue Apr 27 23:18:13 EDT 2004


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

Modified Files:
	test_query.py 
Log Message:
implement CartProduct union() and intersection()


=== Packages/pypes/pypes/tests/test_query.py 1.6 => 1.7 ===
--- Packages/pypes/pypes/tests/test_query.py:1.6	Wed Apr 21 01:32:09 2004
+++ Packages/pypes/pypes/tests/test_query.py	Tue Apr 27 23:18:12 2004
@@ -46,6 +46,22 @@
     
     def __call__(self, *args, **kw):
         return self._func(*args, **kw)
+        
+    def __eq__(self, other):
+        return (self._func.func_code == other._func.func_code
+            and self._names == other.__names)
+    
+    def __or__(self, other):
+        assert Set(self._names) == Set(other._names)
+        def func(*args, **kw):
+            return self._func(*args, **kw) or other._func(*args, **kw)
+        return DummyExpr(func, self._names)
+    
+    def __and__(self, other):
+        assert Set(self._names) == Set(other._names)
+        def func(*args, **kw):
+            return self._func(*args, **kw) and other._func(*args, **kw)
+        return DummyExpr(func, self._names)
 
 class Foo(TestClass):
     pass
@@ -64,6 +80,19 @@
         self.assertEqual(union['jerry'], map1['jerry'].union(map2['jerry']))
         self.assertEqual(union['george'], map1['george'].union(map2['george']))
         self.failIf(union is map1 or union is map2)
+        
+    def test_union_input_map_to_self(self):
+        from pypes.query import union_input_maps
+        inmap = {'pee wee': Set(('loner', 'rebel'))}
+        union = union_input_maps(inmap, inmap)
+        self.assertEqual(inmap, union)
+        self.failIf(inmap is union)
+    
+    def test_union_input_maps_fails_with_different_inputs(self):
+        from pypes.query import union_input_maps
+        map1 = {'larry':Set(), 'moe':Set(), 'curly':Set()}
+        map2 = {'larry':Set(), 'moe':Set(), 'shemp':Set()}
+        self.assertRaises(AssertionError, union_input_maps, map1, map2)
     
 class TestCartProduct(unittest.TestCase):
     
@@ -87,7 +116,7 @@
         self.assertEqual(self.cp.criteriaExpr(), None)
         criteria = DummyExpr(None)
         cp = CartProduct(self.inputs, criteria)
-        self.assertEqual(cp.criteriaExpr(), criteria)
+        self.failUnless(cp.criteriaExpr() is criteria)
         
     def test_names_used(self):
         from pypes.query import CartProduct
@@ -121,6 +150,37 @@
             self.failUnless(foo.foo < 5 and bar.bar in ('A', 'B'))
             count += 1
         self.assertEqual(count, len(self.foos))
+    
+    def test_union_cp_with_cp(self):
+        from pypes.query import CartProduct
+        cp1 = CartProduct(
+            self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo == 3))
+        cp2 = CartProduct(
+            self.inputs, DummyExpr(lambda ipt: ipt['bar'].bar in ('A', 'B')))
+        union = cp1.union(cp2)
+        self.failIf(union is cp1)
+        self.failIf(union is cp2)
+        count = 0
+        for foo, bar in union.iterResult('foo', 'bar'):
+            self.failUnless(foo.foo < 5 or bar.bar in ('A', 'B'))
+            count += 1
+        self.assertEqual(count, 44)
+    
+    def test_intersect_cp_with_cp(self):
+        from pypes.query import CartProduct
+        cp1 = CartProduct(
+            self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo < 5))
+        cp2 = CartProduct(
+            self.inputs, DummyExpr(lambda ipt: ipt['bar'].bar in ('A', 'B')))
+        sect = cp1.intersection(cp2)
+        self.failIf(sect is cp1)
+        self.failIf(sect is cp2)
+        count = 0
+        for foo, bar in sect.iterResult('foo', 'bar'):
+            self.failUnless(foo.foo < 5 and bar.bar in ('A', 'B'))
+            count += 1
+        self.assertEqual(count, len(self.foos))
+
 
 class TestSort(unittest.TestCase):
     




More information about the Zope-CVS mailing list