-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_converters.py
More file actions
276 lines (183 loc) · 7.81 KB
/
Copy pathtest_converters.py
File metadata and controls
276 lines (183 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from datetime import timedelta
from sqlobject.col import SODateTimeCol, SOTimeCol
from sqlobject.converters import registerConverter, sqlrepr, \
quote_str, unquote_str
from sqlobject.sqlbuilder import SQLExpression, SQLObjectField, \
Select, Insert, Update, Delete, Replace, \
SQLTrueClauseClass, SQLConstant, SQLPrefix, SQLCall, SQLOp, \
_LikeQuoted
class SOTestClass:
def __repr__(self):
return '<SOTestClass>'
def SOTestClassConverter(value, db):
return repr(value)
registerConverter(SOTestClass, SOTestClassConverter)
class NewSOTestClass:
__metaclass__ = type
def __repr__(self):
return '<NewSOTestClass>'
def NewSOTestClassConverter(value, db):
return repr(value)
registerConverter(NewSOTestClass, NewSOTestClassConverter)
def _sqlrepr(self, db):
return '<%s>' % self.__class__.__name__
SQLExpression.__sqlrepr__ = _sqlrepr
############################################################
# Tests
############################################################
def test_simple_string():
assert sqlrepr('A String', 'firebird') == "'A String'"
def test_string_newline():
assert sqlrepr('A String\nAnother', 'postgres') == "E'A String\\nAnother'"
assert sqlrepr('A String\nAnother', 'sqlite') == "'A String\nAnother'"
def test_string_tab():
assert sqlrepr('A String\tAnother', 'postgres') == "E'A String\\tAnother'"
def test_string_r():
assert sqlrepr('A String\rAnother', 'postgres') == "E'A String\\rAnother'"
def test_string_b():
assert sqlrepr('A String\bAnother', 'postgres') == "E'A String\\bAnother'"
def test_string_000():
assert sqlrepr('A String\000Another', 'postgres') == \
"E'A String\\0Another'"
def test_string_():
assert sqlrepr('A String\tAnother', 'postgres') == "E'A String\\tAnother'"
assert sqlrepr('A String\'Another', 'firebird') == "'A String''Another'"
def test_simple_unicode():
assert sqlrepr(u'A String', 'postgres') == "'A String'"
def test_integer():
assert sqlrepr(10) == "10"
def test_float():
assert sqlrepr(10.01) == "10.01"
def test_none():
assert sqlrepr(None) == "NULL"
def test_list():
assert sqlrepr(['one', 'two', 'three'], 'postgres') == \
"('one', 'two', 'three')"
def test_tuple():
assert sqlrepr(('one', 'two', 'three'), 'postgres') == \
"('one', 'two', 'three')"
def test_bool():
assert sqlrepr(True, 'postgres') == "'t'"
assert sqlrepr(False, 'postgres') == "'f'"
assert sqlrepr(True, 'mysql') == "1"
assert sqlrepr(False, 'mysql') == "0"
def test_datetime():
from datetime import datetime, date, time
if SODateTimeCol.datetimeFormat.find('.%f') > 0:
assert sqlrepr(datetime(2005, 7, 14, 13, 31, 2)) == \
"'2005-07-14 13:31:02.000000'"
else:
assert sqlrepr(datetime(2005, 7, 14, 13, 31, 2)) == \
"'2005-07-14 13:31:02'"
assert sqlrepr(date(2005, 7, 14)) == "'2005-07-14'"
if SOTimeCol.timeFormat.find('.%f') > 0:
assert sqlrepr(time(13, 31, 2)) == "'13:31:02.000000'"
else:
assert sqlrepr(time(13, 31, 2)) == "'13:31:02'"
# now dates before 1900
if SODateTimeCol.datetimeFormat.find('.%f') > 0:
assert sqlrepr(datetime(1428, 7, 14, 13, 31, 2)) == \
"'1428-07-14 13:31:02.000000'"
else:
assert sqlrepr(datetime(1428, 7, 14, 13, 31, 2)) == \
"'1428-07-14 13:31:02'"
assert sqlrepr(date(1428, 7, 14)) == "'1428-07-14'"
def test_instance():
instance = SOTestClass()
assert sqlrepr(instance) == repr(instance)
def test_newstyle():
instance = NewSOTestClass()
assert sqlrepr(instance) == repr(instance)
def test_sqlexpr():
instance = SQLExpression()
assert sqlrepr(instance) == repr(instance)
def test_sqlobjectfield():
instance = SQLObjectField('test', 'test', 'test', None, None)
assert sqlrepr(instance) == repr(instance)
def test_select():
instance = Select('test')
assert sqlrepr(instance, 'mysql') == "SELECT test"
def test_insert():
# Single column, no keyword arguments.
instance = Insert('test', [('test',)])
assert sqlrepr(instance, 'mysql') == "INSERT INTO test VALUES ('test')"
# Multiple columns, no keyword arguments.
instance2 = Insert('test', [('1st', '2nd', '3th', '4th')])
assert sqlrepr(instance2, 'postgres') == \
"INSERT INTO test VALUES ('1st', '2nd', '3th', '4th')"
# Multiple rows, multiple columns, "valueList" keyword argument.
instance3 = Insert('test',
valueList=[('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')])
assert sqlrepr(instance3, 'sqlite') == \
"INSERT INTO test VALUES ('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')"
# Multiple columns, "values" keyword argument.
instance4 = Insert('test', values=('v1', 'v2', 'v3'))
assert sqlrepr(instance4, 'mysql') == \
"INSERT INTO test VALUES ('v1', 'v2', 'v3')"
# Single column, "valueList" keyword argument.
instance5 = Insert('test', valueList=[('v1',)])
assert sqlrepr(instance5, 'mysql') == "INSERT INTO test VALUES ('v1')"
# Multiple rows, Multiple columns, template.
instance6 = Insert('test',
valueList=[('a1', 'b1'), ('a2', 'b2')],
template=['col1', 'col2'])
assert sqlrepr(instance6, 'mysql') == \
"INSERT INTO test (col1, col2) VALUES ('a1', 'b1'), ('a2', 'b2')"
# Multiple columns, implicit template (dictionary value).
instance7 = Insert('test', valueList=[{'col1': 'a1', 'col2': 'b1'}])
assert sqlrepr(instance7, 'mysql') == \
"INSERT INTO test (col1, col2) VALUES ('a1', 'b1')"
# Multiple rows, Multiple columns, implicit template.
instance8 = Insert('test', valueList=[{'col1': 'a1', 'col2': 'b1'},
{'col1': 'a2', 'col2': 'b2'}])
assert sqlrepr(instance8, 'mysql') == \
"INSERT INTO test (col1, col2) VALUES ('a1', 'b1'), ('a2', 'b2')"
def test_update():
instance = Update('test', {'test': 'test'})
assert sqlrepr(instance, 'mysql') == "UPDATE test SET test='test'"
def test_delete():
instance = Delete('test', None)
assert sqlrepr(instance, 'mysql') == "DELETE FROM test"
def test_replace():
instance = Replace('test', {'test': 'test'})
assert sqlrepr(instance, 'mysql') == "REPLACE test SET test='test'"
def test_trueclause():
instance = SQLTrueClauseClass()
assert sqlrepr(instance) == repr(instance)
def test_op():
instance = SQLOp('and', 'this', 'that')
assert sqlrepr(instance, 'mysql') == "(('this') AND ('that'))"
def test_call():
instance = SQLCall('test', ('test',))
assert sqlrepr(instance, 'mysql') == "'test'('test')"
def test_constant():
instance = SQLConstant('test')
assert sqlrepr(instance) == repr(instance)
def test_prefix():
instance = SQLPrefix('test', 'test')
assert sqlrepr(instance, 'mysql') == "test 'test'"
def test_dict():
assert sqlrepr({"key": "value"}, "sqlite") == "('key')"
def test_sets():
try:
set
except NameError:
pass
else:
assert sqlrepr(set([1])) == "(1)"
def test_timedelta():
assert sqlrepr(timedelta(seconds=30 * 60)) == \
"INTERVAL '0 days 1800 seconds'"
def test_quote_unquote_str():
assert quote_str('test%', 'postgres') == "'test%'"
assert quote_str('test%', 'sqlite') == "'test%'"
assert quote_str('test\\%', 'postgres') == "E'test\\%'"
assert quote_str('test\\%', 'sqlite') == "'test\\%'"
assert unquote_str("'test%'") == 'test%'
assert unquote_str("'test\\%'") == 'test\\%'
assert unquote_str("E'test\\%'") == 'test\\%'
def test_like_quoted():
assert sqlrepr(_LikeQuoted('test'), 'postgres') == "'test'"
assert sqlrepr(_LikeQuoted('test'), 'sqlite') == "'test'"
assert sqlrepr(_LikeQuoted('test%'), 'postgres') == r"E'test\\%'"
assert sqlrepr(_LikeQuoted('test%'), 'sqlite') == r"'test\%'"