[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_query.py:1.15
Casey Duncan
casey at zope.com
Thu Jun 10 00:44:25 EDT 2004
Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv7303/tests
Modified Files:
test_query.py
Log Message:
Define and implement resultSet() method for IPartialResult classes
=== Packages/pypes/pypes/tests/test_query.py 1.14 => 1.15 ===
--- Packages/pypes/pypes/tests/test_query.py:1.14 Mon May 31 23:05:50 2004
+++ Packages/pypes/pypes/tests/test_query.py Thu Jun 10 00:44:24 2004
@@ -134,8 +134,8 @@
def setUp(self):
from pypes.query import CartProduct
- self.foos = [Foo(foo=i) for i in range(10)]
- self.bars = [Bar(bar=chr(i+65)) for i in range(26)]
+ self.foos = Set([Foo(foo=i) for i in range(10)])
+ self.bars = Set([Bar(bar=chr(i+65)) for i in range(26)])
self.inputs = {'foo':self.foos, 'bar':self.bars}
self.cp = CartProduct(self.inputs)
@@ -224,7 +224,7 @@
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'))
+ self.failUnless(foo.foo == 3 or bar.bar in ('A', 'B'))
count += 1
self.assertEqual(count, 44)
@@ -242,7 +242,49 @@
self.failUnless(foo.foo < 5 and bar.bar in ('A', 'B'))
count += 1
self.assertEqual(count, len(self.foos))
-
+
+ def test_result_set_no_criteria(self):
+ self.assertEqual(self.cp.resultSet('foo'), self.inputs['foo'])
+ self.assertEqual(self.cp.resultSet('bar'), self.inputs['bar'])
+
+ def test_result_set_criteria_tests_one_input(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(
+ self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo == 3,
+ names=['foo']))
+ self.assertEqual(list(cp.resultSet('foo')),
+ [foo for foo in self.inputs['foo'] if foo.foo == 3])
+ self.assertEqual(cp.resultSet('bar'), self.inputs['bar'])
+ cp = CartProduct(
+ self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo == 99,
+ names=['foo']))
+ self.failIf(cp.resultSet('foo'))
+ self.failIf(cp.resultSet('bar'))
+
+ def test_result_set_criteria_tests_all_inputs(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(
+ self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo > 5
+ and ipt['bar'].bar in ('B', 'Y'),
+ names=['foo', 'bar']))
+ expected = Set([foo for foo in self.inputs['foo'] if foo.foo > 5])
+ self.assertEqual(cp.resultSet('foo'), expected)
+ expected = Set([bar for bar in self.inputs['bar']
+ if bar.bar in ('B','Y')])
+ self.assertEqual(cp.resultSet('bar'), expected)
+
+ def test_result_set_criteria_matches_all(self):
+ from pypes.query import CartProduct
+ cp = CartProduct(
+ self.inputs, DummyExpr(lambda ipt: ipt['foo'].foo < 50
+ and ipt['bar'].bar,
+ names=['foo', 'bar']))
+ self.assertEqual(cp.resultSet('foo'), self.inputs['foo'])
+ self.assertEqual(cp.resultSet('bar'), self.inputs['bar'])
+
+ def test_result_set_bogus_input(self):
+ from pypes.exceptions import PypesQueryError
+ self.assertRaises(PypesQueryError, self.cp.resultSet, 'cartouche')
class TestSort(unittest.TestCase):
@@ -306,8 +348,7 @@
right = ['bc', ['s','a'], ['yo']]
joined = list(equijoin(left, lambda x: x, right, lambda x: x))
self.assertEqual(
- sort(joined), sort([('bc','bc'), (['s','a'],['s','a'])]))
-
+ sort(joined), sort([('bc','bc'), (['s','a'],['s','a'])]))
def test_equijoin_empty(self):
from pypes.query import equijoin
joined = equijoin([], lambda x:x, [], lambda x:x)
@@ -583,7 +624,37 @@
for c in self.inputs['c']
if a[-1] == b or a[:2] == c])
self.assertEqual(sort(union.iterResult('a', 'b', 'c')), expected)
-
+
+ def test_left_result_set(self):
+ from pypes.query import Join
+ j = Join(self.inputs, DummyExpr(expr='a[-1] == b', names=['a', 'b']))
+ expected = Set([a for a in self.inputs['a']
+ for b in self.inputs['b']
+ if a[-1] == b])
+ self.assertEqual(j.resultSet('a'), expected)
+
+ def test_right_result_set(self):
+ from pypes.query import Join
+ j = Join(self.inputs, DummyExpr(expr='a[-1] == b', names=['a', 'b']))
+ expected = Set([b for a in self.inputs['a']
+ for b in self.inputs['b']
+ if a[-1] == b])
+ self.assertEqual(j.resultSet('b'), expected)
+
+ def test_result_set_not_in_criteria(self):
+ from pypes.query import Join
+ j = Join(self.inputs, DummyExpr(expr='a[-1] == b', names=['a', 'b']))
+ self.assertEqual(j.resultSet('c'), self.inputs['c'])
+
+ def test_empty_result_set(self):
+ self.failIf(self.join.resultSet('a'))
+ self.failIf(self.join.resultSet('b'))
+ self.failIf(self.join.resultSet('c'))
+
+ def test_result_set_bogus_input(self):
+ from pypes.exceptions import PypesQueryError
+ self.assertRaises(PypesQueryError, self.join.resultSet, 'cartouche')
+
class TestActualizedResult(unittest.TestCase):
@@ -631,7 +702,7 @@
from pypes.query import ActualizedResult
inputset = Set((1,2,3,4))
ar = ActualizedResult({'x':inputset})
- self.assertEqual(Set(ar.iterResult('x')), inputset)
+ self.assertEqual(Set(ar.iterResult('x')), inputset)
def test_select_one_input(self):
expected = Set([a for a in self.inputs['a'] if a%20 == 0])
@@ -647,6 +718,15 @@
expected = sort([(b, a) for a, b in expected])
self.assertEqual(sort(self.result.iterResult('b', 'a')), expected)
+ def test_empty_result(self):
+ from pypes.query import ActualizedResult
+ ar = ActualizedResult(
+ self.inputs, DummyExpr(expr="a > 100", names=['a']))
+ self.failIf(list(ar.iterResult('a', 'b')))
+ self.failIf(list(ar.iterResult('b', 'a')))
+ self.failIf(list(ar.iterResult('a')))
+ self.failIf(list(ar.iterResult('b')))
+
def test_select_fixed_one_input(self):
self.assertEqual(list(self.result.iterResult('a', a=40)), [40])
self.assertEqual(
@@ -670,7 +750,21 @@
from pypes.exceptions import PypesQueryError
self.assertRaises(
PypesQueryError, self.result.iterResult('a', 'b', uncle='bob').next)
-
+
+ def test_result_set_input_in_criteria(self):
+ self.assertEqual(self.result.resultSet('a'), Set((20, 40, 60)))
+ self.assertEqual(self.result.resultSet('b'), Set('NOPQRSTUVWXYZ'))
+
+ def test_empty_result_set(self):
+ from pypes.query import ActualizedResult
+ ar = ActualizedResult(
+ self.inputs, DummyExpr(expr="a > 100", names=['a']))
+ self.failIf(ar.resultSet('a'))
+ self.failIf(ar.resultSet('b'))
+
+ def test_result_set_bogus_input(self):
+ from pypes.exceptions import PypesQueryError
+ self.assertRaises(PypesQueryError, self.result.resultSet, 'cleo')
if __name__ == '__main__':
unittest.main()
More information about the Zope-CVS
mailing list