[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_order.py:1.1 test_queryops.py:1.2

Casey Duncan casey at zope.com
Fri Aug 20 16:21:24 EDT 2004


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

Modified Files:
	test_queryops.py 
Added Files:
	test_order.py 
Log Message:
Initial sort implementation, not quite ready for primetime


=== Added File Packages/pypes/pypes/tests/test_order.py ===
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Pypes order tests

$Id: test_order.py,v 1.1 2004/08/20 20:21:24 caseman Exp $"""

import unittest
from sets import Set, ImmutableSet
from pypes.tests.common import sort
from pypes.expression import Asc, Desc
from pypes.order import Sort


class Test:
    
    def __repr__(self):
        return '<Test %s>' % str(self.__dict__)


def sortem(iterable, func, reverse=False):
    s = [(func(i), i) for i in iterable]
    s.sort()
    if reverse:
        s.reverse()
    return [i[1] for i in s]


class TestSort(unittest.TestCase):
    
    def setUp(self):
        self.set = Set()
        for i in range(10):
            ob = Test()
            ob.a = i % 8
            ob.b = chr((i % 20) + 65)
            ob.c = (50-i) // 7
            self.set.add(ob)
    
    def test_single_asc(self):
        # Test single ascending sort
        sorted = Sort(self.set, Asc('x.a'))
        self.assertEqual([ob.a for ob in sorted], 
                         [ob.a for ob in sortem(self.set, lambda x: x.a)])
        sorted = Sort(self.set, Asc('x.b'))
        self.assertEqual([ob.b for ob in sorted], 
                         [ob.b for ob in sortem(self.set, lambda x: x.b)])
        sorted = Sort(self.set, Asc('x.c'))
        self.assertEqual([ob.c for ob in sorted], 
                         [ob.c for ob in sortem(self.set, lambda x: x.c)])
    
    def test_empty_asc(self):
        # Test empty ascending sort
        sorted = Sort(Set(), Asc('x.a'))
        self.failIf(list(sorted))
    
    def test_single_desc(self):
        # Test single descending sort
        sorted = Sort(self.set, Desc('x.a'))
        self.assertEqual([ob.a for ob in sorted], 
                         [ob.a for ob in sortem(self.set, lambda x: x.a, True)])
        sorted = Sort(self.set, Desc('x.b'))
        self.assertEqual([ob.b for ob in sorted], 
                         [ob.b for ob in sortem(self.set, lambda x: x.b, True)])
        sorted = Sort(self.set, Desc('x.c'))
        self.assertEqual([ob.c for ob in sorted], 
                         [ob.c for ob in sortem(self.set, lambda x: x.c, True)])
    
    def test_sort_not_hashable(self):
        # Many of the sort strategies use dicts to optimize performance
        # for clustered keys. They should fall back to a non-dict
        # strategy if any of the values are not hashable, however
        nohash = ['f', ['b'], ['c', 'd'], ['t', 'y'], 't']
        sorted = Sort(nohash, Asc('x'))
        self.assertEqual(list(sorted), sortem(nohash, lambda x:x))
        import pdb; pdb.set_trace()
        sorted = Sort(nohash, Desc('len(x)'), Asc('x'))
        expected = sortem(sortem(nohash, lambda x: x), lambda x: len(x), True)
        self.assertEqual(list(sorted), sortem(nohash, lambda x:x))
    
    def test_empty_desc(self):
        # Test empty ascending sort
        sorted = Sort(Set(), Desc('x.a'))
        self.failIf(list(sorted))
    
    def test_multi_asc(self):
        # Test multiple ascending sort
        sorted = Sort(self.set, Asc('x.a'), Asc('x.b'),
                                Asc('x.c'))
        expected = sortem(self.set, lambda x: (x.a, x.b, x.c))
        self.assertEqual(list(sorted), expected)
        sorted = Sort(self.set, Asc('x.c'),
                                Asc('x.b'))
        expected = sortem(self.set, lambda x: (x.c, x.b))
        self.assertEqual(list(sorted), expected)
    
    def test_multi_desc(self):
        # Test multiple descending sort
        sorted = Sort(self.set, Desc('x.c'),
                                Desc('x.a'),
                                Desc('x.b'))
        expected = sortem(self.set, lambda x: (x.c, x.a, x.b), True)
        self.assertEqual(list(sorted), expected)
        sorted = Sort(self.set, Desc('x.b'),
                                Desc('x.a'))
        expected = sortem(self.set, lambda x: (x.b, x.a), True)
        self.assertEqual(list(sorted), expected)
    
    def test_multi_mixed(self):
        # Test mixed sort order
        sorted = Sort(self.set, Asc('x.b'),
                                Desc('x.c'))
        expected = sortem(sortem(self.set, lambda  x: x.c, True), lambda x: x.b)
        #import pdb; pdb.set_trace()
        self.assertEqual(list(sorted), expected)
        sorted = Sort(self.set, Desc('x.a'),
                                Asc('x.b'))
        expected = sortem(sortem(self.set, lambda  x: x.b), lambda x: x.a, True)
        self.assertEqual(list(sorted), expected)
        sorted = Sort(self.set, Desc('x.c'),
                                Asc('x.b'),
                                Asc('x.a'))
        expected = sortem(
            sortem(sortem(self.set, lambda  x: x.a), lambda x: x.b),
            lambda x: x.c, True)
        self.assertEqual(list(sorted), expected)
    
    def test_expr_multiple_names(self):
        # Cannot sort using expression with multiple free names
        from pypes.exceptions import SortError
        self.assertRaises(SortError, Sort, self.set, Asc('a * b'))
        self.assertRaises(SortError, Sort, Set(), Asc('a | b'))
        self.assertRaises(SortError, 
            Sort, self.set, Asc('a + b'), Desc('b - c'))
        self.assertRaises(SortError, Sort, Set(), Asc('a + b'), Desc('b(a)'))
    
    def test_expr_no_names(self):
        # Cannot sort using an expression no names either
        from pypes.exceptions import SortError
        self.assertRaises(SortError, Sort, self.set, Asc('42'))
        self.assertRaises(SortError, Sort, Set(), Asc('33'))
        self.assertRaises(SortError, Sort, self.set, Asc('b'), Desc('1'))
        self.assertRaises(SortError, Sort, Set(), Asc('1'), Desc('a'))
        
    def test_no_exprs(self):
        # Cannot sort without an expression
        from pypes.exceptions import SortError
        self.assertRaises(SortError, Sort, self.set)


if __name__ == '__main__':
    unittest.main()


=== Packages/pypes/pypes/tests/test_queryops.py 1.1 => 1.2 ===
--- Packages/pypes/pypes/tests/test_queryops.py:1.1	Thu Jul  1 23:18:56 2004
+++ Packages/pypes/pypes/tests/test_queryops.py	Fri Aug 20 16:21:24 2004
@@ -18,7 +18,8 @@
 import unittest
 from sets import Set
 from operator import mul
-from compiler import parse
+from compiler import parse, misc, ast
+from compiler.pycodegen import ExpressionCodeGenerator
 from zope.interface import implements
 from zope.interface.verify import verifyObject, verifyClass
 from persistent import Persistent
@@ -35,7 +36,7 @@
         if expr is not None:
             self._ast = parse(expr, mode='eval')
             if func is None:
-                self._func = lambda ns: eval(expr, {}, ns) 
+                self._func = lambda ns: eval(expr, {}, ns)
     
     def names(self):
         return self._names
@@ -45,6 +46,14 @@
     
     def freeNames(self, free_names=[]):
         return self._names
+    
+    def makeFunction(self, args=[]):
+        code = ('def f(%s): return _func({%s})' %
+                (','.join(args), 
+                ','.join(['"%s":%s' % (arg, arg) for arg in args])))
+        d = {'_func':self._func}
+        exec code in d
+        return d['f']
     
     def ast(self):
         return self._ast



More information about the Zope-CVS mailing list