Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

root/trunk/geniusql/test/test_genexp.py

Revision 311 (checked in by lakin, 7 months ago)

further fixes to the whitespace.

  • Property svn:eol-style set to native
Line 
1 import datetime
2 import unittest
3
4 import geniusql
5 from geniusql import Query
6
7
8 db = geniusql.db("sqlite", **{'name': ':memory:'})
9 db.create()
10 schema = db.schema()
11 schema.create()
12
13 Person = schema.table('Person')
14 Person['ID'] = schema.column(int, autoincrement=True, key=True)
15 Person['Name'] = schema.column(hints={'bytes': 100})
16 schema['Person'] = Person
17
18 Torture = schema.table('Torture')
19 Torture['ID'] = schema.column(int, autoincrement=True, key=True)
20 Torture['Name'] = schema.column(hints={'bytes': 100})
21 schema['Torture'] = Torture
22
23
24
25 amount = 5
26 relation = [schema['Person'], schema['Torture']]
27 a = [1,2,3,4,5]
28 f = [(([t1.a, t1.b + t2.a] for t1, t2 in relation if t1.a > 13),
29       Query(relation, lambda t1, t2: [t1.a, t1.b + t2.a],
30             lambda t1, t2: t1.a > 13)),
31      (([x.Name] for x in schema['Person'] if x.Name in a),
32       Query(schema['Person'], lambda x: [x.Name], lambda x: x.Name in a)),
33 ##     (lambda x: x + datetime.date(2004, 1, 1), astwalk.AST(
34 ##          Add((Name('x'), Const(datetime.date(2004, 1, 1)))),
35 ##          ['x'])),
36 ##     (lambda x, **kw: (x.Date == datetime.date(2004, 1, 1)
37 ##                       and x.Qty < kw['Size']), astwalk.AST(
38 ##          And((Compare(Getattr(Name('x'), 'Date'),
39 ##                       [('==', Const(datetime.date(2004, 1, 1)))]),
40 ##               Compare(Getattr(Name('x'), 'Qty'),
41 ##                       [('<', Subscript(Name('kw'), 'OP_APPLY',
42 ##                                        [Const('Size')]))]))),
43 ##          ['x'], dstar_args='kw')),
44 ##      # Mix names from globals, locals, attrs
45 ##     (lambda x, amount: (4 != x.amount) or (amount * 3 > 20), astwalk.AST(
46 ##          Or((Compare(Const(4), [('!=', Getattr(Name('x'), 'amount'))]),
47 ##              Compare(Mul((Name('amount'), Const(3))), [('>', Const(20))]))),
48 ##          ['x', 'amount'])),
49 ##     (lambda x: 3 * 4 * 5 * x, astwalk.AST(
50 ##          Mul((Const(60), Name('x'))),
51 ##          ['x'])),
52 ##     (lambda x: a[2:4] == -x['offset'], astwalk.AST(
53 ##          Compare(Const([3, 4]),
54 ##                  [('==', UnarySub(Subscript(Name('x'), 'OP_APPLY',
55 ##                                             [Const('offset')])))]),
56 ##          ['x'])),
57 ##     (lambda x: amount == 5 or amount == x.Qty, astwalk.AST(
58 ##          Or((Const(True),
59 ##              Compare(Const(5), [('==', Getattr(Name('x'), 'Qty'))]))),
60 ##          ['x'])),
61 ##     (lambda x: not (x.a == 3 and (x.b > 1 or x.b < -10)), astwalk.AST(
62 ##          Not(And((Compare(Getattr(Name('x'), 'a'),
63 ##                           [('==', Const(3))]),
64 ##                   Or((Compare(Getattr(Name('x'), 'b'),
65 ##                               [('>', Const(1))]),
66 ##                       Compare(Getattr(Name('x'), 'b'),
67 ##                               [('<', Const(-10))])))))),
68 ##          ['x'])),
69 ##     # Unicode const
70 ##     (lambda x: x.Name == u'Dimsdale', astwalk.AST(
71 ##          Compare(Getattr(Name('x'), 'Name'), [('==', Const(u'Dimsdale'))]),
72 ##          ['x'])),
73 ##     # getattr
74 ##     (lambda x: getattr(x, 'Name') == u'Dimsdale', astwalk.AST(
75 ##          Compare(Getattr(Name('x'), 'Name'), [('==', Const(u'Dimsdale'))]),
76 ##          ['x'])),
77 ##     # multiple args
78 ##     (lambda x, y, z, **kw: x.Qty > 1 and y.Qty > 20 and z.Type == 'A', astwalk.AST(
79 ##          And((Compare(Getattr(Name('x'), 'Qty'),
80 ##                       [('>', Const(1))]),
81 ##               And((Compare(Getattr(Name('y'), 'Qty'),
82 ##                            [('>', Const(20))]),
83 ##                    Compare(Getattr(Name('z'), 'Type'),
84 ##                            [('==', Const('A'))]))))),
85 ##          ['x', 'y', 'z'], dstar_args='kw')),
86      ]
87
88
89 class GenExpTests(unittest.TestCase):
90
91     def test_GenExp(self):
92         for genexp, expected in f:
93             q = Query.from_genexp(genexp)
94 ##            lp.verbose = True
95             self.assertEqual(q.relation, expected.relation)
96             self.assertEqual(q.attributes, expected.attributes)
97             self.assertEqual(q.restriction, expected.restriction)
98
99
100 if __name__ == "__main__":
101     unittest.main(__name__)
102
Note: See TracBrowser for help on using the browser.