import datetime import unittest import geniusql from geniusql import Query db = geniusql.db("sqlite", **{'name': ':memory:'}) db.create() schema = db.schema() schema.create() Person = schema.table('Person') Person['ID'] = schema.column(int, autoincrement=True, key=True) Person['Name'] = schema.column(hints={'bytes': 100}) schema['Person'] = Person Torture = schema.table('Torture') Torture['ID'] = schema.column(int, autoincrement=True, key=True) Torture['Name'] = schema.column(hints={'bytes': 100}) schema['Torture'] = Torture amount = 5 relation = [schema['Person'], schema['Torture']] a = [1,2,3,4,5] f = [(([t1.a, t1.b + t2.a] for t1, t2 in relation if t1.a > 13), Query(relation, lambda t1, t2: [t1.a, t1.b + t2.a], lambda t1, t2: t1.a > 13)), (([x.Name] for x in schema['Person'] if x.Name in a), Query(schema['Person'], lambda x: [x.Name], lambda x: x.Name in a)), ## (lambda x: x + datetime.date(2004, 1, 1), astwalk.AST( ## Add((Name('x'), Const(datetime.date(2004, 1, 1)))), ## ['x'])), ## (lambda x, **kw: (x.Date == datetime.date(2004, 1, 1) ## and x.Qty < kw['Size']), astwalk.AST( ## And((Compare(Getattr(Name('x'), 'Date'), ## [('==', Const(datetime.date(2004, 1, 1)))]), ## Compare(Getattr(Name('x'), 'Qty'), ## [('<', Subscript(Name('kw'), 'OP_APPLY', ## [Const('Size')]))]))), ## ['x'], dstar_args='kw')), ## # Mix names from globals, locals, attrs ## (lambda x, amount: (4 != x.amount) or (amount * 3 > 20), astwalk.AST( ## Or((Compare(Const(4), [('!=', Getattr(Name('x'), 'amount'))]), ## Compare(Mul((Name('amount'), Const(3))), [('>', Const(20))]))), ## ['x', 'amount'])), ## (lambda x: 3 * 4 * 5 * x, astwalk.AST( ## Mul((Const(60), Name('x'))), ## ['x'])), ## (lambda x: a[2:4] == -x['offset'], astwalk.AST( ## Compare(Const([3, 4]), ## [('==', UnarySub(Subscript(Name('x'), 'OP_APPLY', ## [Const('offset')])))]), ## ['x'])), ## (lambda x: amount == 5 or amount == x.Qty, astwalk.AST( ## Or((Const(True), ## Compare(Const(5), [('==', Getattr(Name('x'), 'Qty'))]))), ## ['x'])), ## (lambda x: not (x.a == 3 and (x.b > 1 or x.b < -10)), astwalk.AST( ## Not(And((Compare(Getattr(Name('x'), 'a'), ## [('==', Const(3))]), ## Or((Compare(Getattr(Name('x'), 'b'), ## [('>', Const(1))]), ## Compare(Getattr(Name('x'), 'b'), ## [('<', Const(-10))])))))), ## ['x'])), ## # Unicode const ## (lambda x: x.Name == u'Dimsdale', astwalk.AST( ## Compare(Getattr(Name('x'), 'Name'), [('==', Const(u'Dimsdale'))]), ## ['x'])), ## # getattr ## (lambda x: getattr(x, 'Name') == u'Dimsdale', astwalk.AST( ## Compare(Getattr(Name('x'), 'Name'), [('==', Const(u'Dimsdale'))]), ## ['x'])), ## # multiple args ## (lambda x, y, z, **kw: x.Qty > 1 and y.Qty > 20 and z.Type == 'A', astwalk.AST( ## And((Compare(Getattr(Name('x'), 'Qty'), ## [('>', Const(1))]), ## And((Compare(Getattr(Name('y'), 'Qty'), ## [('>', Const(20))]), ## Compare(Getattr(Name('z'), 'Type'), ## [('==', Const('A'))]))))), ## ['x', 'y', 'z'], dstar_args='kw')), ] class GenExpTests(unittest.TestCase): def test_GenExp(self): for genexp, expected in f: q = Query.from_genexp(genexp) ## lp.verbose = True self.assertEqual(q.relation, expected.relation) self.assertEqual(q.attributes, expected.attributes) self.assertEqual(q.restriction, expected.restriction) if __name__ == "__main__": unittest.main(__name__)