[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_query.py:1.5
Casey Duncan
casey at zope.com
Mon Apr 19 01:12:47 EDT 2004
Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv10739/tests
Modified Files:
test_query.py
Log Message:
Replace xproduct generator with CartProduct class which is lazy and will support later manipulation efficiently.
=== Packages/pypes/pypes/tests/test_query.py 1.4 => 1.5 ===
--- Packages/pypes/pypes/tests/test_query.py:1.4 Mon Apr 12 21:03:06 2004
+++ Packages/pypes/pypes/tests/test_query.py Mon Apr 19 01:12:46 2004
@@ -17,6 +17,7 @@
import unittest
from sets import Set
+from operator import mul
from zope.interface import implements
from zope.interface.verify import verifyObject
from persistent import Persistent
@@ -46,71 +47,43 @@
def __call__(self, *args, **kw):
return self._func(*args, **kw)
+class Foo(TestClass):
+ pass
-class TestXProduct(unittest.TestCase):
+class Bar(TestClass):
+ pass
- def setUp(self):
- self.foos = [TestClass(foo=i) for i in range(10)]
- self.bars = [TestClass(bar=chr(i+65)) for i in range(26)]
+class TestCartProduct(unittest.TestCase):
- def test_xproduct_one_input(self):
- from pypes.query import xproduct
- rows = [row.copy() for row in xproduct(foo=self.foos)]
- for foo, row in zip(self.foos, rows):
- self.assertEqual(len(row), 1)
- self.failUnless(row['foo'] is foo)
-
- def test_xproduct_one_self_named_input(self):
- from pypes.query import xproduct
- from UserList import UserList
- foos = UserList(self.foos)
- foos.__name__ = 'yahoozie'
- rows = [row.copy() for row in xproduct(foos)]
- for foo, row in zip(self.foos, rows):
- self.assertEqual(len(row), 1)
- self.failUnless(row['yahoozie'] is foo)
-
- def test_xproduct_unnamed_fails(self):
- from pypes.query import xproduct
- from pypes.exceptions import PypesQueryInputError
- rows = xproduct(self.foos)
- self.assertRaises(PypesQueryInputError, list, rows)
-
- def test_xproduct_with_dupe_names_fails(self):
- from pypes.query import xproduct
- from pypes.exceptions import PypesQueryInputError
- from UserList import UserList
- foos = UserList(self.foos)
- foos.__name__ = 'johndoe'
- bars = UserList(self.bars)
- bars.__name__ = 'johndoe'
- rows = xproduct(foos, bars)
- self.assertRaises(PypesQueryInputError, list, rows)
- rows = xproduct(bars, johndoe=foos)
- self.assertRaises(PypesQueryInputError, list, rows)
-
- def test_xproduct_multiple_inputs(self):
- from sets import Set
- from pypes.query import xproduct
- rows = [row.copy() for row in xproduct(foo=self.foos, bar=self.bars)]
- self.assertEqual(len(rows), len(self.foos) * len(self.bars))
- expected = [{'foo':foo, 'bar':bar}
- for foo in self.foos for bar in self.bars]
- self.assertEqual(sort(rows), sort(expected))
-
- def test_xproduct_multiple_self_named_inputs(self):
- from sets import Set
- from pypes.query import xproduct
- from UserList import UserList
- foos = UserList(self.foos)
- foos.__name__ = 'yup'
- bars = UserList(self.bars)
- bars.__name__ = 'nope'
- rows = [row.copy() for row in xproduct(foos, bars)]
- self.assertEqual(len(rows), len(self.foos) * len(self.bars))
- expected = [{'yup':foo, 'nope':bar}
- for foo in self.foos for bar in self.bars]
- self.assertEqual(sort(rows), sort(expected))
+ def setUp(self):
+ self.foos = [Foo(foo=i) for i in range(10)]
+ self.bars = [Bar(bar=chr(i+65)) for i in range(26)]
+ self.inputs = {'foo':self.foos, 'bar':self.bars}
+
+ def test_maxLen(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(self.inputs)
+ expected = reduce(mul, [len(ipt) for ipt in self.inputs.values()])
+ self.assertEqual(cp.maxLen(), expected)
+
+ def test_iter_one_no_criteria(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(self.inputs)
+ result = list(cp.iterProduct('foo'))
+ self.assertEqual(len(result), len(self.foos) * len(self.bars))
+ expected = Set(self.foos)
+ result = Set(result)
+ self.assertEqual(result, expected)
+
+ def test_iter_with_criteria(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(self.inputs,
+ lambda ipt: ipt['foo'].foo < 5 and ipt['bar'].bar in ('A', 'B'))
+ count = 0
+ for foo, bar in cp.iterProduct('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