~jkakar/storm/fluidinfo-trunk

129 by Gustavo Niemeyer
Added LGPL version 2.1 in the LICENSE file, and a standard
1
#
2
# Copyright (c) 2006, 2007 Canonical
3
#
4
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
5
#
6
# This file is part of Storm Object Relational Mapper.
7
#
8
# Storm is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License as
10
# published by the Free Software Foundation; either version 2.1 of
11
# the License, or (at your option) any later version.
12
#
13
# Storm is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU Lesser General Public License for more details.
17
#
18
# You should have received a copy of the GNU Lesser General Public License
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
#
144.2.1 by jdahlin at com
Add NumericVariable and Numeric property mapping to python's Decimal type
21
from decimal import Decimal
22
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
23
from tests.helper import TestHelper
24
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
25
from storm.variables import *
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
26
from storm.expr import *
27
28
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
29
class Func1(NamedFunc):
30
    name = "func1"
31
32
class Func2(NamedFunc):
33
    name = "func2"
34
35
# Create columnN, tableN, and elemN variables.
36
for i in range(10):
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
37
    for name in ["column", "elem"]:
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
38
        exec "%s%d = SQLToken('%s%d')" % (name, i, name, i)
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
39
    for name in ["table"]:
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
40
        exec "%s%d = '%s %d'" % (name, i, name, i)
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
41
42
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
43
class TrackContext(FromExpr):
44
    context = None
45
46
@compile.when(TrackContext)
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
47
def compile_track_context(compile, expr, state):
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
48
    expr.context = state.context
49
    return ""
50
51
def track_contexts(n):
52
    return [TrackContext() for i in range(n)]
53
54
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
55
class ExprTest(TestHelper):
56
57
    def test_select_default(self):
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
58
        expr = Select(())
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
59
        self.assertEquals(expr.columns, ())
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
60
        self.assertEquals(expr.where, Undef)
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
61
        self.assertEquals(expr.tables, Undef)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
62
        self.assertEquals(expr.default_tables, Undef)
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
63
        self.assertEquals(expr.order_by, Undef)
64
        self.assertEquals(expr.group_by, Undef)
65
        self.assertEquals(expr.limit, Undef)
66
        self.assertEquals(expr.offset, Undef)
67
        self.assertEquals(expr.distinct, False)
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
68
69
    def test_select_constructor(self):
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
70
        objects = [object() for i in range(9)]
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
71
        expr = Select(*objects)
72
        self.assertEquals(expr.columns, objects[0])
73
        self.assertEquals(expr.where, objects[1])
74
        self.assertEquals(expr.tables, objects[2])
75
        self.assertEquals(expr.default_tables, objects[3])
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
76
        self.assertEquals(expr.order_by, objects[4])
77
        self.assertEquals(expr.group_by, objects[5])
78
        self.assertEquals(expr.limit, objects[6])
79
        self.assertEquals(expr.offset, objects[7])
80
        self.assertEquals(expr.distinct, objects[8])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
81
82
    def test_insert_default(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
83
        expr = Insert(None)
84
        self.assertEquals(expr.map, None)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
85
        self.assertEquals(expr.table, Undef)
86
        self.assertEquals(expr.default_table, Undef)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
87
        self.assertEquals(expr.primary_columns, Undef)
88
        self.assertEquals(expr.primary_variables, Undef)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
89
90
    def test_insert_constructor(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
91
        objects = [object() for i in range(5)]
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
92
        expr = Insert(*objects)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
93
        self.assertEquals(expr.map, objects[0])
94
        self.assertEquals(expr.table, objects[1])
95
        self.assertEquals(expr.default_table, objects[2])
96
        self.assertEquals(expr.primary_columns, objects[3])
97
        self.assertEquals(expr.primary_variables, objects[4])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
98
99
    def test_update_default(self):
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
100
        expr = Update(None)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
101
        self.assertEquals(expr.map, None)
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
102
        self.assertEquals(expr.where, Undef)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
103
        self.assertEquals(expr.table, Undef)
104
        self.assertEquals(expr.default_table, Undef)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
105
106
    def test_update_constructor(self):
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
107
        objects = [object() for i in range(4)]
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
108
        expr = Update(*objects)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
109
        self.assertEquals(expr.map, objects[0])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
110
        self.assertEquals(expr.where, objects[1])
111
        self.assertEquals(expr.table, objects[2])
112
        self.assertEquals(expr.default_table, objects[3])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
113
114
    def test_delete_default(self):
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
115
        expr = Delete()
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
116
        self.assertEquals(expr.where, Undef)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
117
        self.assertEquals(expr.table, Undef)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
118
119
    def test_delete_constructor(self):
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
120
        objects = [object() for i in range(3)]
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
121
        expr = Delete(*objects)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
122
        self.assertEquals(expr.where, objects[0])
123
        self.assertEquals(expr.table, objects[1])
124
        self.assertEquals(expr.default_table, objects[2])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
125
4 by Gustavo Niemeyer
- Implemented And, Or, and Parameter expressions.
126
    def test_and(self):
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
127
        expr = And(elem1, elem2, elem3)
128
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
4 by Gustavo Niemeyer
- Implemented And, Or, and Parameter expressions.
129
130
    def test_or(self):
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
131
        expr = Or(elem1, elem2, elem3)
132
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
133
134
    def test_column_default(self):
30 by Gustavo Niemeyer
- Added support for retrieving values automatically set by the
135
        expr = Column()
136
        self.assertEquals(expr.name, Undef)
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
137
        self.assertEquals(expr.table, Undef)
95.6.5 by Gustavo Niemeyer
- __storm_table__ now will *only* contain the table.
138
139
        # Test for identity. We don't want False there.
140
        self.assertTrue(expr.primary is 0)
141
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
142
        self.assertEquals(expr.variable_factory, Variable)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
143
144
    def test_column_constructor(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
145
        objects = [object() for i in range(3)]
95.6.5 by Gustavo Niemeyer
- __storm_table__ now will *only* contain the table.
146
        objects.insert(2, True)
56 by Gustavo Niemeyer
- Implemented support for default and nullable parameters in columns,
147
        expr = Column(*objects)
148
        self.assertEquals(expr.name, objects[0])
149
        self.assertEquals(expr.table, objects[1])
95.6.5 by Gustavo Niemeyer
- __storm_table__ now will *only* contain the table.
150
151
        # Test for identity. We don't want True there either.
152
        self.assertTrue(expr.primary is 1)
153
154
        self.assertEquals(expr.variable_factory, objects[3])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
155
156
    def test_func(self):
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
157
        expr = Func("myfunc", elem1, elem2)
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
158
        self.assertEquals(expr.name, "myfunc")
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
159
        self.assertEquals(expr.args, (elem1, elem2))
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
160
161
    def test_named_func(self):
162
        class MyFunc(NamedFunc):
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
163
            name = "myfunc"
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
164
        expr = MyFunc(elem1, elem2)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
165
        self.assertEquals(expr.name, "myfunc")
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
166
        self.assertEquals(expr.args, (elem1, elem2))
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
167
168
    def test_like(self):
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
169
        expr = Like(elem1, elem2)
170
        self.assertEquals(expr.expr1, elem1)
171
        self.assertEquals(expr.expr2, elem2)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
172
111.3.1 by Michel Pelletier
-Case insensitive LIKE in Storm, take II
173
    def test_like_escape(self):
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
174
        expr = Like(elem1, elem2, elem3)
175
        self.assertEquals(expr.expr1, elem1)
176
        self.assertEquals(expr.expr2, elem2)
177
        self.assertEquals(expr.escape, elem3)
178
111.3.3 by Michel Pelletier
-Removed unused import
179
    def test_like_case(self):
180
        expr = Like(elem1, elem2, elem3)
181
        self.assertEquals(expr.case_sensitive, None)
182
        expr = Like(elem1, elem2, elem3, True)
183
        self.assertEquals(expr.case_sensitive, True)
184
        expr = Like(elem1, elem2, elem3, False)
185
        self.assertEquals(expr.case_sensitive, False)
186
329.4.1 by James Henstridge
Implement startswith() and endswith() methods for Comparable.
187
    def test_startswith(self):
188
        expr = Func1()
189
        self.assertRaises(ExprError, expr.startswith, "not a unicode string")
190
191
        like_expr = expr.startswith(u"abc!!_%")
192
        self.assertTrue(isinstance(like_expr, Like))
193
        self.assertTrue(like_expr.expr1 is expr)
194
        self.assertEquals(like_expr.expr2, u"abc!!!!!_!%%")
195
        self.assertEquals(like_expr.escape, u"!")
196
197
    def test_endswith(self):
198
        expr = Func1()
199
        self.assertRaises(ExprError, expr.startswith, "not a unicode string")
200
201
        like_expr = expr.endswith(u"abc!!_%")
202
        self.assertTrue(isinstance(like_expr, Like))
203
        self.assertTrue(like_expr.expr1 is expr)
204
        self.assertEquals(like_expr.expr2, u"%abc!!!!!_!%")
205
        self.assertEquals(like_expr.escape, u"!")
206
329.4.2 by James Henstridge
Add contains_string() method to check for substring matches.
207
    def test_contains_string(self):
208
        expr = Func1()
209
        self.assertRaises(
210
            ExprError, expr.contains_string, "not a unicode string")
211
212
        like_expr = expr.contains_string(u"abc!!_%")
213
        self.assertTrue(isinstance(like_expr, Like))
214
        self.assertTrue(like_expr.expr1 is expr)
215
        self.assertEquals(like_expr.expr2, u"%abc!!!!!_!%%")
216
        self.assertEquals(like_expr.escape, u"!")
217
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
218
    def test_eq(self):
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
219
        expr = Eq(elem1, elem2)
220
        self.assertEquals(expr.expr1, elem1)
221
        self.assertEquals(expr.expr2, elem2)
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
222
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
223
    def test_sql_default(self):
224
        expr = SQL(None)
225
        self.assertEquals(expr.expr, None)
226
        self.assertEquals(expr.params, Undef)
227
        self.assertEquals(expr.tables, Undef)
228
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
229
    def test_sql_constructor(self):
230
        objects = [object() for i in range(3)]
231
        expr = SQL(*objects)
232
        self.assertEquals(expr.expr, objects[0])
233
        self.assertEquals(expr.params, objects[1])
234
        self.assertEquals(expr.tables, objects[2])
235
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
236
    def test_join_expr_right(self):
237
        expr = JoinExpr(None)
238
        self.assertEquals(expr.right, None)
239
        self.assertEquals(expr.left, Undef)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
240
        self.assertEquals(expr.on, Undef)
241
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
242
    def test_join_expr_on(self):
243
        on = Expr()
244
        expr = JoinExpr(None, on)
245
        self.assertEquals(expr.right, None)
246
        self.assertEquals(expr.left, Undef)
247
        self.assertEquals(expr.on, on)
248
249
    def test_join_expr_on_keyword(self):
250
        on = Expr()
251
        expr = JoinExpr(None, on=on)
252
        self.assertEquals(expr.right, None)
253
        self.assertEquals(expr.left, Undef)
254
        self.assertEquals(expr.on, on)
255
256
    def test_join_expr_on_invalid(self):
257
        on = Expr()
258
        self.assertRaises(ExprError, JoinExpr, None, on, None)
259
260
    def test_join_expr_right_left(self):
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
261
        objects = [object() for i in range(2)]
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
262
        expr = JoinExpr(*objects)
263
        self.assertEquals(expr.left, objects[0])
264
        self.assertEquals(expr.right, objects[1])
265
        self.assertEquals(expr.on, Undef)
266
267
    def test_join_expr_right_left_on(self):
268
        objects = [object() for i in range(3)]
269
        expr = JoinExpr(*objects)
270
        self.assertEquals(expr.left, objects[0])
271
        self.assertEquals(expr.right, objects[1])
272
        self.assertEquals(expr.on, objects[2])
273
274
    def test_join_expr_right_join(self):
275
        join = JoinExpr(None)
276
        expr = JoinExpr(None, join)
277
        self.assertEquals(expr.right, join)
278
        self.assertEquals(expr.left, None)
279
        self.assertEquals(expr.on, Undef)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
280
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
281
    def test_table(self):
282
        objects = [object() for i in range(1)]
283
        expr = Table(*objects)
284
        self.assertEquals(expr.name, objects[0])
285
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
286
    def test_alias_default(self):
287
        expr = Alias(None)
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
288
        self.assertEquals(expr.expr, None)
289
        self.assertTrue(isinstance(expr.name, str))
290
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
291
    def test_alias_constructor(self):
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
292
        objects = [object() for i in range(2)]
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
293
        expr = Alias(*objects)
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
294
        self.assertEquals(expr.expr, objects[0])
295
        self.assertEquals(expr.name, objects[1])
296
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
297
    def test_union(self):
298
        expr = Union(elem1, elem2, elem3)
299
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
300
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
301
    def test_union_with_kwargs(self):
302
        expr = Union(elem1, elem2, all=True, order_by=(), limit=1, offset=2)
303
        self.assertEquals(expr.exprs, (elem1, elem2))
304
        self.assertEquals(expr.all, True)
305
        self.assertEquals(expr.order_by, ())
306
        self.assertEquals(expr.limit, 1)
307
        self.assertEquals(expr.offset, 2)
308
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
309
    def test_union_collapse(self):
310
        expr = Union(Union(elem1, elem2), elem3)
311
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
312
313
        # Only first expression is collapsed.
314
        expr = Union(elem1, Union(elem2, elem3))
315
        self.assertEquals(expr.exprs[0], elem1)
316
        self.assertTrue(isinstance(expr.exprs[1], Union))
317
318
        # Don't collapse if all is different.
319
        expr = Union(Union(elem1, elem2, all=True), elem3)
320
        self.assertTrue(isinstance(expr.exprs[0], Union))
321
        expr = Union(Union(elem1, elem2), elem3, all=True)
322
        self.assertTrue(isinstance(expr.exprs[0], Union))
323
        expr = Union(Union(elem1, elem2, all=True), elem3, all=True)
324
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
325
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
326
        # Don't collapse if limit or offset are set.
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
327
        expr = Union(Union(elem1, elem2, limit=1), elem3)
328
        self.assertTrue(isinstance(expr.exprs[0], Union))
329
        expr = Union(Union(elem1, elem2, offset=3), elem3)
330
        self.assertTrue(isinstance(expr.exprs[0], Union))
331
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
332
        # Don't collapse other set expressions.
333
        expr = Union(Except(elem1, elem2), elem3)
334
        self.assertTrue(isinstance(expr.exprs[0], Except))
335
        expr = Union(Intersect(elem1, elem2), elem3)
336
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
337
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
338
    def test_except(self):
339
        expr = Except(elem1, elem2, elem3)
340
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
341
342
    def test_except_with_kwargs(self):
343
        expr = Except(elem1, elem2, all=True, order_by=(), limit=1, offset=2)
344
        self.assertEquals(expr.exprs, (elem1, elem2))
345
        self.assertEquals(expr.all, True)
346
        self.assertEquals(expr.order_by, ())
347
        self.assertEquals(expr.limit, 1)
348
        self.assertEquals(expr.offset, 2)
349
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
350
    def test_except_collapse(self):
351
        expr = Except(Except(elem1, elem2), elem3)
352
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
353
354
        # Only first expression is collapsed.
355
        expr = Except(elem1, Except(elem2, elem3))
356
        self.assertEquals(expr.exprs[0], elem1)
357
        self.assertTrue(isinstance(expr.exprs[1], Except))
358
359
        # Don't collapse if all is different.
360
        expr = Except(Except(elem1, elem2, all=True), elem3)
361
        self.assertTrue(isinstance(expr.exprs[0], Except))
362
        expr = Except(Except(elem1, elem2), elem3, all=True)
363
        self.assertTrue(isinstance(expr.exprs[0], Except))
364
        expr = Except(Except(elem1, elem2, all=True), elem3, all=True)
365
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
366
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
367
        # Don't collapse if limit or offset are set.
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
368
        expr = Except(Except(elem1, elem2, limit=1), elem3)
369
        self.assertTrue(isinstance(expr.exprs[0], Except))
370
        expr = Except(Except(elem1, elem2, offset=3), elem3)
371
        self.assertTrue(isinstance(expr.exprs[0], Except))
372
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
373
        # Don't collapse other set expressions.
374
        expr = Except(Union(elem1, elem2), elem3)
375
        self.assertTrue(isinstance(expr.exprs[0], Union))
376
        expr = Except(Intersect(elem1, elem2), elem3)
377
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
378
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
379
    def test_intersect(self):
380
        expr = Intersect(elem1, elem2, elem3)
381
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
382
383
    def test_intersect_with_kwargs(self):
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
384
        expr = Intersect(
385
            elem1, elem2, all=True, order_by=(), limit=1, offset=2)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
386
        self.assertEquals(expr.exprs, (elem1, elem2))
387
        self.assertEquals(expr.all, True)
388
        self.assertEquals(expr.order_by, ())
389
        self.assertEquals(expr.limit, 1)
390
        self.assertEquals(expr.offset, 2)
391
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
392
    def test_intersect_collapse(self):
393
        expr = Intersect(Intersect(elem1, elem2), elem3)
394
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
395
396
        # Only first expression is collapsed.
397
        expr = Intersect(elem1, Intersect(elem2, elem3))
398
        self.assertEquals(expr.exprs[0], elem1)
399
        self.assertTrue(isinstance(expr.exprs[1], Intersect))
400
401
        # Don't collapse if all is different.
402
        expr = Intersect(Intersect(elem1, elem2, all=True), elem3)
403
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
404
        expr = Intersect(Intersect(elem1, elem2), elem3, all=True)
405
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
406
        expr = Intersect(Intersect(elem1, elem2, all=True), elem3, all=True)
407
        self.assertEquals(expr.exprs, (elem1, elem2, elem3))
408
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
409
        # Don't collapse if limit or offset are set.
329.2.1 by James Henstridge
Make SetExpr.__init__() collapse the first argument if it is compatible.
410
        expr = Intersect(Intersect(elem1, elem2, limit=1), elem3)
411
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
412
        expr = Intersect(Intersect(elem1, elem2, offset=3), elem3)
413
        self.assertTrue(isinstance(expr.exprs[0], Intersect))
414
329.2.2 by James Henstridge
Collapse set expressions with order_by set, and add some extra tests.
415
        # Don't collapse other set expressions.
416
        expr = Intersect(Union(elem1, elem2), elem3)
417
        self.assertTrue(isinstance(expr.exprs[0], Union))
418
        expr = Intersect(Except(elem1, elem2), elem3)
419
        self.assertTrue(isinstance(expr.exprs[0], Except))
420
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
421
    def test_auto_tables(self):
422
        expr = AutoTables(elem1, [elem2])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
423
        self.assertEquals(expr.expr, elem1)
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
424
        self.assertEquals(expr.tables, [elem2])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
425
156.1.2 by Gustavo Niemeyer
Implemented Sequence(name) expression, and added support for it
426
    def test_sequence(self):
427
        expr = Sequence(elem1)
428
        self.assertEquals(expr.name, elem1)
429
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
430
15 by Gustavo Niemeyer
Now compiler will pass a state around rather than just
431
class StateTest(TestHelper):
432
433
    def setUp(self):
434
        TestHelper.setUp(self)
435
        self.state = State()
436
437
    def test_attrs(self):
438
        self.assertEquals(self.state.parameters, [])
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
439
        self.assertEquals(self.state.auto_tables, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
440
        self.assertEquals(self.state.context, None)
15 by Gustavo Niemeyer
Now compiler will pass a state around rather than just
441
442
    def test_push_pop(self):
443
        self.state.parameters.extend([1, 2])
444
        self.state.push("parameters", [])
445
        self.assertEquals(self.state.parameters, [])
446
        self.state.pop()
447
        self.assertEquals(self.state.parameters, [1, 2])
448
        self.state.push("parameters")
449
        self.assertEquals(self.state.parameters, [1, 2])
450
        self.state.parameters.append(3)
451
        self.assertEquals(self.state.parameters, [1, 2, 3])
452
        self.state.pop()
453
        self.assertEquals(self.state.parameters, [1, 2])
454
455
    def test_push_pop_unexistent(self):
456
        self.state.push("nonexistent")
457
        self.assertEquals(self.state.nonexistent, None)
458
        self.state.nonexistent = "something"
459
        self.state.pop()
460
        self.assertEquals(self.state.nonexistent, None)
461
462
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
463
class CompileTest(TestHelper):
464
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
465
    def test_simple_inheritance(self):
466
        custom_compile = compile.create_child()
467
        statement = custom_compile(Func1())
468
        self.assertEquals(statement, "func1()")
469
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
470
    def test_customize(self):
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
471
        custom_compile = compile.create_child()
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
472
        @custom_compile.when(type(None))
15 by Gustavo Niemeyer
Now compiler will pass a state around rather than just
473
        def compile_none(compile, state, expr):
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
474
            return "None"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
475
        statement = custom_compile(Func1(None))
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
476
        self.assertEquals(statement, "func1(None)")
477
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
478
    def test_customize_inheritance(self):
479
        class C(object): pass
480
        compile_parent = Compile()
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
481
        compile_child = compile_parent.create_child()
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
482
483
        @compile_parent.when(C)
484
        def compile_in_parent(compile, state, expr):
485
            return "parent"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
486
        statement = compile_child(C())
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
487
        self.assertEquals(statement, "parent")
488
489
        @compile_child.when(C)
490
        def compile_in_child(compile, state, expr):
491
            return "child"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
492
        statement = compile_child(C())
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
493
        self.assertEquals(statement, "child")
494
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
495
    def test_precedence(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
496
        for i in range(10):
497
            exec "e%d = SQLRaw('%d')" % (i, i)
498
        expr = And(e1, Or(e2, e3),
499
                   Add(e4, Mul(e5, Sub(e6, Div(e7, Div(e8, e9))))))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
500
        statement = compile(expr)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
501
        self.assertEquals(statement, "1 AND (2 OR 3) AND 4+5*(6-7/(8/9))")
502
503
        expr = Func1(Select(Count()), [Select(Count())])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
504
        statement = compile(expr)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
505
        self.assertEquals(statement,
506
                          "func1((SELECT COUNT(*)), (SELECT COUNT(*)))")
507
508
    def test_get_precedence(self):
509
        self.assertTrue(compile.get_precedence(Or) <
510
                        compile.get_precedence(And))
511
        self.assertTrue(compile.get_precedence(Add) <
512
                        compile.get_precedence(Mul))
513
        self.assertTrue(compile.get_precedence(Sub) <
514
                        compile.get_precedence(Div))
515
516
    def test_customize_precedence(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
517
        expr = And(elem1, Or(elem2, elem3))
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
518
        custom_compile = compile.create_child()
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
519
        custom_compile.set_precedence(10, And)
520
521
        custom_compile.set_precedence(11, Or)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
522
        statement = custom_compile(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
523
        self.assertEquals(statement, "elem1 AND elem2 OR elem3")
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
524
525
        custom_compile.set_precedence(10, Or)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
526
        statement = custom_compile(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
527
        self.assertEquals(statement, "elem1 AND elem2 OR elem3")
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
528
529
        custom_compile.set_precedence(9, Or)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
530
        statement = custom_compile(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
531
        self.assertEquals(statement, "elem1 AND (elem2 OR elem3)")
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
532
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
533
    def test_customize_precedence_inheritance(self):
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
534
        compile_parent = compile.create_child()
535
        compile_child = compile_parent.create_child()
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
536
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
537
        expr = And(elem1, Or(elem2, elem3))
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
538
539
        compile_parent.set_precedence(10, And)
540
541
        compile_parent.set_precedence(11, Or)
542
        self.assertEquals(compile_child.get_precedence(Or), 11)
543
        self.assertEquals(compile_parent.get_precedence(Or), 11)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
544
        statement = compile_child(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
545
        self.assertEquals(statement, "elem1 AND elem2 OR elem3")
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
546
547
        compile_parent.set_precedence(10, Or)
548
        self.assertEquals(compile_child.get_precedence(Or), 10)
549
        self.assertEquals(compile_parent.get_precedence(Or), 10)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
550
        statement = compile_child(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
551
        self.assertEquals(statement, "elem1 AND elem2 OR elem3")
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
552
553
        compile_child.set_precedence(9, Or)
554
        self.assertEquals(compile_child.get_precedence(Or), 9)
555
        self.assertEquals(compile_parent.get_precedence(Or), 10)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
556
        statement = compile_child(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
557
        self.assertEquals(statement, "elem1 AND (elem2 OR elem3)")
81.1.11 by Gustavo Niemeyer
Implemented proper inheritance of Compile, replacing the copy()
558
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
559
    def test_compile_sequence(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
560
        expr = [elem1, Func1(), (Func2(), None)]
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
561
        statement = compile(expr)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
562
        self.assertEquals(statement, "elem1, func1(), func2(), NULL")
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
563
564
    def test_compile_invalid(self):
565
        self.assertRaises(CompileError, compile, object())
566
        self.assertRaises(CompileError, compile, [object()])
567
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
568
    def test_str(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
569
        state = State()
570
        statement = compile("str", state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
571
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
572
        self.assertVariablesEqual(state.parameters, [RawStrVariable("str")])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
573
574
    def test_unicode(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
575
        state = State()
576
        statement = compile(u"str", state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
577
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
578
        self.assertVariablesEqual(state.parameters, [UnicodeVariable(u"str")])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
579
580
    def test_int(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
581
        state = State()
582
        statement = compile(1, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
583
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
584
        self.assertVariablesEqual(state.parameters, [IntVariable(1)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
585
586
    def test_long(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
587
        state = State()
588
        statement = compile(1L, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
589
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
590
        self.assertVariablesEqual(state.parameters, [IntVariable(1)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
591
592
    def test_bool(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
593
        state = State()
594
        statement = compile(True, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
595
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
596
        self.assertVariablesEqual(state.parameters, [BoolVariable(1)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
597
598
    def test_float(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
599
        state = State()
600
        statement = compile(1.1, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
601
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
602
        self.assertVariablesEqual(state.parameters, [FloatVariable(1.1)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
603
144.2.3 by Gustavo Niemeyer
- Renamed NumericVariable to DecimalVariable, and the property to Decimal,
604
    def test_decimal(self):
157 by Gustavo Niemeyer
Merged storm-numeric branch, by Johan Dahlin [r=jamesh,niemeyer]
605
        state = State()
606
        statement = compile(Decimal("1.1"), state)
144.2.1 by jdahlin at com
Add NumericVariable and Numeric property mapping to python's Decimal type
607
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
608
        self.assertVariablesEqual(
609
            state.parameters, [DecimalVariable(Decimal("1.1"))])
144.2.1 by jdahlin at com
Add NumericVariable and Numeric property mapping to python's Decimal type
610
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
611
    def test_datetime(self):
612
        dt = datetime(1977, 5, 4, 12, 34)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
613
        state = State()
614
        statement = compile(dt, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
615
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
616
        self.assertVariablesEqual(state.parameters, [DateTimeVariable(dt)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
617
618
    def test_date(self):
619
        d = date(1977, 5, 4)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
620
        state = State()
621
        statement = compile(d, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
622
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
623
        self.assertVariablesEqual(state.parameters, [DateVariable(d)])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
624
625
    def test_time(self):
626
        t = time(12, 34)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
627
        state = State()
628
        statement = compile(t, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
629
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
630
        self.assertVariablesEqual(state.parameters, [TimeVariable(t)])
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
631
95.4.15 by James Henstridge
handle compilation of timedeltas
632
    def test_timedelta(self):
633
        td = timedelta(days=1, seconds=2, microseconds=3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
634
        state = State()
635
        statement = compile(td, state)
95.4.15 by James Henstridge
handle compilation of timedeltas
636
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
637
        self.assertVariablesEqual(state.parameters, [TimeDeltaVariable(td)])
95.4.15 by James Henstridge
handle compilation of timedeltas
638
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
639
    def test_none(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
640
        state = State()
641
        statement = compile(None, state)
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
642
        self.assertEquals(statement, "NULL")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
643
        self.assertEquals(state.parameters, [])
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
644
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
645
    def test_select(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
646
        expr = Select([column1, column2])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
647
        state = State()
648
        statement = compile(expr, state)
4 by Gustavo Niemeyer
- Implemented And, Or, and Parameter expressions.
649
        self.assertEquals(statement, "SELECT column1, column2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
650
        self.assertEquals(state.parameters, [])
3 by Gustavo Niemeyer
Created Select expression and Compiler with basic Select abilities.
651
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
652
    def test_select_distinct(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
653
        expr = Select([column1, column2], Undef, [table1], distinct=True)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
654
        state = State()
655
        statement = compile(expr, state)
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
656
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
657
                          'SELECT DISTINCT column1, column2 FROM "table 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
658
        self.assertEquals(state.parameters, [])
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
659
386.6.1 by William Grant
Support DISTINCT ON by passing a tuple/list into .config(distinct=...).
660
    def test_select_distinct_on(self):
661
        expr = Select([column1, column2], Undef, [table1],
662
                      distinct=[column2, column1])
663
        state = State()
664
        statement = compile(expr, state)
665
        self.assertEquals(statement,
666
                          'SELECT DISTINCT ON (column2, column1) '
667
                          'column1, column2 FROM "table 1"')
668
        self.assertEquals(state.parameters, [])
669
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
670
    def test_select_where(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
671
        expr = Select([column1, Func1()],
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
672
                      Func1(),
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
673
                      [table1, Func1()],
674
                      order_by=[column2, Func1()],
675
                      group_by=[column3, Func1()],
7 by Gustavo Niemeyer
- Started implementing Store.
676
                      limit=3, offset=4)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
677
        state = State()
678
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
679
        self.assertEquals(statement, 'SELECT column1, func1() '
680
                                     'FROM "table 1", func1() '
681
                                     'WHERE func1() '
151 by Gustavo Niemeyer
Inverted the order in which GROUP BY and ORDER BY are compiled
682
                                     'GROUP BY column3, func1() '
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
683
                                     'ORDER BY column2, func1() '
684
                                     'LIMIT 3 OFFSET 4')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
685
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
686
86 by Gustavo Niemeyer
Fix ordering of parameters when select has variables inside
687
    def test_select_join_where(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
688
        expr = Select(column1,
86 by Gustavo Niemeyer
Fix ordering of parameters when select has variables inside
689
                      Func1() == "value1",
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
690
                      Join(table1, Func2() == "value2"))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
691
        state = State()
692
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
693
        self.assertEquals(statement, 'SELECT column1 FROM '
694
                                     'JOIN "table 1" ON func2() = ? '
695
                                     'WHERE func1() = ?')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
696
        self.assertEquals([variable.get() for variable in state.parameters],
86 by Gustavo Niemeyer
Fix ordering of parameters when select has variables inside
697
                          ["value2", "value1"])
698
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
699
    def test_select_auto_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
700
        expr = Select(Column(column1, table1),
701
                      Column(column2, table2) == 1),
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
702
        state = State()
703
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
704
        self.assertEquals(statement, 'SELECT "table 1".column1 '
705
                                     'FROM "table 1", "table 2" '
706
                                     'WHERE "table 2".column2 = ?')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
707
        self.assertVariablesEqual(state.parameters, [Variable(1)])
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
708
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
709
    def test_select_auto_table_duplicated(self):
710
        expr = Select(Column(column1, table1),
711
                      Column(column2, table1) == 1),
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
712
        state = State()
713
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
714
        self.assertEquals(statement, 'SELECT "table 1".column1 '
715
                                     'FROM "table 1" WHERE '
716
                                     '"table 1".column2 = ?')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
717
        self.assertVariablesEqual(state.parameters, [Variable(1)])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
718
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
719
    def test_select_auto_table_default(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
720
        expr = Select(Column(column1),
721
                      Column(column2) == 1,
722
                      default_tables=table1),
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
723
        state = State()
724
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
725
        self.assertEquals(statement, 'SELECT column1 FROM "table 1" '
726
                                     'WHERE column2 = ?')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
727
        self.assertVariablesEqual(state.parameters, [Variable(1)])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
728
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
729
    def test_select_auto_table_default_with_joins(self):
730
        expr = Select(Column(column1),
731
                      default_tables=[table1, Join(table2)]),
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
732
        state = State()
733
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
734
        self.assertEquals(statement, 'SELECT column1 '
735
                                     'FROM "table 1" JOIN "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
736
        self.assertEquals(state.parameters, [])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
737
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
738
    def test_select_auto_table_unknown(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
739
        statement = compile(Select(elem1))
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
740
        self.assertEquals(statement, "SELECT elem1")
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
741
742
    def test_select_auto_table_sub(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
743
        col1 = Column(column1, table1)
744
        col2 = Column(column2, table2)
745
        expr = Select(col1, In(elem1, Select(col2, col1 == col2, col2.table)))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
746
        statement = compile(expr)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
747
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
748
                          'SELECT "table 1".column1 FROM "table 1" WHERE '
749
                          'elem1 IN (SELECT "table 2".column2 FROM "table 2" '
750
                          'WHERE "table 1".column1 = "table 2".column2)')
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
751
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
752
    def test_select_join(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
753
        expr = Select([column1, Func1()], Func1(),
754
                      [table1, Join(table2), Join(table3)])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
755
        state = State()
756
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
757
        self.assertEquals(statement, 'SELECT column1, func1() '
758
                                     'FROM "table 1" JOIN "table 2"'
759
                                     ' JOIN "table 3" '
760
                                     'WHERE func1()')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
761
        self.assertEquals(state.parameters, [])
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
762
763
    def test_select_join_right_left(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
764
        expr = Select([column1, Func1()], Func1(),
765
                      [table1, Join(table2, table3)])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
766
        state = State()
767
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
768
        self.assertEquals(statement, 'SELECT column1, func1() '
769
                                     'FROM "table 1", "table 2" '
770
                                     'JOIN "table 3" WHERE func1()')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
771
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
772
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
773
    def test_select_with_strings(self):
774
        expr = Select(column1, "1 = 2", table1, order_by="column1",
775
                      group_by="column2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
776
        state = State()
777
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
778
        self.assertEquals(statement, 'SELECT column1 FROM "table 1" '
151 by Gustavo Niemeyer
Inverted the order in which GROUP BY and ORDER BY are compiled
779
                                     'WHERE 1 = 2 GROUP BY column2 '
780
                                     'ORDER BY column1')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
781
        self.assertEquals(state.parameters, [])
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
782
95.3.50 by Gustavo Niemeyer
Now unicode strings are also considered in the 'raw' test in the compiler.
783
    def test_select_with_unicode(self):
784
        expr = Select(column1, u"1 = 2", table1, order_by=u"column1",
785
                      group_by=[u"column2"])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
786
        state = State()
787
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
788
        self.assertEquals(statement, 'SELECT column1 FROM "table 1" '
151 by Gustavo Niemeyer
Inverted the order in which GROUP BY and ORDER BY are compiled
789
                                     'WHERE 1 = 2 GROUP BY column2 '
790
                                     'ORDER BY column1')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
791
        self.assertEquals(state.parameters, [])
95.3.50 by Gustavo Niemeyer
Now unicode strings are also considered in the 'raw' test in the compiler.
792
215.8.3 by Thomas Herve
Add a test for select 'having' support
793
    def test_select_having(self):
794
        expr = Select(column1, tables=table1, order_by=u"column1",
795
                      group_by=[u"column2"], having=u"1 = 2")
796
        state = State()
797
        statement = compile(expr, state)
798
        self.assertEquals(statement, 'SELECT column1 FROM "table 1" '
799
                                     'GROUP BY column2 HAVING 1 = 2 '
800
                                     'ORDER BY column1')
801
        self.assertEquals(state.parameters, [])
802
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
803
    def test_select_contexts(self):
804
        column, where, table, order_by, group_by = track_contexts(5)
805
        expr = Select(column, where, table,
806
                      order_by=order_by, group_by=group_by)
807
        compile(expr)
808
        self.assertEquals(column.context, COLUMN)
809
        self.assertEquals(where.context, EXPR)
810
        self.assertEquals(table.context, TABLE)
811
        self.assertEquals(order_by.context, EXPR)
812
        self.assertEquals(group_by.context, EXPR)
813
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
814
    def test_insert(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
815
        expr = Insert({column1: elem1, Func1(): Func2()}, Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
816
        state = State()
817
        statement = compile(expr, state)
156.1.5 by Gustavo Niemeyer
Fix bad spacing, as reported by Jamu.
818
        self.assertTrue(statement in (
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
819
                        "INSERT INTO func2() (column1, func1()) "
820
                        "VALUES (elem1, func2())",
821
                        "INSERT INTO func2() (func1(), column1) "
822
                        "VALUES (func2(), elem1)"), statement)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
823
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
824
16 by Gustavo Niemeyer
- Made Update similar to Insert, taking columns and values,
825
    def test_insert_with_columns(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
826
        expr = Insert({Column(column1, table1): elem1,
827
                       Column(column2, table1): elem2}, table2)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
828
        state = State()
829
        statement = compile(expr, state)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
830
        self.assertTrue(statement in (
831
                        'INSERT INTO "table 2" (column1, column2) '
832
                        'VALUES (elem1, elem2)',
833
                        'INSERT INTO "table 2" (column2, column1) '
834
                        'VALUES (elem2, elem1)'), statement)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
835
        self.assertEquals(state.parameters, [])
16 by Gustavo Niemeyer
- Made Update similar to Insert, taking columns and values,
836
206.1.2 by Gustavo Niemeyer
Added a test equivalent to the one Thomas Herve added for update, but
837
    def test_insert_with_columns_to_escape(self):
838
        expr = Insert({Column("column 1", table1): elem1}, table2)
839
        state = State()
840
        statement = compile(expr, state)
841
        self.assertEquals(statement,
842
                          'INSERT INTO "table 2" ("column 1") VALUES (elem1)')
843
        self.assertEquals(state.parameters, [])
844
208 by Gustavo Niemeyer
Use token=True when compiling column names on the insert/update map
845
    def test_insert_with_columns_as_raw_strings(self):
846
        expr = Insert({"column 1": elem1}, table2)
847
        state = State()
848
        statement = compile(expr, state)
849
        self.assertEquals(statement,
850
                          'INSERT INTO "table 2" ("column 1") VALUES (elem1)')
851
        self.assertEquals(state.parameters, [])
852
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
853
    def test_insert_auto_table(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
854
        expr = Insert({Column(column1, table1): elem1})
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
855
        state = State()
856
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
857
        self.assertEquals(statement, 'INSERT INTO "table 1" (column1) '
858
                                     'VALUES (elem1)')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
859
        self.assertEquals(state.parameters, [])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
860
861
    def test_insert_auto_table_default(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
862
        expr = Insert({Column(column1): elem1}, default_table=table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
863
        state = State()
864
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
865
        self.assertEquals(statement, 'INSERT INTO "table 1" (column1) '
866
                                     'VALUES (elem1)')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
867
        self.assertEquals(state.parameters, [])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
868
869
    def test_insert_auto_table_unknown(self):
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
870
        expr = Insert({Column(column1): elem1})
59 by Gustavo Niemeyer
- Created exception hierarchy, and patched DBAPI2 modules to include
871
        self.assertRaises(NoTableError, compile, expr)
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
872
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
873
    def test_insert_contexts(self):
874
        column, value, table = track_contexts(3)
156.1.1 by Gustavo Niemeyer
- Implemented support for lazy expressions in primary keys. This opens
875
        expr = Insert({column: value}, table)
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
876
        compile(expr)
877
        self.assertEquals(column.context, COLUMN_NAME)
878
        self.assertEquals(value.context, EXPR)
879
        self.assertEquals(table.context, TABLE)
880
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
881
    def test_update(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
882
        expr = Update({column1: elem1, Func1(): Func2()}, table=Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
883
        state = State()
884
        statement = compile(expr, state)
156.1.5 by Gustavo Niemeyer
Fix bad spacing, as reported by Jamu.
885
        self.assertTrue(statement in (
886
                        "UPDATE func1() SET column1=elem1, func1()=func2()",
887
                        "UPDATE func1() SET func1()=func2(), column1=elem1"
888
                        ), statement)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
889
        self.assertEquals(state.parameters, [])
16 by Gustavo Niemeyer
- Made Update similar to Insert, taking columns and values,
890
891
    def test_update_with_columns(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
892
        expr = Update({Column(column1, table1): elem1}, table=table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
893
        state = State()
894
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
895
        self.assertEquals(statement, 'UPDATE "table 1" SET column1=elem1')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
896
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
897
206.1.1 by Thomas Herve
Add a test with a fix.
898
    def test_update_with_columns_to_escape(self):
899
        expr = Update({Column("column x", table1): elem1}, table=table1)
900
        state = State()
901
        statement = compile(expr, state)
902
        self.assertEquals(statement, 'UPDATE "table 1" SET "column x"=elem1')
903
        self.assertEquals(state.parameters, [])
904
208 by Gustavo Niemeyer
Use token=True when compiling column names on the insert/update map
905
    def test_update_with_columns_as_raw_strings(self):
906
        expr = Update({"column 1": elem1}, table=table2)
907
        state = State()
908
        statement = compile(expr, state)
909
        self.assertEquals(statement,
910
                          'UPDATE "table 2" SET "column 1"=elem1')
911
        self.assertEquals(state.parameters, [])
912
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
913
    def test_update_where(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
914
        expr = Update({column1: elem1}, Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
915
        state = State()
916
        statement = compile(expr, state)
17 by Gustavo Niemeyer
- Compile now is able to handle sequences internally, meaning that
917
        self.assertEquals(statement,
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
918
                          "UPDATE func2() SET column1=elem1 WHERE func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
919
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
920
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
921
    def test_update_auto_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
922
        expr = Update({Column(column1, table1): elem1})
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
923
        state = State()
924
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
925
        self.assertEquals(statement, 'UPDATE "table 1" SET column1=elem1')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
926
        self.assertEquals(state.parameters, [])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
927
928
    def test_update_auto_table_default(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
929
        expr = Update({Column(column1): elem1}, default_table=table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
930
        state = State()
931
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
932
        self.assertEquals(statement, 'UPDATE "table 1" SET column1=elem1')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
933
        self.assertEquals(state.parameters, [])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
934
935
    def test_update_auto_table_unknown(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
936
        expr = Update({Column(column1): elem1})
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
937
        self.assertRaises(CompileError, compile, expr)
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
938
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
939
    def test_update_with_strings(self):
940
        expr = Update({column1: elem1}, "1 = 2", table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
941
        state = State()
942
        statement = compile(expr, state)
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
943
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
944
                          'UPDATE "table 1" SET column1=elem1 WHERE 1 = 2')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
945
        self.assertEquals(state.parameters, [])
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
946
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
947
    def test_update_contexts(self):
948
        set_left, set_right, where, table = track_contexts(4)
949
        expr = Update({set_left: set_right}, where, table)
950
        compile(expr)
951
        self.assertEquals(set_left.context, COLUMN_NAME)
952
        self.assertEquals(set_right.context, COLUMN_NAME)
953
        self.assertEquals(where.context, EXPR)
954
        self.assertEquals(table.context, TABLE)
955
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
956
    def test_delete(self):
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
957
        expr = Delete(table=table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
958
        state = State()
959
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
960
        self.assertEquals(statement, 'DELETE FROM "table 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
961
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
962
963
    def test_delete_where(self):
964
        expr = Delete(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
965
        state = State()
966
        statement = compile(expr, state)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
967
        self.assertEquals(statement, "DELETE FROM func2() WHERE func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
968
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
969
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
970
    def test_delete_with_strings(self):
971
        expr = Delete("1 = 2", table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
972
        state = State()
973
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
974
        self.assertEquals(statement, 'DELETE FROM "table 1" WHERE 1 = 2')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
975
        self.assertEquals(state.parameters, [])
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
976
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
977
    def test_delete_auto_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
978
        expr = Delete(Column(column1, table1) == 1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
979
        state = State()
980
        statement = compile(expr, state)
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
981
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
982
                          'DELETE FROM "table 1" WHERE "table 1".column1 = ?')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
983
        self.assertVariablesEqual(state.parameters, [Variable(1)])
18 by Gustavo Niemeyer
Implemented AutoTable and added support on all statements.
984
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
985
    def test_delete_auto_table_default(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
986
        expr = Delete(Column(column1) == 1, default_table=table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
987
        state = State()
988
        statement = compile(expr, state)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
989
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
990
                          'DELETE FROM "table 1" WHERE column1 = ?')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
991
        self.assertVariablesEqual(state.parameters, [Variable(1)])
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
992
993
    def test_delete_auto_table_unknown(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
994
        expr = Delete(Column(column1) == 1)
59 by Gustavo Niemeyer
- Created exception hierarchy, and patched DBAPI2 modules to include
995
        self.assertRaises(NoTableError, compile, expr)
24 by Gustavo Niemeyer
- Removed AutoTable. It's now implemented internally in statements.
996
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
997
    def test_delete_contexts(self):
998
        where, table = track_contexts(2)
999
        expr = Delete(where, table)
1000
        compile(expr)
1001
        self.assertEquals(where.context, EXPR)
1002
        self.assertEquals(table.context, TABLE)
1003
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1004
    def test_column(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1005
        expr = Column(column1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1006
        state = State()
1007
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1008
        self.assertEquals(statement, "column1")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1009
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1010
1011
    def test_column_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1012
        expr = Select(Column(column1, Func1()))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1013
        state = State()
1014
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1015
        self.assertEquals(statement, "SELECT func1().column1 FROM func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1016
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1017
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1018
    def test_column_contexts(self):
1019
        table, = track_contexts(1)
1020
        expr = Column(column1, table)
1021
        compile(expr)
1022
        self.assertEquals(table.context, COLUMN_PREFIX)
1023
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1024
    def test_column_with_reserved_words(self):
1025
        expr = Select(Column("name 1", "table 1"))
1026
        state = State()
1027
        statement = compile(expr, state)
1028
        self.assertEquals(statement,
1029
                          'SELECT "table 1"."name 1" FROM "table 1"')
1030
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
1031
    def test_variable(self):
1032
        expr = Variable("value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1033
        state = State()
1034
        statement = compile(expr, state)
4 by Gustavo Niemeyer
- Implemented And, Or, and Parameter expressions.
1035
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1036
        self.assertVariablesEqual(state.parameters, [Variable("value")])
4 by Gustavo Niemeyer
- Implemented And, Or, and Parameter expressions.
1037
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1038
    def test_eq(self):
1039
        expr = Eq(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1040
        state = State()
1041
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1042
        self.assertEquals(statement, "func1() = func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1043
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1044
1045
        expr = Func1() == "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1046
        state = State()
1047
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1048
        self.assertEquals(statement, "func1() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1049
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1050
59.1.3 by Jamshed Kakar
- Introduced Result.close() and Connection.close().
1051
    def test_is_in(self):
1052
        expr = Func1().is_in(["Hello", "World"])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1053
        state = State()
1054
        statement = compile(expr, state)
59.1.3 by Jamshed Kakar
- Introduced Result.close() and Connection.close().
1055
        self.assertEquals(statement, "func1() IN (?, ?)")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1056
        self.assertVariablesEqual(
1057
            state.parameters, [Variable("Hello"), Variable("World")])
59.1.3 by Jamshed Kakar
- Introduced Result.close() and Connection.close().
1058
95.1.4 by Gustavo Niemeyer
Return None from column.is_in([]) so that it compiles to NULL, instead
1059
    def test_is_in_empty(self):
1060
        expr = Func1().is_in([])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1061
        state = State()
1062
        statement = compile(expr, state)
233.3.2 by Thomas Hervé
Remove useless SQLFalse/SQLTrue tokens.
1063
        self.assertEquals(statement, "?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1064
        self.assertVariablesEqual(state.parameters, [BoolVariable(False)])
95.1.4 by Gustavo Niemeyer
Return None from column.is_in([]) so that it compiles to NULL, instead
1065
95.1.5 by Gustavo Niemeyer
Implemented support for column.is_in(Expr()).
1066
    def test_is_in_expr(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1067
        expr = Func1().is_in(Select(column1))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1068
        state = State()
1069
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1070
        self.assertEquals(statement, "func1() IN (SELECT column1)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1071
        self.assertEquals(state.parameters, [])
95.1.5 by Gustavo Niemeyer
Implemented support for column.is_in(Expr()).
1072
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1073
    def test_eq_none(self):
1074
        expr = Func1() == None
1075
1076
        self.assertTrue(expr.expr2 is None)
1077
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1078
        state = State()
1079
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1080
        self.assertEquals(statement, "func1() IS NULL")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1081
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1082
1083
    def test_ne(self):
1084
        expr = Ne(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1085
        state = State()
1086
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1087
        self.assertEquals(statement, "func1() != func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1088
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1089
1090
        expr = Func1() != "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1091
        state = State()
1092
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1093
        self.assertEquals(statement, "func1() != ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1094
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1095
1096
    def test_ne_none(self):
1097
        expr = Func1() != None
1098
1099
        self.assertTrue(expr.expr2 is None)
1100
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1101
        state = State()
1102
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1103
        self.assertEquals(statement, "func1() IS NOT NULL")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1104
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1105
1106
    def test_gt(self):
1107
        expr = Gt(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1108
        state = State()
1109
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1110
        self.assertEquals(statement, "func1() > func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1111
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1112
1113
        expr = Func1() > "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1114
        state = State()
1115
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1116
        self.assertEquals(statement, "func1() > ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1117
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1118
1119
    def test_ge(self):
1120
        expr = Ge(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1121
        state = State()
1122
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1123
        self.assertEquals(statement, "func1() >= func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1124
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1125
1126
        expr = Func1() >= "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1127
        state = State()
1128
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1129
        self.assertEquals(statement, "func1() >= ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1130
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1131
1132
    def test_lt(self):
1133
        expr = Lt(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1134
        state = State()
1135
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1136
        self.assertEquals(statement, "func1() < func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1137
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1138
1139
        expr = Func1() < "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1140
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1141
        self.assertEquals(statement, "func1() < ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1142
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1143
1144
    def test_le(self):
1145
        expr = Le(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1146
        state = State()
1147
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1148
        self.assertEquals(statement, "func1() <= func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1149
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1150
1151
        expr = Func1() <= "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1152
        state = State()
1153
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1154
        self.assertEquals(statement, "func1() <= ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1155
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1156
1157
    def test_lshift(self):
1158
        expr = LShift(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1159
        state = State()
1160
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1161
        self.assertEquals(statement, "func1()<<func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1162
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1163
1164
        expr = Func1() << "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1165
        state = State()
1166
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1167
        self.assertEquals(statement, "func1()<<?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1168
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1169
1170
    def test_rshift(self):
1171
        expr = RShift(Func1(), Func2())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1172
        state = State()
1173
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1174
        self.assertEquals(statement, "func1()>>func2()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1175
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1176
1177
        expr = Func1() >> "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1178
        state = State()
1179
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1180
        self.assertEquals(statement, "func1()>>?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1181
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1182
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
1183
    def test_like(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1184
        expr = Like(Func1(), "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1185
        state = State()
1186
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1187
        self.assertEquals(statement, "func1() LIKE ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1188
        self.assertVariablesEqual(state.parameters, [RawStrVariable("value")])
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
1189
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1190
        expr = Func1().like("Hello")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1191
        state = State()
1192
        statement = compile(expr, state)
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1193
        self.assertEquals(statement, "func1() LIKE ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1194
        self.assertVariablesEqual(state.parameters, [Variable("Hello")])
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1195
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
1196
    def test_like_escape(self):
1197
        expr = Like(Func1(), "value", "!")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1198
        state = State()
1199
        statement = compile(expr, state)
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
1200
        self.assertEquals(statement, "func1() LIKE ? ESCAPE ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1201
        self.assertVariablesEqual(state.parameters,
147 by Gustavo Niemeyer
- The Chars type is now RawStr. Chars will stay as an alias for
1202
                          [RawStrVariable("value"), RawStrVariable("!")])
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
1203
1204
        expr = Func1().like("Hello", "!")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1205
        state = State()
1206
        statement = compile(expr, state)
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
1207
        self.assertEquals(statement, "func1() LIKE ? ESCAPE ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1208
        self.assertVariablesEqual(state.parameters,
150 by Gustavo Niemeyer
Merged the reserved-words branch [r=jkakar,radix] [f=126875]
1209
                          [Variable("Hello"), RawStrVariable("!")])
95.3.66 by Gustavo Niemeyer
Implemented support for ESCAPE in Like expressions.
1210
111.3.4 by Michel Pelletier
Added test for Comparable.like() case_sensitive argument
1211
    def test_like_compareable_case(self):
1212
        expr = Func1().like("Hello")
1213
        self.assertEquals(expr.case_sensitive, None)
1214
        expr = Func1().like("Hello", case_sensitive=True)
1215
        self.assertEquals(expr.case_sensitive, True)
1216
        expr = Func1().like("Hello", case_sensitive=False)
1217
        self.assertEquals(expr.case_sensitive, False)
1218
21 by Gustavo Niemeyer
- Added support for using multiple classes on find() to query
1219
    def test_in(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1220
        expr = In(Func1(), "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1221
        state = State()
1222
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1223
        self.assertEquals(statement, "func1() IN (?)")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1224
        self.assertVariablesEqual(state.parameters, [RawStrVariable("value")])
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1225
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1226
        expr = In(Func1(), elem1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1227
        state = State()
1228
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1229
        self.assertEquals(statement, "func1() IN (elem1)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1230
        self.assertEquals(state.parameters, [])
21 by Gustavo Niemeyer
- Added support for using multiple classes on find() to query
1231
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1232
    def test_and(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1233
        expr = And(elem1, elem2, And(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1234
        state = State()
1235
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1236
        self.assertEquals(statement, "elem1 AND elem2 AND elem3 AND elem4")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1237
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1238
1239
        expr = Func1() & "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1240
        state = State()
1241
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1242
        self.assertEquals(statement, "func1() AND ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1243
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1244
1245
    def test_or(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1246
        expr = Or(elem1, elem2, Or(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1247
        state = State()
1248
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1249
        self.assertEquals(statement, "elem1 OR elem2 OR elem3 OR elem4")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1250
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1251
1252
        expr = Func1() | "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1253
        state = State()
1254
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1255
        self.assertEquals(statement, "func1() OR ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1256
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1257
95.3.41 by Gustavo Niemeyer
And and Or will now also compile their direct children as raw=True.
1258
    def test_and_with_strings(self):
1259
        expr = And("elem1", "elem2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1260
        state = State()
1261
        statement = compile(expr, state)
95.3.41 by Gustavo Niemeyer
And and Or will now also compile their direct children as raw=True.
1262
        self.assertEquals(statement, "elem1 AND elem2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1263
        self.assertEquals(state.parameters, [])
95.3.41 by Gustavo Niemeyer
And and Or will now also compile their direct children as raw=True.
1264
1265
    def test_or_with_strings(self):
1266
        expr = Or("elem1", "elem2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1267
        state = State()
1268
        statement = compile(expr, state)
95.3.41 by Gustavo Niemeyer
And and Or will now also compile their direct children as raw=True.
1269
        self.assertEquals(statement, "elem1 OR elem2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1270
        self.assertEquals(state.parameters, [])
95.3.41 by Gustavo Niemeyer
And and Or will now also compile their direct children as raw=True.
1271
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1272
    def test_add(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1273
        expr = Add(elem1, elem2, Add(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1274
        state = State()
1275
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1276
        self.assertEquals(statement, "elem1+elem2+elem3+elem4")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1277
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1278
1279
        expr = Func1() + "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1280
        state = State()
1281
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1282
        self.assertEquals(statement, "func1()+?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1283
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1284
1285
    def test_sub(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1286
        expr = Sub(elem1, Sub(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1287
        state = State()
1288
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1289
        self.assertEquals(statement, "elem1-(elem2-elem3)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1290
        self.assertEquals(state.parameters, [])
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1291
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1292
        expr = Sub(Sub(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1293
        state = State()
1294
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1295
        self.assertEquals(statement, "elem1-elem2-elem3")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1296
        self.assertVariablesEqual(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1297
1298
        expr = Func1() - "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1299
        state = State()
1300
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1301
        self.assertEquals(statement, "func1()-?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1302
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1303
1304
    def test_mul(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1305
        expr = Mul(elem1, elem2, Mul(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1306
        state = State()
1307
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1308
        self.assertEquals(statement, "elem1*elem2*elem3*elem4")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1309
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1310
1311
        expr = Func1() * "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1312
        state = State()
1313
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1314
        self.assertEquals(statement, "func1()*?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1315
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1316
1317
    def test_div(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1318
        expr = Div(elem1, Div(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1319
        state = State()
1320
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1321
        self.assertEquals(statement, "elem1/(elem2/elem3)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1322
        self.assertEquals(state.parameters, [])
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1323
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1324
        expr = Div(Div(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1325
        state = State()
1326
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1327
        self.assertEquals(statement, "elem1/elem2/elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1328
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1329
1330
        expr = Func1() / "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1331
        state = State()
1332
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1333
        self.assertEquals(statement, "func1()/?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1334
        self.assertVariablesEqual(state.parameters, [Variable("value")])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1335
1336
    def test_mod(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1337
        expr = Mod(elem1, Mod(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1338
        state = State()
1339
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1340
        self.assertEquals(statement, "elem1%(elem2%elem3)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1341
        self.assertEquals(state.parameters, [])
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1342
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1343
        expr = Mod(Mod(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1344
        state = State()
1345
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1346
        self.assertEquals(statement, "elem1%elem2%elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1347
        self.assertEquals(state.parameters, [])
6 by Gustavo Niemeyer
- Implemented Comparable expressions.
1348
1349
        expr = Func1() % "value"
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1350
        state = State()
1351
        statement = compile(expr, state)
22 by Gustavo Niemeyer
Implemented automatic addition of parenthesis when needed,
1352
        self.assertEquals(statement, "func1()%?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1353
        self.assertVariablesEqual(state.parameters, [Variable("value")])
7 by Gustavo Niemeyer
- Started implementing Store.
1354
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
1355
    def test_func(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1356
        expr = Func("myfunc", elem1, Func1(elem2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1357
        state = State()
1358
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1359
        self.assertEquals(statement, "myfunc(elem1, func1(elem2))")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1360
        self.assertEquals(state.parameters, [])
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
1361
1362
    def test_named_func(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1363
        expr = Func1(elem1, Func2(elem2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1364
        state = State()
1365
        statement = compile(expr, state)
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1366
        self.assertEquals(statement, "func1(elem1, func2(elem2))")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1367
        self.assertEquals(state.parameters, [])
8 by Gustavo Niemeyer
Refactored the expression compiler into a generic function pattern.
1368
7 by Gustavo Niemeyer
- Started implementing Store.
1369
    def test_count(self):
1370
        expr = Count(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1371
        state = State()
1372
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1373
        self.assertEquals(statement, "COUNT(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1374
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1375
1376
    def test_count_all(self):
1377
        expr = Count()
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1378
        state = State()
1379
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1380
        self.assertEquals(statement, "COUNT(*)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1381
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1382
87 by Gustavo Niemeyer
Implemented distinct support in count expressions.
1383
    def test_count_distinct(self):
1384
        expr = Count(Func1(), distinct=True)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1385
        state = State()
1386
        statement = compile(expr, state)
87 by Gustavo Niemeyer
Implemented distinct support in count expressions.
1387
        self.assertEquals(statement, "COUNT(DISTINCT func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1388
        self.assertEquals(state.parameters, [])
87 by Gustavo Niemeyer
Implemented distinct support in count expressions.
1389
1390
    def test_count_distinct_all(self):
1391
        self.assertRaises(ValueError, Count, distinct=True)
1392
383.1.1 by Jelmer Vernooij
Add Cast() function to storm.expr.
1393
    def test_cast(self):
384 by Jelmer Vernooij
Merged cast [r=jkakar,niemeyer] [f=681121]
1394
        """
1395
        The L{Cast} expression renders a C{CAST} function call with a
1396
        user-defined input value and the type to cast it to.
1397
        """
383.1.1 by Jelmer Vernooij
Add Cast() function to storm.expr.
1398
        expr = Cast(Func1(), "TEXT")
1399
        state = State()
1400
        statement = compile(expr, state)
1401
        self.assertEquals(statement, "CAST(func1() AS TEXT)")
1402
        self.assertEquals(state.parameters, [])
1403
7 by Gustavo Niemeyer
- Started implementing Store.
1404
    def test_max(self):
1405
        expr = Max(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1406
        state = State()
1407
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1408
        self.assertEquals(statement, "MAX(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1409
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1410
1411
    def test_min(self):
1412
        expr = Min(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1413
        state = State()
1414
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1415
        self.assertEquals(statement, "MIN(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1416
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1417
1418
    def test_avg(self):
1419
        expr = Avg(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1420
        state = State()
1421
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1422
        self.assertEquals(statement, "AVG(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1423
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1424
1425
    def test_sum(self):
1426
        expr = Sum(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1427
        state = State()
1428
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1429
        self.assertEquals(statement, "SUM(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1430
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1431
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1432
    def test_lower(self):
1433
        expr = Lower(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1434
        state = State()
1435
        statement = compile(expr, state)
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1436
        self.assertEquals(statement, "LOWER(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1437
        self.assertEquals(state.parameters, [])
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1438
1439
        expr = Func1().lower()
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1440
        state = State()
1441
        statement = compile(expr, state)
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1442
        self.assertEquals(statement, "LOWER(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1443
        self.assertEquals(state.parameters, [])
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1444
1445
    def test_upper(self):
1446
        expr = Upper(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1447
        state = State()
1448
        statement = compile(expr, state)
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1449
        self.assertEquals(statement, "UPPER(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1450
        self.assertEquals(state.parameters, [])
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1451
1452
        expr = Func1().upper()
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1453
        state = State()
1454
        statement = compile(expr, state)
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1455
        self.assertEquals(statement, "UPPER(func1())")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1456
        self.assertEquals(state.parameters, [])
95.1.7 by Gustavo Niemeyer
Implemented Lower, Upper, and Column.lower/upper().
1457
314.1.1 by Jamu Kakar
- A new Coalesce expression is available.
1458
    def test_coalesce(self):
1459
        expr = Coalesce(Func1())
1460
        state = State()
1461
        statement = compile(expr, state)
1462
        self.assertEquals(statement, "COALESCE(func1())")
1463
        self.assertEquals(state.parameters, [])
1464
1465
    def test_coalesce_with_many_arguments(self):
1466
        expr = Coalesce(Func1(), Func2(), None)
1467
        state = State()
1468
        statement = compile(expr, state)
1469
        self.assertEquals(statement, "COALESCE(func1(), func2(), NULL)")
1470
        self.assertEquals(state.parameters, [])
1471
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1472
    def test_not(self):
1473
        expr = Not(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1474
        state = State()
1475
        statement = compile(expr, state)
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1476
        self.assertEquals(statement, "NOT func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1477
        self.assertEquals(state.parameters, [])
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1478
1479
    def test_exists(self):
1480
        expr = Exists(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1481
        state = State()
1482
        statement = compile(expr, state)
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1483
        self.assertEquals(statement, "EXISTS func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1484
        self.assertEquals(state.parameters, [])
95.1.6 by Gustavo Niemeyer
- Implemented support for Column.like(...).
1485
312.1.1 by Michael Hudson
support unary -
1486
    def test_neg(self):
1487
        expr = Neg(Func1())
1488
        state = State()
1489
        statement = compile(expr, state)
1490
        self.assertEquals(statement, "- func1()")
1491
        self.assertEquals(state.parameters, [])
1492
1493
        expr = -Func1()
1494
        state = State()
1495
        statement = compile(expr, state)
1496
        self.assertEquals(statement, "- func1()")
1497
        self.assertEquals(state.parameters, [])
1498
7 by Gustavo Niemeyer
- Started implementing Store.
1499
    def test_asc(self):
1500
        expr = Asc(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1501
        state = State()
1502
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1503
        self.assertEquals(statement, "func1() ASC")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1504
        self.assertEquals(state.parameters, [])
7 by Gustavo Niemeyer
- Started implementing Store.
1505
1506
    def test_desc(self):
1507
        expr = Desc(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1508
        state = State()
1509
        statement = compile(expr, state)
7 by Gustavo Niemeyer
- Started implementing Store.
1510
        self.assertEquals(statement, "func1() DESC")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1511
        self.assertEquals(state.parameters, [])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
1512
95.3.34 by Gustavo Niemeyer
- Desc and Asc now use raw=True on their arguments.
1513
    def test_asc_with_string(self):
1514
        expr = Asc("column")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1515
        state = State()
1516
        statement = compile(expr, state)
95.3.34 by Gustavo Niemeyer
- Desc and Asc now use raw=True on their arguments.
1517
        self.assertEquals(statement, "column ASC")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1518
        self.assertEquals(state.parameters, [])
95.3.34 by Gustavo Niemeyer
- Desc and Asc now use raw=True on their arguments.
1519
1520
    def test_desc_with_string(self):
1521
        expr = Desc("column")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1522
        state = State()
1523
        statement = compile(expr, state)
95.3.34 by Gustavo Niemeyer
- Desc and Asc now use raw=True on their arguments.
1524
        self.assertEquals(statement, "column DESC")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1525
        self.assertEquals(state.parameters, [])
95.3.34 by Gustavo Niemeyer
- Desc and Asc now use raw=True on their arguments.
1526
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1527
    def test_sql(self):
1528
        expr = SQL("expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1529
        state = State()
1530
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1531
        self.assertEquals(statement, "expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1532
        self.assertEquals(state.parameters, [])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1533
1534
    def test_sql_params(self):
1535
        expr = SQL("expression", ["params"])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1536
        state = State()
1537
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1538
        self.assertEquals(statement, "expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1539
        self.assertEquals(state.parameters, ["params"])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1540
1541
    def test_sql_invalid_params(self):
1542
        expr = SQL("expression", "not a list or tuple")
1543
        self.assertRaises(CompileError, compile, expr)
1544
1545
    def test_sql_tables(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1546
        expr = Select([column1, Func1()], SQL("expression", [], Func2()))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1547
        state = State()
1548
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1549
        self.assertEquals(statement,
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1550
                          "SELECT column1, func1() FROM func2() "
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1551
                          "WHERE expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1552
        self.assertEquals(state.parameters, [])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1553
1554
    def test_sql_tables_with_list_or_tuple(self):
1555
        sql = SQL("expression", [], [Func1(), Func2()])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1556
        expr = Select(column1, sql)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1557
        state = State()
1558
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1559
        self.assertEquals(statement,
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1560
                          "SELECT column1 FROM func1(), func2() "
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1561
                          "WHERE expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1562
        self.assertEquals(state.parameters, [])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1563
1564
        sql = SQL("expression", [], (Func1(), Func2()))
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1565
        expr = Select(column1, sql)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1566
        state = State()
1567
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1568
        self.assertEquals(statement,
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1569
                          "SELECT column1 FROM func1(), func2() "
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1570
                          "WHERE expression")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1571
        self.assertEquals(state.parameters, [])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1572
1573
    def test_sql_comparison(self):
1574
        expr = SQL("expression1") & SQL("expression2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1575
        state = State()
1576
        statement = compile(expr, state)
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1577
        self.assertEquals(statement, "(expression1) AND (expression2)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1578
        self.assertEquals(state.parameters, [])
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
1579
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
1580
    def test_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1581
        expr = Table(table1)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1582
        state = State()
1583
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1584
        self.assertEquals(statement, '"table 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1585
        self.assertEquals(state.parameters, [])
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
1586
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
1587
    def test_alias(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1588
        expr = Alias(Table(table1), "name")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1589
        state = State()
1590
        statement = compile(expr, state)
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1591
        self.assertEquals(statement, "name")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1592
        self.assertEquals(state.parameters, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1593
1594
    def test_alias_in_tables(self):
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1595
        expr = Select(column1, tables=Alias(Table(table1), "alias 1"))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1596
        state = State()
1597
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1598
        self.assertEquals(statement,
1599
                          'SELECT column1 FROM "table 1" AS "alias 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1600
        self.assertEquals(state.parameters, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1601
1602
    def test_alias_in_tables_auto_name(self):
1603
        expr = Select(column1, tables=Alias(Table(table1)))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1604
        state = State()
1605
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1606
        self.assertEquals(statement[:statement.rfind("_")+1],
158 by Gustavo Niemeyer
- The reserved-words branch had a bug: MySQL's default escaping
1607
                          'SELECT column1 FROM "table 1" AS "_')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1608
        self.assertEquals(state.parameters, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1609
1610
    def test_alias_in_column_prefix(self):
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1611
        expr = Select(Column(column1, Alias(Table(table1), "alias 1")))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1612
        state = State()
1613
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1614
        self.assertEquals(statement, 'SELECT "alias 1".column1 '
1615
                                     'FROM "table 1" AS "alias 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1616
        self.assertEquals(state.parameters, [])
81.1.12 by Gustavo Niemeyer
- Reimplemented currval() call in the postgres database backend
1617
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1618
    def test_alias_for_column(self):
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1619
        expr = Select(Alias(Column(column1, table1), "alias 1"))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1620
        state = State()
1621
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1622
        self.assertEquals(statement, 'SELECT "table 1".column1 AS "alias 1" '
1623
                                     'FROM "table 1"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1624
        self.assertEquals(state.parameters, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1625
1626
    def test_alias_union(self):
1627
        union = Union(Select(elem1), Select(elem2))
1628
        expr = Select(elem3, tables=Alias(union, "alias"))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1629
        state = State()
1630
        statement = compile(expr, state)
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1631
        self.assertEquals(statement,
1632
                          "SELECT elem3 FROM "
1633
                          "((SELECT elem1) UNION (SELECT elem2)) AS alias")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1634
        self.assertEquals(state.parameters, [])
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1635
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1636
    def test_join(self):
1637
        expr = Join(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1638
        state = State()
1639
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1640
        self.assertEquals(statement, "JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1641
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1642
1643
    def test_join_on(self):
1644
        expr = Join(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1645
        state = State()
1646
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1647
        self.assertEquals(statement, "JOIN func1() ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1648
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1649
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
1650
    def test_join_on_with_string(self):
1651
        expr = Join(Func1(), on="a = b")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1652
        state = State()
1653
        statement = compile(expr, state)
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
1654
        self.assertEquals(statement, "JOIN func1() ON a = b")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1655
        self.assertEquals(state.parameters, [])
95.3.33 by Gustavo Niemeyer
Now the compiler is smart enough to know that in certain places, a
1656
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
1657
    def test_join_left_right(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1658
        expr = Join(table1, table2)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1659
        state = State()
1660
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1661
        self.assertEquals(statement, '"table 1" JOIN "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1662
        self.assertEquals(state.parameters, [])
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
1663
1664
    def test_join_nested(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1665
        expr = Join(table1, Join(table2, table3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1666
        state = State()
1667
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1668
        self.assertEquals(statement, '"table 1" JOIN '
1669
                                     '("table 2" JOIN "table 3")')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1670
        self.assertEquals(state.parameters, [])
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
1671
1672
    def test_join_double_nested(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1673
        expr = Join(Join(table1, table2), Join(table3, table4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1674
        state = State()
1675
        statement = compile(expr, state)
217.2.1 by James Henstridge
Joins are left-associative, so only add parentheses to the right.
1676
        self.assertEquals(statement, '"table 1" JOIN "table 2" JOIN '
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1677
                                     '("table 3" JOIN "table 4")')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1678
        self.assertEquals(state.parameters, [])
81.1.3 by Gustavo Niemeyer
- Redesigned join expressions to handle more complex cases.
1679
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
1680
    def test_join_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
1681
        expr = Join(Table(table1), Table(table2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1682
        state = State()
1683
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1684
        self.assertEquals(statement, '"table 1" JOIN "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1685
        self.assertEquals(state.parameters, [])
81.1.10 by Gustavo Niemeyer
Implemented As and Table expressions.
1686
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1687
    def test_join_contexts(self):
1688
        table1, table2, on = track_contexts(3)
1689
        expr = Join(table1, table2, on)
1690
        compile(expr)
1691
        self.assertEquals(table1.context, None)
1692
        self.assertEquals(table2.context, None)
1693
        self.assertEquals(on.context, EXPR)
1694
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1695
    def test_left_join(self):
1696
        expr = LeftJoin(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1697
        state = State()
1698
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1699
        self.assertEquals(statement, "LEFT JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1700
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1701
1702
    def test_left_join_on(self):
1703
        expr = LeftJoin(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1704
        state = State()
1705
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1706
        self.assertEquals(statement, "LEFT JOIN func1() ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1707
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1708
1709
    def test_right_join(self):
1710
        expr = RightJoin(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1711
        state = State()
1712
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1713
        self.assertEquals(statement, "RIGHT JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1714
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1715
1716
    def test_right_join_on(self):
1717
        expr = RightJoin(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1718
        state = State()
1719
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1720
        self.assertEquals(statement, "RIGHT JOIN func1() ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1721
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1722
1723
    def test_natural_join(self):
1724
        expr = NaturalJoin(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1725
        state = State()
1726
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1727
        self.assertEquals(statement, "NATURAL JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1728
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1729
1730
    def test_natural_join_on(self):
1731
        expr = NaturalJoin(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1732
        state = State()
1733
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1734
        self.assertEquals(statement, "NATURAL JOIN func1() ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1735
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1736
1737
    def test_natural_left_join(self):
1738
        expr = NaturalLeftJoin(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1739
        state = State()
1740
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1741
        self.assertEquals(statement, "NATURAL LEFT JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1742
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1743
1744
    def test_natural_left_join_on(self):
1745
        expr = NaturalLeftJoin(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1746
        state = State()
1747
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1748
        self.assertEquals(statement, "NATURAL LEFT JOIN func1() "
1749
                                     "ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1750
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1751
1752
    def test_natural_right_join(self):
1753
        expr = NaturalRightJoin(Func1())
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1754
        state = State()
1755
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1756
        self.assertEquals(statement, "NATURAL RIGHT JOIN func1()")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1757
        self.assertEquals(state.parameters, [])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1758
1759
    def test_natural_right_join_on(self):
1760
        expr = NaturalRightJoin(Func1(), Func2() == "value")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1761
        state = State()
1762
        statement = compile(expr, state)
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1763
        self.assertEquals(statement, "NATURAL RIGHT JOIN func1() "
1764
                                     "ON func2() = ?")
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1765
        self.assertVariablesEqual(state.parameters, [Variable("value")])
81.1.2 by Gustavo Niemeyer
Implemented join expressions.
1766
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1767
    def test_union(self):
1768
        expr = Union(Func1(), elem2, elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1769
        state = State()
1770
        statement = compile(expr, state)
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1771
        self.assertEquals(statement, "func1() UNION elem2 UNION elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1772
        self.assertEquals(state.parameters, [])
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1773
1774
    def test_union_all(self):
1775
        expr = Union(Func1(), elem2, elem3, all=True)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1776
        state = State()
1777
        statement = compile(expr, state)
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1778
        self.assertEquals(statement, "func1() UNION ALL elem2 UNION ALL elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1779
        self.assertEquals(state.parameters, [])
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1780
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1781
    def test_union_order_by_limit_offset(self):
1782
        expr = Union(elem1, elem2, order_by=Func1(), limit=1, offset=2)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1783
        state = State()
1784
        statement = compile(expr, state)
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1785
        self.assertEquals(statement, "elem1 UNION elem2 ORDER BY func1() "
1786
                                     "LIMIT 1 OFFSET 2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1787
        self.assertEquals(state.parameters, [])
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1788
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1789
    def test_union_select(self):
1790
        expr = Union(Select(elem1), Select(elem2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1791
        state = State()
1792
        statement = compile(expr, state)
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1793
        self.assertEquals(statement, "(SELECT elem1) UNION (SELECT elem2)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1794
        self.assertEquals(state.parameters, [])
95.3.20 by Gustavo Niemeyer
Initial implementation of ResultSet.union(). order_by() doesn't yet
1795
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1796
    def test_union_select_nested(self):
1797
        expr = Union(Select(elem1), Union(Select(elem2), Select(elem3)))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1798
        state = State()
1799
        statement = compile(expr, state)
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1800
        self.assertEquals(statement, "(SELECT elem1) UNION"
1801
                                     " ((SELECT elem2) UNION (SELECT elem3))")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1802
        self.assertEquals(state.parameters, [])
95.3.32 by Gustavo Niemeyer
- Improved a bit support for unions.
1803
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1804
    def test_union_order_by_and_select(self):
142.1.16 by Gustavo Niemeyer
Adding some docstrings and testing all reserved words from SQL1992,
1805
        """
1806
        When ORDER BY is present, databases usually have trouble using
1807
        fully qualified column names.  Because of that, we transform
1808
        pure column names into aliases, and use them in the ORDER BY.
1809
        """
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1810
        Alias.auto_counter = 0
1811
        column1 = Column(elem1)
1812
        column2 = Column(elem2)
1813
        expr = Union(Select(column1), Select(column2),
1814
                     order_by=(column1, column2))
1815
        state = State()
1816
        statement = compile(expr, state)
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1817
        self.assertEquals(
1818
            statement,
1819
            '(SELECT elem1 AS "_1") UNION (SELECT elem2 AS "_2") '
1820
            'ORDER BY "_1", "_2"')
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1821
        self.assertEquals(state.parameters, [])
1822
95.3.53 by Gustavo Niemeyer
- Now handling additional database crazyness regarding the ORDER BY
1823
    def test_union_contexts(self):
1824
        select1, select2, order_by = track_contexts(3)
1825
        expr = Union(select1, select2, order_by=order_by)
1826
        compile(expr)
1827
        self.assertEquals(select1.context, SELECT)
1828
        self.assertEquals(select2.context, SELECT)
1829
        self.assertEquals(order_by.context, COLUMN_NAME)
1830
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1831
    def test_except(self):
1832
        expr = Except(Func1(), elem2, elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1833
        state = State()
1834
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1835
        self.assertEquals(statement, "func1() EXCEPT elem2 EXCEPT elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1836
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1837
1838
    def test_except_all(self):
1839
        expr = Except(Func1(), elem2, elem3, all=True)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1840
        state = State()
1841
        statement = compile(expr, state)
126 by Gustavo Niemeyer
Let's go with Binary(Variable) rather than Bin(Variable).
1842
        self.assertEquals(statement, "func1() EXCEPT ALL elem2 "
1843
                                     "EXCEPT ALL elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1844
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1845
1846
    def test_except_order_by_limit_offset(self):
1847
        expr = Except(elem1, elem2, order_by=Func1(), limit=1, offset=2)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1848
        state = State()
1849
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1850
        self.assertEquals(statement, "elem1 EXCEPT elem2 ORDER BY func1() "
1851
                                     "LIMIT 1 OFFSET 2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1852
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1853
1854
    def test_except_select(self):
1855
        expr = Except(Select(elem1), Select(elem2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1856
        state = State()
1857
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1858
        self.assertEquals(statement, "(SELECT elem1) EXCEPT (SELECT elem2)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1859
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1860
1861
    def test_except_select_nested(self):
1862
        expr = Except(Select(elem1), Except(Select(elem2), Select(elem3)))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1863
        state = State()
1864
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1865
        self.assertEquals(statement, "(SELECT elem1) EXCEPT"
1866
                                     " ((SELECT elem2) EXCEPT (SELECT elem3))")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1867
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1868
1869
    def test_except_contexts(self):
1870
        select1, select2, order_by = track_contexts(3)
1871
        expr = Except(select1, select2, order_by=order_by)
1872
        compile(expr)
1873
        self.assertEquals(select1.context, SELECT)
1874
        self.assertEquals(select2.context, SELECT)
1875
        self.assertEquals(order_by.context, COLUMN_NAME)
1876
1877
    def test_intersect(self):
1878
        expr = Intersect(Func1(), elem2, elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1879
        state = State()
1880
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1881
        self.assertEquals(statement, "func1() INTERSECT elem2 INTERSECT elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1882
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1883
1884
    def test_intersect_all(self):
1885
        expr = Intersect(Func1(), elem2, elem3, all=True)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1886
        state = State()
1887
        statement = compile(expr, state)
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1888
        self.assertEquals(
1889
            statement, "func1() INTERSECT ALL elem2 INTERSECT ALL elem3")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1890
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1891
1892
    def test_intersect_order_by_limit_offset(self):
1893
        expr = Intersect(elem1, elem2, order_by=Func1(), limit=1, offset=2)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1894
        state = State()
1895
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1896
        self.assertEquals(statement, "elem1 INTERSECT elem2 ORDER BY func1() "
1897
                                     "LIMIT 1 OFFSET 2")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1898
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1899
1900
    def test_intersect_select(self):
1901
        expr = Intersect(Select(elem1), Select(elem2))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1902
        state = State()
1903
        statement = compile(expr, state)
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1904
        self.assertEquals(statement, "(SELECT elem1) INTERSECT (SELECT elem2)")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1905
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1906
1907
    def test_intersect_select_nested(self):
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1908
        expr = Intersect(
1909
            Select(elem1), Intersect(Select(elem2), Select(elem3)))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1910
        state = State()
1911
        statement = compile(expr, state)
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1912
        self.assertEquals(
1913
            statement, "(SELECT elem1) INTERSECT"
1914
                       " ((SELECT elem2) INTERSECT (SELECT elem3))")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1915
        self.assertEquals(state.parameters, [])
95.3.60 by Gustavo Niemeyer
Implemented support for ResultSet.difference()/intersection().
1916
1917
    def test_intersect_contexts(self):
1918
        select1, select2, order_by = track_contexts(3)
1919
        expr = Intersect(select1, select2, order_by=order_by)
1920
        compile(expr)
1921
        self.assertEquals(select1.context, SELECT)
1922
        self.assertEquals(select2.context, SELECT)
1923
        self.assertEquals(order_by.context, COLUMN_NAME)
1924
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1925
    def test_auto_table(self):
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
1926
        expr = Select(AutoTables(1, [table1]))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1927
        state = State()
1928
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1929
        self.assertEquals(statement, 'SELECT ? FROM "table 1"')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1930
        self.assertVariablesEqual(state.parameters, [IntVariable(1)])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1931
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
1932
    def test_auto_tables_with_column(self):
1933
        expr = Select(AutoTables(Column(elem1, table1), [table2]))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1934
        state = State()
1935
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1936
        self.assertEquals(statement, 'SELECT "table 1".elem1 '
1937
                                     'FROM "table 1", "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1938
        self.assertEquals(state.parameters, [])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1939
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
1940
    def test_auto_tables_with_column_and_replace(self):
1941
        expr = Select(AutoTables(Column(elem1, table1), [table2], replace=True))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1942
        state = State()
1943
        statement = compile(expr, state)
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1944
        self.assertEquals(statement, 'SELECT "table 1".elem1 FROM "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1945
        self.assertEquals(state.parameters, [])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1946
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
1947
    def test_auto_tables_with_join(self):
1948
        expr = Select(AutoTables(Column(elem1, table1), [LeftJoin(table2)]))
1949
        state = State()
1950
        statement = compile(expr, state)
1951
        self.assertEquals(statement, 'SELECT "table 1".elem1 FROM "table 1" '
1952
                                     'LEFT JOIN "table 2"')
1953
        self.assertEquals(state.parameters, [])
1954
1955
    def test_auto_tables_with_join_with_left_table(self):
1956
        expr = Select(AutoTables(Column(elem1, table1),
1957
                                 [LeftJoin(table1, table2)]))
1958
        state = State()
1959
        statement = compile(expr, state)
1960
        self.assertEquals(statement, 'SELECT "table 1".elem1 FROM "table 1" '
1961
                                     'LEFT JOIN "table 2"')
1962
        self.assertEquals(state.parameters, [])
1963
1964
    def test_auto_tables_duplicated(self):
1965
        expr = Select([AutoTables(Column(elem1, table1), [Join(table2)]),
1966
                       AutoTables(Column(elem2, table2), [Join(table1)]),
1967
                       AutoTables(Column(elem3, table1), [Join(table1)]),
1968
                       AutoTables(Column(elem4, table3), [table1]),
1969
                       AutoTables(Column(elem5, table1),
1970
                                  [Join(table4, table5)])])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1971
        state = State()
1972
        statement = compile(expr, state)
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1973
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1974
                          'SELECT "table 1".elem1, "table 2".elem2, '
1975
                          '"table 1".elem3, "table 3".elem4, "table 1".elem5 '
1976
                          'FROM "table 3", "table 4" JOIN "table 5" JOIN '
1977
                          '"table 1" JOIN "table 2"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1978
        self.assertEquals(state.parameters, [])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1979
215.4.5 by Gustavo Niemeyer
Renamed AutoTable to AutoTables. It now uses a list/tuple of tables
1980
    def test_auto_tables_duplicated_nested(self):
1981
        expr = Select(AutoTables(Column(elem1, table1), [Join(table2)]),
1982
                      In(1, Select(AutoTables(Column(elem1, table1),
1983
                                              [Join(table2)]))))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1984
        state = State()
1985
        statement = compile(expr, state)
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1986
        self.assertEquals(statement,
142.1.14 by Gustavo Niemeyer
- Added support for token=bool in the compiler. If used, that flag will
1987
                          'SELECT "table 1".elem1 FROM "table 1" JOIN '
1988
                          '"table 2" WHERE ? IN (SELECT "table 1".elem1 '
1989
                          'FROM "table 1" JOIN "table 2")')
269.1.3 by Thomas Hervé
Make tests/expr.py not rely on Variable.__eq__
1990
        self.assertVariablesEqual(state.parameters, [IntVariable(1)])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
1991
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
1992
    def test_sql_token(self):
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
1993
        expr = SQLToken("something")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1994
        state = State()
1995
        statement = compile(expr, state)
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
1996
        self.assertEquals(statement, "something")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
1997
        self.assertEquals(state.parameters, [])
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
1998
142.1.13 by Gustavo Niemeyer
SQLToken will escape the string when there are embedded spaces, or
1999
    def test_sql_token_spaces(self):
2000
        expr = SQLToken("some thing")
2001
        statement = compile(expr)
2002
        self.assertEquals(statement, '"some thing"')
2003
2004
    def test_sql_token_quotes(self):
142.1.15 by Gustavo Niemeyer
Embedded single quotes should be double-quoted as well.
2005
        expr = SQLToken("some'thing")
2006
        statement = compile(expr)
2007
        self.assertEquals(statement, '"some\'thing"')
2008
2009
    def test_sql_token_double_quotes(self):
142.1.13 by Gustavo Niemeyer
SQLToken will escape the string when there are embedded spaces, or
2010
        expr = SQLToken('some"thing')
2011
        statement = compile(expr)
2012
        self.assertEquals(statement, '"some""thing"')
2013
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2014
    def test_sql_token_reserved(self):
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
2015
        custom_compile = compile.create_child()
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
2016
        custom_compile.add_reserved_words(["something"])
2017
        expr = SQLToken("something")
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2018
        state = State()
2019
        statement = custom_compile(expr, state)
142.1.1 by Gustavo Niemeyer
- Added Compile.add_reserved_words()/is_reserved_word().
2020
        self.assertEquals(statement, '"something"')
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2021
        self.assertEquals(state.parameters, [])
106.1.1 by Gustavo Niemeyer
- state.auto_tables and select/update/insert.default_table(s) may now
2022
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2023
    def test_sql_token_reserved_from_parent(self):
2024
        expr = SQLToken("something")
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
2025
        parent_compile = compile.create_child()
2026
        child_compile = parent_compile.create_child()
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2027
        statement = child_compile(expr)
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2028
        self.assertEquals(statement, "something")
2029
        parent_compile.add_reserved_words(["something"])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2030
        statement = child_compile(expr)
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2031
        self.assertEquals(statement, '"something"')
2032
2033
    def test_sql_token_remove_reserved_word_on_child(self):
2034
        expr = SQLToken("something")
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
2035
        parent_compile = compile.create_child()
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2036
        parent_compile.add_reserved_words(["something"])
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
2037
        child_compile = parent_compile.create_child()
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2038
        statement = child_compile(expr)
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2039
        self.assertEquals(statement, '"something"')
2040
        child_compile.remove_reserved_words(["something"])
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2041
        statement = child_compile(expr)
142.1.5 by Gustavo Niemeyer
Some fixups to state update logic of reserved word changes.
2042
        self.assertEquals(statement, "something")
2043
2044
    def test_is_reserved_word(self):
142.1.8 by Gustavo Niemeyer
- Renamed Compile.fork() to create_child(), since it's not *really* a
2045
        parent_compile = compile.create_child()
2046
        child_compile = parent_compile.create_child()
142.1.6 by Gustavo Niemeyer
- Added list of reserved words from SQL1992.
2047
        self.assertEquals(child_compile.is_reserved_word("someTHING"), False)
2048
        parent_compile.add_reserved_words(["SOMEthing"])
2049
        self.assertEquals(child_compile.is_reserved_word("somETHing"), True)
2050
        child_compile.remove_reserved_words(["soMETHing"])
2051
        self.assertEquals(child_compile.is_reserved_word("somethING"), False)
2052
2053
    def test_sql1992_reserved_words(self):
142.1.16 by Gustavo Niemeyer
Adding some docstrings and testing all reserved words from SQL1992,
2054
        reserved_words = """
2055
            absolute action add all allocate alter and any are as asc assertion
2056
            at authorization avg begin between bit bit_length both by cascade
2057
            cascaded case cast catalog char character char_ length
2058
            character_length check close coalesce collate collation column
2059
            commit connect connection constraint constraints continue convert
2060
            corresponding count create cross current current_date current_time
2061
            current_timestamp current_ user cursor date day deallocate dec
2062
            decimal declare default deferrable deferred delete desc describe
2063
            descriptor diagnostics disconnect distinct domain double drop else
2064
            end end-exec escape except exception exec execute exists external
2065
            extract false fetch first float for foreign found from full get
2066
            global go goto grant group having hour identity immediate in
2067
            indicator initially inner input insensitive insert int integer
2068
            intersect interval into is isolation join key language last leading
2069
            left level like local lower match max min minute module month names
2070
            national natural nchar next no not null nullif numeric octet_length
2071
            of on only open option or order outer output overlaps pad partial
2072
            position precision prepare preserve primary prior privileges
2073
            procedure public read real references relative restrict revoke
2074
            right rollback rows schema scroll second section select session
2075
            session_ user set size smallint some space sql sqlcode sqlerror
2076
            sqlstate substring sum system_user table temporary then time
2077
            timestamp timezone_ hour timezone_minute to trailing transaction
2078
            translate translation trim true union unique unknown update upper
2079
            usage user using value values varchar varying view when whenever
2080
            where with work write year zone
2081
            """.split()
2082
        for word in reserved_words:
2083
            self.assertEquals(compile.is_reserved_word(word), True)
142.1.3 by Gustavo Niemeyer
Implemented Compile.remove_reserved_words().
2084
81.1.1 by Gustavo Niemeyer
- Implemented generic SQL() expression.
2085
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2086
class CompilePythonTest(TestHelper):
2087
2088
    def test_precedence(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2089
        for i in range(10):
2090
            exec "e%d = SQLRaw('%d')" % (i, i)
2091
        expr = And(e1, Or(e2, e3),
2092
                   Add(e4, Mul(e5, Sub(e6, Div(e7, Div(e8, e9))))))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2093
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2094
        self.assertEquals(py_expr, "1 and (2 or 3) and 4+5*(6-7/(8/9))")
2095
2096
    def test_get_precedence(self):
2097
        self.assertTrue(compile_python.get_precedence(Or) <
2098
                        compile_python.get_precedence(And))
2099
        self.assertTrue(compile_python.get_precedence(Add) <
2100
                        compile_python.get_precedence(Mul))
2101
        self.assertTrue(compile_python.get_precedence(Sub) <
2102
                        compile_python.get_precedence(Div))
2103
2104
    def test_compile_sequence(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2105
        expr = [elem1, Variable(1), (Variable(2), None)]
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2106
        state = State()
2107
        py_expr = compile_python(expr, state)
2108
        self.assertEquals(py_expr, "elem1, _0, _1, None")
2109
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2110
2111
    def test_compile_invalid(self):
2112
        self.assertRaises(CompileError, compile_python, object())
2113
        self.assertRaises(CompileError, compile_python, [object()])
2114
2115
    def test_compile_unsupported(self):
2116
        self.assertRaises(CompileError, compile_python, Expr())
2117
        self.assertRaises(CompileError, compile_python, Func1())
2118
2119
    def test_str(self):
304.2.4 by James Henstridge
Compile str, unicode, int, long, float and None to string
2120
        py_expr = compile_python("str")
2121
        self.assertEquals(py_expr, "'str'")
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2122
2123
    def test_unicode(self):
304.2.4 by James Henstridge
Compile str, unicode, int, long, float and None to string
2124
        py_expr = compile_python(u"str")
2125
        self.assertEquals(py_expr, "u'str'")
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2126
2127
    def test_int(self):
304.2.4 by James Henstridge
Compile str, unicode, int, long, float and None to string
2128
        py_expr = compile_python(1)
2129
        self.assertEquals(py_expr, "1")
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2130
2131
    def test_long(self):
304.2.4 by James Henstridge
Compile str, unicode, int, long, float and None to string
2132
        py_expr = compile_python(1L)
2133
        self.assertEquals(py_expr, "1L")
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2134
2135
    def test_bool(self):
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2136
        state = State()
2137
        py_expr = compile_python(True, state)
2138
        self.assertEquals(py_expr, "_0")
2139
        self.assertEquals(state.parameters, [True])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2140
2141
    def test_float(self):
304.2.4 by James Henstridge
Compile str, unicode, int, long, float and None to string
2142
        py_expr = compile_python(1.1)
2143
        self.assertEquals(py_expr, repr(1.1))
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2144
2145
    def test_datetime(self):
2146
        dt = datetime(1977, 5, 4, 12, 34)
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2147
        state = State()
2148
        py_expr = compile_python(dt, state)
2149
        self.assertEquals(py_expr, "_0")
2150
        self.assertEquals(state.parameters, [dt])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2151
2152
    def test_date(self):
2153
        d = date(1977, 5, 4)
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2154
        state = State()
2155
        py_expr = compile_python(d, state)
2156
        self.assertEquals(py_expr, "_0")
2157
        self.assertEquals(state.parameters, [d])
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2158
2159
    def test_time(self):
2160
        t = time(12, 34)
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2161
        state = State()
2162
        py_expr = compile_python(t, state)
2163
        self.assertEquals(py_expr, "_0")
2164
        self.assertEquals(state.parameters, [t])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2165
95.3.37 by Gustavo Niemeyer
Merging awesome changes from James' branch.
2166
    def test_timedelta(self):
2167
        td = timedelta(days=1, seconds=2, microseconds=3)
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2168
        state = State()
2169
        py_expr = compile_python(td, state)
2170
        self.assertEquals(py_expr, "_0")
2171
        self.assertEquals(state.parameters, [td])
95.3.37 by Gustavo Niemeyer
Merging awesome changes from James' branch.
2172
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2173
    def test_none(self):
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2174
        py_expr = compile_python(None)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2175
        self.assertEquals(py_expr, "None")
2176
2177
    def test_column(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2178
        expr = Column(column1)
304.2.5 by James Henstridge
Change the get_column() callback to take actual column objects rather
2179
        state = State()
2180
        py_expr = compile_python(expr, state)
2181
        self.assertEquals(py_expr, "get_column(_0)")
2182
        self.assertEquals(state.parameters, [expr])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2183
2184
    def test_column_table(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2185
        expr = Column(column1, table1)
304.2.5 by James Henstridge
Change the get_column() callback to take actual column objects rather
2186
        state = State()
2187
        py_expr = compile_python(expr, state)
2188
        self.assertEquals(py_expr, "get_column(_0)")
2189
        self.assertEquals(state.parameters, [expr])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2190
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2191
    def test_variable(self):
2192
        expr = Variable("value")
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2193
        state = State()
2194
        py_expr = compile_python(expr, state)
2195
        self.assertEquals(py_expr, "_0")
2196
        self.assertEquals(state.parameters, ["value"])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2197
2198
    def test_eq(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2199
        expr = Eq(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2200
        state = State()
2201
        py_expr = compile_python(expr, state)
2202
        self.assertEquals(py_expr, "_0 == _1")
2203
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2204
2205
    def test_ne(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2206
        expr = Ne(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2207
        state = State()
2208
        py_expr = compile_python(expr, state)
2209
        self.assertEquals(py_expr, "_0 != _1")
2210
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2211
2212
    def test_gt(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2213
        expr = Gt(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2214
        state = State()
2215
        py_expr = compile_python(expr, state)
2216
        self.assertEquals(py_expr, "_0 > _1")
2217
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2218
2219
    def test_ge(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2220
        expr = Ge(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2221
        state = State()
2222
        py_expr = compile_python(expr, state)
2223
        self.assertEquals(py_expr, "_0 >= _1")
2224
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2225
2226
    def test_lt(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2227
        expr = Lt(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2228
        state = State()
2229
        py_expr = compile_python(expr, state)
2230
        self.assertEquals(py_expr, "_0 < _1")
2231
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2232
2233
    def test_le(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2234
        expr = Le(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2235
        state = State()
2236
        py_expr = compile_python(expr, state)
2237
        self.assertEquals(py_expr, "_0 <= _1")
2238
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2239
2240
    def test_lshift(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2241
        expr = LShift(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2242
        state = State()
2243
        py_expr = compile_python(expr, state)
2244
        self.assertEquals(py_expr, "_0<<_1")
2245
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2246
2247
    def test_rshift(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2248
        expr = RShift(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2249
        state = State()
2250
        py_expr = compile_python(expr, state)
2251
        self.assertEquals(py_expr, "_0>>_1")
2252
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2253
2254
    def test_in(self):
58 by Gustavo Niemeyer
- Major refactoring of the typing system. Kinds are gone. Now Variables
2255
        expr = In(Variable(1), Variable(2))
304.2.3 by James Henstridge
Fix expr tests to pass with changes to compile_python.
2256
        state = State()
2257
        py_expr = compile_python(expr, state)
2258
        self.assertEquals(py_expr, "_0 in (_1,)")
2259
        self.assertEquals(state.parameters, [1, 2])
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2260
2261
    def test_and(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2262
        expr = And(elem1, elem2, And(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2263
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2264
        self.assertEquals(py_expr, "elem1 and elem2 and elem3 and elem4")
2265
2266
    def test_or(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2267
        expr = Or(elem1, elem2, Or(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2268
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2269
        self.assertEquals(py_expr, "elem1 or elem2 or elem3 or elem4")
2270
2271
    def test_add(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2272
        expr = Add(elem1, elem2, Add(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2273
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2274
        self.assertEquals(py_expr, "elem1+elem2+elem3+elem4")
2275
312.1.2 by Michael Hudson
add compile_python.when(Neg)
2276
    def test_neg(self):
2277
        expr = Neg(elem1)
2278
        py_expr = compile_python(expr)
2279
        self.assertEquals(py_expr, "-elem1")
2280
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2281
    def test_sub(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2282
        expr = Sub(elem1, Sub(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2283
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2284
        self.assertEquals(py_expr, "elem1-(elem2-elem3)")
2285
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2286
        expr = Sub(Sub(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2287
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2288
        self.assertEquals(py_expr, "elem1-elem2-elem3")
2289
2290
    def test_mul(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2291
        expr = Mul(elem1, elem2, Mul(elem3, elem4))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2292
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2293
        self.assertEquals(py_expr, "elem1*elem2*elem3*elem4")
2294
2295
    def test_div(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2296
        expr = Div(elem1, Div(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2297
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2298
        self.assertEquals(py_expr, "elem1/(elem2/elem3)")
2299
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2300
        expr = Div(Div(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2301
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2302
        self.assertEquals(py_expr, "elem1/elem2/elem3")
2303
2304
    def test_mod(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2305
        expr = Mod(elem1, Mod(elem2, elem3))
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2306
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2307
        self.assertEquals(py_expr, "elem1%(elem2%elem3)")
2308
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2309
        expr = Mod(Mod(elem1, elem2), elem3)
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2310
        py_expr = compile_python(expr)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2311
        self.assertEquals(py_expr, "elem1%elem2%elem3")
2312
2313
    def test_match(self):
95.3.14 by Gustavo Niemeyer
Changed the meaning of plain strings for the expression compiler. Now
2314
        col1 = Column(column1)
2315
        col2 = Column(column2)
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2316
142.1.7 by Gustavo Niemeyer
Sanitized return values from Compile. Now it always return only
2317
        match = compile_python.get_matcher((col1 > 10) & (col2 < 10))
43 by Gustavo Niemeyer
Implemented compiler that builds python expressions.
2318
304.2.5 by James Henstridge
Change the get_column() callback to take actual column objects rather
2319
        self.assertTrue(match({col1: 15, col2: 5}.get))
2320
        self.assertFalse(match({col1: 5, col2: 15}.get))
81.1.18 by Gustavo Niemeyer
- LazyValues don't need a constructor.
2321
304.2.6 by James Henstridge
Add a test to show that get_matcher() works for values whose repr() is
2322
    def test_match_bad_repr(self):
2323
        """The get_matcher() works for expressions containing values
2324
        whose repr is not valid Python syntax."""
2325
        class BadRepr(object):
2326
            def __repr__(self):
2327
                return "$Not a valid Python expression$"
2328
2329
        value = BadRepr()
2330
        col1 = Column(column1)
2331
        match = compile_python.get_matcher(col1 == Variable(value))
2332
        self.assertTrue(match({col1: value}.get))
2333
81.1.18 by Gustavo Niemeyer
- LazyValues don't need a constructor.
2334
2335
class LazyValueExprTest(TestHelper):
2336
2337
    def test_expr_is_lazy_value(self):
2338
        marker = object()
2339
        expr = SQL("Hullah!")
2340
        variable = Variable()
2341
        variable.set(expr)
2342
        self.assertTrue(variable.get(marker) is marker)