-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_basic.py
More file actions
359 lines (280 loc) · 10.1 KB
/
test_basic.py
File metadata and controls
359 lines (280 loc) · 10.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import codecs
import pytest
from sqlobject import BoolCol, ForeignKey, IntCol, KeyCol, SQLObject, \
StringCol, connectionForURI, sqlhub
from sqlobject.tests.dbtest import inserts, raises, setupClass, supports
class SOTestSO1(SQLObject):
name = StringCol(length=50, dbName='name_col')
name.title = 'Your Name'
name.foobar = 1
passwd = StringCol(length=10)
class sqlmeta:
cacheValues = False
def _set_passwd(self, passwd):
self._SO_set_passwd(codecs.encode(passwd, 'rot13'))
def setupGetters(cls):
setupClass(cls)
inserts(cls, [('bob', 'god'), ('sally', 'sordid'),
('dave', 'dremel'), ('fred', 'forgo')],
'name passwd')
def test_case1():
setupGetters(SOTestSO1)
bob = SOTestSO1.selectBy(name='bob')[0]
assert bob.name == 'bob'
assert bob.passwd == codecs.encode('god', 'rot13')
bobs = SOTestSO1.selectBy(name='bob')[:10]
assert len(list(bobs)) == 1
def test_newline():
setupGetters(SOTestSO1)
bob = SOTestSO1.selectBy(name='bob')[0]
testString = 'hey\nyou\\can\'t you see me?\t'
bob.name = testString
bob.expire()
assert bob.name == testString
def test_count():
setupGetters(SOTestSO1)
assert SOTestSO1.selectBy(name=None).count() == 0
assert SOTestSO1.selectBy(name='bob').count() == 1
assert SOTestSO1.select(SOTestSO1.q.name == 'bob').count() == 1
assert SOTestSO1.select().count() == len(list(SOTestSO1.select()))
def test_getset():
setupGetters(SOTestSO1)
bob = SOTestSO1.selectBy(name='bob')[0]
assert bob.name == 'bob'
bob.name = 'joe'
assert bob.name == 'joe'
bob.set(name='joebob', passwd='testtest')
assert bob.name == 'joebob'
def test_extra_vars():
setupGetters(SOTestSO1)
col = SOTestSO1.sqlmeta.columns['name']
assert col.title == 'Your Name'
assert col.foobar == 1
assert getattr(SOTestSO1.sqlmeta.columns['passwd'], 'title', None) is None
class SOTestSO2(SQLObject):
name = StringCol(length=50, dbName='name_col')
passwd = StringCol(length=10)
def _set_passwd(self, passwd):
self._SO_set_passwd(codecs.encode(passwd, 'rot13'))
def test_case2():
setupGetters(SOTestSO2)
bob = SOTestSO2.selectBy(name='bob')[0]
assert bob.name == 'bob'
assert bob.passwd == codecs.encode('god', 'rot13')
class Student(SQLObject):
is_smart = BoolCol()
def test_boolCol():
setupClass(Student)
student = Student(is_smart=False)
assert not student.is_smart
student2 = Student(is_smart=1)
assert student2.is_smart
class SOTestSO3(SQLObject):
name = StringCol(length=10, dbName='name_col')
other = ForeignKey('SOTestSO4', default=None)
other2 = KeyCol(foreignKey='SOTestSO4', default=None)
class SOTestSO4(SQLObject):
me = StringCol(length=10)
def test_foreignKey():
setupClass([SOTestSO4, SOTestSO3])
test3_order = [col.name for col in SOTestSO3.sqlmeta.columnList]
assert test3_order == ['name', 'otherID', 'other2ID']
tc3 = SOTestSO3(name='a')
assert tc3.other is None
assert tc3.other2 is None
assert tc3.otherID is None
assert tc3.other2ID is None
tc4a = SOTestSO4(me='1')
tc3.other = tc4a
assert tc3.other == tc4a
assert tc3.otherID == tc4a.id
tc4b = SOTestSO4(me='2')
tc3.other = tc4b.id
assert tc3.other == tc4b
assert tc3.otherID == tc4b.id
tc4c = SOTestSO4(me='3')
tc3.other2 = tc4c
assert tc3.other2 == tc4c
assert tc3.other2ID == tc4c.id
tc4d = SOTestSO4(me='4')
tc3.other2 = tc4d.id
assert tc3.other2 == tc4d
assert tc3.other2ID == tc4d.id
tcc = SOTestSO3(name='b', other=tc4a)
assert tcc.other == tc4a
tcc2 = SOTestSO3(name='c', other=tc4a.id)
assert tcc2.other == tc4a
def test_selectBy():
setupClass([SOTestSO4, SOTestSO3])
tc4 = SOTestSO4(me='another')
tc3 = SOTestSO3(name='sel', other=tc4)
SOTestSO3(name='not joined')
assert tc3.other == tc4
assert list(SOTestSO3.selectBy(other=tc4)) == [tc3]
assert list(SOTestSO3.selectBy(otherID=tc4.id)) == [tc3]
assert SOTestSO3.selectBy(otherID=tc4.id)[0] == tc3
assert list(SOTestSO3.selectBy(otherID=tc4.id)[:10]) == [tc3]
assert list(SOTestSO3.selectBy(other=tc4)[:10]) == [tc3]
class SOTestSO5(SQLObject):
name = StringCol(length=10, dbName='name_col')
other = ForeignKey('SOTestSO6', default=None, cascade=True)
another = ForeignKey('SOTestSO7', default=None, cascade=True)
class SOTestSO6(SQLObject):
name = StringCol(length=10, dbName='name_col')
other = ForeignKey('SOTestSO7', default=None, cascade=True)
class SOTestSO7(SQLObject):
name = StringCol(length=10, dbName='name_col')
def test_foreignKeyDestroySelfCascade():
setupClass([SOTestSO7, SOTestSO6, SOTestSO5])
tc5 = SOTestSO5(name='a')
tc6a = SOTestSO6(name='1')
tc5.other = tc6a
tc7a = SOTestSO7(name='2')
tc6a.other = tc7a
tc5.another = tc7a
assert tc5.other == tc6a
assert tc5.otherID == tc6a.id
assert tc6a.other == tc7a
assert tc6a.otherID == tc7a.id
assert tc5.other.other == tc7a
assert tc5.other.otherID == tc7a.id
assert tc5.another == tc7a
assert tc5.anotherID == tc7a.id
assert tc5.other.other == tc5.another
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 1
assert SOTestSO7.select().count() == 1
tc6b = SOTestSO6(name='3')
tc6c = SOTestSO6(name='4')
tc7b = SOTestSO7(name='5')
tc6b.other = tc7b
tc6c.other = tc7b
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 3
assert SOTestSO7.select().count() == 2
tc6b.destroySelf()
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 2
assert SOTestSO7.select().count() == 2
tc7b.destroySelf()
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 1
assert SOTestSO7.select().count() == 1
tc7a.destroySelf()
assert SOTestSO5.select().count() == 0
assert SOTestSO6.select().count() == 0
assert SOTestSO7.select().count() == 0
def testForeignKeyDropTableCascade():
if not supports('dropTableCascade'):
pytest.skip("dropTableCascade isn't supported")
setupClass(SOTestSO7)
setupClass(SOTestSO6)
setupClass(SOTestSO5)
tc5a = SOTestSO5(name='a')
tc6a = SOTestSO6(name='1')
tc5a.other = tc6a
tc7a = SOTestSO7(name='2')
tc6a.other = tc7a
tc5a.another = tc7a
tc5b = SOTestSO5(name='b')
tc5c = SOTestSO5(name='c')
tc6b = SOTestSO6(name='3')
tc5c.other = tc6b
assert SOTestSO5.select().count() == 3
assert SOTestSO6.select().count() == 2
assert SOTestSO7.select().count() == 1
SOTestSO7.dropTable(cascade=True)
assert SOTestSO5.select().count() == 3
assert SOTestSO6.select().count() == 2
tc6a.destroySelf()
assert SOTestSO5.select().count() == 2
assert SOTestSO6.select().count() == 1
tc6b.destroySelf()
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 0
assert next(iter(SOTestSO5.select())) == tc5b
tc6c = SOTestSO6(name='3')
tc5b.other = tc6c
assert SOTestSO5.select().count() == 1
assert SOTestSO6.select().count() == 1
tc6c.destroySelf()
assert SOTestSO5.select().count() == 0
assert SOTestSO6.select().count() == 0
class SOTestSO8(SQLObject):
name = StringCol(length=10, dbName='name_col')
other = ForeignKey('SOTestSO9', default=None, cascade=False)
class SOTestSO9(SQLObject):
name = StringCol(length=10, dbName='name_col')
def testForeignKeyDestroySelfRestrict():
setupClass([SOTestSO9, SOTestSO8])
tc8a = SOTestSO8(name='a')
tc9a = SOTestSO9(name='1')
tc8a.other = tc9a
tc8b = SOTestSO8(name='b')
tc9b = SOTestSO9(name='2')
assert tc8a.other == tc9a
assert tc8a.otherID == tc9a.id
assert SOTestSO8.select().count() == 2
assert SOTestSO9.select().count() == 2
raises(Exception, tc9a.destroySelf)
tc9b.destroySelf()
assert SOTestSO8.select().count() == 2
assert SOTestSO9.select().count() == 1
tc8a.destroySelf()
tc8b.destroySelf()
tc9a.destroySelf()
assert SOTestSO8.select().count() == 0
assert SOTestSO9.select().count() == 0
class SOTestSO10(SQLObject):
name = StringCol()
class SOTestSO11(SQLObject):
name = StringCol()
other = ForeignKey('SOTestSO10', default=None, cascade='null')
def testForeignKeySetNull():
setupClass([SOTestSO10, SOTestSO11])
obj1 = SOTestSO10(name='foo')
obj2 = SOTestSO10(name='bar')
dep1 = SOTestSO11(name='xxx', other=obj1)
dep2 = SOTestSO11(name='yyy', other=obj1)
dep3 = SOTestSO11(name='zzz', other=obj2)
for name in 'xxx', 'yyy', 'zzz':
assert len(list(SOTestSO11.selectBy(name=name))) == 1
obj1.destroySelf()
for name in 'xxx', 'yyy', 'zzz':
assert len(list(SOTestSO11.selectBy(name=name))) == 1
assert dep1.other is None
assert dep2.other is None
assert dep3.other is obj2
def testAsDict():
setupGetters(SOTestSO1)
bob = SOTestSO1.selectBy(name='bob')[0]
assert bob.sqlmeta.asDict() == {
'passwd': 'tbq', 'name': 'bob', 'id': bob.id}
def test_nonexisting_attr():
setupClass(Student)
raises(AttributeError, getattr, Student.q, 'nonexisting')
class SOTestSO12(SQLObject):
name = StringCol()
so_value = IntCol(defaultSQL='1')
def test_defaultSQL():
setupClass(SOTestSO12)
test = SOTestSO12(name="test")
assert test.so_value == 1
def test_connection_override():
sqlhub.processConnection = connectionForURI('sqlite:///db1')
class SOTestSO13(SQLObject):
_connection = connectionForURI('sqlite:///db2')
assert SOTestSO13._connection.uri() == 'sqlite:///db2'
del sqlhub.processConnection
def _test_wrong_sqlmeta_idType():
class SOTestSO13(SQLObject):
class sqlmeta:
idType = dict
def test_wrong_sqlmeta_idType():
pytest.raises(TypeError, _test_wrong_sqlmeta_idType)
def _test_wrong_sqlmeta_idSize():
class SOTestSO14(SQLObject):
class sqlmeta:
idSize = 'DEFAULT'
def test_wrong_sqlmeta_idSize():
pytest.raises(ValueError, _test_wrong_sqlmeta_idSize)