1
from sqlobject import *
2
from sqlobject.tests.dbtest import *
4
class TestSO1(SQLObject):
6
name = StringCol(length=50, dbName='name_col')
7
name.title = 'Your Name'
9
passwd = StringCol(length=10)
14
def _set_passwd(self, passwd):
15
self._SO_set_passwd(passwd.encode('rot13'))
17
def setupGetters(cls):
19
inserts(cls, [('bob', 'god'), ('sally', 'sordid'),
20
('dave', 'dremel'), ('fred', 'forgo')],
25
bob = TestSO1.selectBy(name='bob')[0]
26
assert bob.name == 'bob'
27
assert bob.passwd == 'god'.encode('rot13')
28
bobs = TestSO1.selectBy(name='bob')[:10]
29
assert len(list(bobs)) == 1
33
bob = TestSO1.selectBy(name='bob')[0]
34
testString = 'hey\nyou\\can\'t you see me?\t'
37
assert bob.name == testString
41
assert TestSO1.selectBy(name=None).count() == 0
42
assert TestSO1.selectBy(name='bob').count() == 1
43
assert TestSO1.select(TestSO1.q.name == 'bob').count() == 1
44
assert TestSO1.select().count() == len(list(TestSO1.select()))
48
bob = TestSO1.selectBy(name='bob')[0]
49
assert bob.name == 'bob'
51
assert bob.name == 'joe'
52
bob.set(name='joebob', passwd='testtest')
53
assert bob.name == 'joebob'
55
def test_extra_vars():
57
col = TestSO1.sqlmeta.columns['name']
58
assert col.title == 'Your Name'
59
assert col.foobar == 1
60
assert getattr(TestSO1.sqlmeta.columns['passwd'], 'title', None) is None
62
class TestSO2(SQLObject):
63
name = StringCol(length=50, dbName='name_col')
64
passwd = StringCol(length=10)
66
def _set_passwd(self, passwd):
67
self._SO_set_passwd(passwd.encode('rot13'))
71
bob = TestSO2.selectBy(name='bob')[0]
72
assert bob.name == 'bob'
73
assert bob.passwd == 'god'.encode('rot13')
75
class Student(SQLObject):
80
student = Student(is_smart=False)
81
assert student.is_smart == False
82
student2 = Student(is_smart='false')
83
assert student2.is_smart == True
85
class TestSO3(SQLObject):
86
name = StringCol(length=10, dbName='name_col')
87
other = ForeignKey('TestSO4', default=None)
88
other2 = KeyCol(foreignKey='TestSO4', default=None)
90
class TestSO4(SQLObject):
91
me = StringCol(length=10)
93
def test_foreignKey():
94
setupClass([TestSO4, TestSO3])
95
test3_order = [col.name for col in TestSO3.sqlmeta.columnList]
96
assert test3_order == ['name', 'otherID', 'other2ID']
97
tc3 = TestSO3(name='a')
98
assert tc3.other is None
99
assert tc3.other2 is None
100
assert tc3.otherID is None
101
assert tc3.other2ID is None
102
tc4a = TestSO4(me='1')
104
assert tc3.other == tc4a
105
assert tc3.otherID == tc4a.id
106
tc4b = TestSO4(me='2')
108
assert tc3.other == tc4b
109
assert tc3.otherID == tc4b.id
110
tc4c = TestSO4(me='3')
112
assert tc3.other2 == tc4c
113
assert tc3.other2ID == tc4c.id
114
tc4d = TestSO4(me='4')
116
assert tc3.other2 == tc4d
117
assert tc3.other2ID == tc4d.id
118
tcc = TestSO3(name='b', other=tc4a)
119
assert tcc.other == tc4a
120
tcc2 = TestSO3(name='c', other=tc4a.id)
121
assert tcc2.other == tc4a
124
setupClass([TestSO4, TestSO3])
125
tc4 = TestSO4(me='another')
126
tc3 = TestSO3(name='sel', other=tc4)
127
anothertc3 = TestSO3(name='not joined')
128
assert tc3.other == tc4
129
assert list(TestSO3.selectBy(other=tc4)) == [tc3]
130
assert list(TestSO3.selectBy(otherID=tc4.id)) == [tc3]
131
assert TestSO3.selectBy(otherID=tc4.id)[0] == tc3
132
assert list(TestSO3.selectBy(otherID=tc4.id)[:10]) == [tc3]
133
assert list(TestSO3.selectBy(other=tc4)[:10]) == [tc3]
135
class TestSO5(SQLObject):
136
name = StringCol(length=10, dbName='name_col')
137
other = ForeignKey('TestSO6', default=None, cascade=True)
138
another = ForeignKey('TestSO7', default=None, cascade=True)
140
class TestSO6(SQLObject):
141
name = StringCol(length=10, dbName='name_col')
142
other = ForeignKey('TestSO7', default=None, cascade=True)
144
class TestSO7(SQLObject):
145
name = StringCol(length=10, dbName='name_col')
147
def test_foreignKeyDestroySelfCascade():
148
setupClass([TestSO7, TestSO6, TestSO5])
150
tc5 = TestSO5(name='a')
151
tc6a = TestSO6(name='1')
153
tc7a = TestSO7(name='2')
156
assert tc5.other == tc6a
157
assert tc5.otherID == tc6a.id
158
assert tc6a.other == tc7a
159
assert tc6a.otherID == tc7a.id
160
assert tc5.other.other == tc7a
161
assert tc5.other.otherID == tc7a.id
162
assert tc5.another == tc7a
163
assert tc5.anotherID == tc7a.id
164
assert tc5.other.other == tc5.another
165
assert TestSO5.select().count() == 1
166
assert TestSO6.select().count() == 1
167
assert TestSO7.select().count() == 1
168
tc6b = TestSO6(name='3')
169
tc6c = TestSO6(name='4')
170
tc7b = TestSO7(name='5')
173
assert TestSO5.select().count() == 1
174
assert TestSO6.select().count() == 3
175
assert TestSO7.select().count() == 2
177
assert TestSO5.select().count() == 1
178
assert TestSO6.select().count() == 2
179
assert TestSO7.select().count() == 2
181
assert TestSO5.select().count() == 1
182
assert TestSO6.select().count() == 1
183
assert TestSO7.select().count() == 1
185
assert TestSO5.select().count() == 0
186
assert TestSO6.select().count() == 0
187
assert TestSO7.select().count() == 0
189
def testForeignKeyDropTableCascade():
190
if not supports('dropTableCascade'):
196
tc5a = TestSO5(name='a')
197
tc6a = TestSO6(name='1')
199
tc7a = TestSO7(name='2')
202
tc5b = TestSO5(name='b')
203
tc5c = TestSO5(name='c')
204
tc6b = TestSO6(name='3')
206
assert TestSO5.select().count() == 3
207
assert TestSO6.select().count() == 2
208
assert TestSO7.select().count() == 1
209
TestSO7.dropTable(cascade=True)
210
assert TestSO5.select().count() == 3
211
assert TestSO6.select().count() == 2
213
assert TestSO5.select().count() == 2
214
assert TestSO6.select().count() == 1
216
assert TestSO5.select().count() == 1
217
assert TestSO6.select().count() == 0
218
assert iter(TestSO5.select()).next() == tc5b
219
tc6c = TestSO6(name='3')
221
assert TestSO5.select().count() == 1
222
assert TestSO6.select().count() == 1
224
assert TestSO5.select().count() == 0
225
assert TestSO6.select().count() == 0
227
class TestSO8(SQLObject):
228
name = StringCol(length=10, dbName='name_col')
229
other = ForeignKey('TestSO9', default=None, cascade=False)
231
class TestSO9(SQLObject):
232
name = StringCol(length=10, dbName='name_col')
234
def testForeignKeyDestroySelfRestrict():
235
setupClass([TestSO9, TestSO8])
237
tc8a = TestSO8(name='a')
238
tc9a = TestSO9(name='1')
240
tc8b = TestSO8(name='b')
241
tc9b = TestSO9(name='2')
242
assert tc8a.other == tc9a
243
assert tc8a.otherID == tc9a.id
244
assert TestSO8.select().count() == 2
245
assert TestSO9.select().count() == 2
246
raises(Exception, tc9a.destroySelf)
248
assert TestSO8.select().count() == 2
249
assert TestSO9.select().count() == 1
253
assert TestSO8.select().count() == 0
254
assert TestSO9.select().count() == 0
256
class TestSO10(SQLObject):
259
class TestSO11(SQLObject):
261
other = ForeignKey('TestSO10', default=None, cascade='null')
263
def testForeignKeySetNull():
264
setupClass([TestSO10, TestSO11])
265
obj1 = TestSO10(name='foo')
266
obj2 = TestSO10(name='bar')
267
dep1 = TestSO11(name='xxx', other=obj1)
268
dep2 = TestSO11(name='yyy', other=obj1)
269
dep3 = TestSO11(name='zzz', other=obj2)
270
for name in 'xxx', 'yyy', 'zzz':
271
assert len(list(TestSO11.selectBy(name=name))) == 1
273
for name in 'xxx', 'yyy', 'zzz':
274
assert len(list(TestSO11.selectBy(name=name))) == 1
275
assert dep1.other is None
276
assert dep2.other is None
277
assert dep3.other is obj2
280
setupGetters(TestSO1)
281
bob = TestSO1.selectBy(name='bob')[0]
282
assert bob.sqlmeta.asDict() == {
283
'passwd': 'tbq', 'name': 'bob', 'id': bob.id}
285
def test_nonexisting_attr():
288
Student.select(Student.q.nonexisting)
289
except AttributeError:
292
assert 0, "Expected an AttributeError"