~romaia/stoqlib/storm-old

« back to all changes in this revision

Viewing changes to external/sqlobject/tests/test_basic.py

  • Committer: jdahlin
  • Date: 2006-09-18 16:01:22 UTC
  • Revision ID: vcs-imports@canonical.com-20060918160122-umda7g8dx7bdc53m
Add an external directory with formencode and SQLObject forks, remove SQLObject dependency from documentation and setup.py. Install externals and set sys.path in stoqlib/__init__.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from sqlobject import *
 
2
from sqlobject.tests.dbtest import *
 
3
 
 
4
class TestSO1(SQLObject):
 
5
 
 
6
    name = StringCol(length=50, dbName='name_col')
 
7
    name.title = 'Your Name'
 
8
    name.foobar = 1
 
9
    passwd = StringCol(length=10)
 
10
 
 
11
    class sqlmeta:
 
12
        cacheValues = False
 
13
 
 
14
    def _set_passwd(self, passwd):
 
15
        self._SO_set_passwd(passwd.encode('rot13'))
 
16
 
 
17
def setupGetters(cls):
 
18
    setupClass(cls)
 
19
    inserts(cls, [('bob', 'god'), ('sally', 'sordid'),
 
20
                  ('dave', 'dremel'), ('fred', 'forgo')],
 
21
            'name passwd')
 
22
 
 
23
def test_case1():
 
24
    setupGetters(TestSO1)
 
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
 
30
 
 
31
def test_newline():
 
32
    setupGetters(TestSO1)
 
33
    bob = TestSO1.selectBy(name='bob')[0]
 
34
    testString = 'hey\nyou\\can\'t you see me?\t'
 
35
    bob.name = testString
 
36
    bob.expire()
 
37
    assert bob.name == testString
 
38
 
 
39
def test_count():
 
40
    setupGetters(TestSO1)
 
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()))
 
45
 
 
46
def test_getset():
 
47
    setupGetters(TestSO1)
 
48
    bob = TestSO1.selectBy(name='bob')[0]
 
49
    assert bob.name == 'bob'
 
50
    bob.name = 'joe'
 
51
    assert bob.name == 'joe'
 
52
    bob.set(name='joebob', passwd='testtest')
 
53
    assert bob.name == 'joebob'
 
54
 
 
55
def test_extra_vars():
 
56
    setupGetters(TestSO1)
 
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
 
61
 
 
62
class TestSO2(SQLObject):
 
63
    name = StringCol(length=50, dbName='name_col')
 
64
    passwd = StringCol(length=10)
 
65
 
 
66
    def _set_passwd(self, passwd):
 
67
        self._SO_set_passwd(passwd.encode('rot13'))
 
68
 
 
69
def test_case2():
 
70
    setupGetters(TestSO2)
 
71
    bob = TestSO2.selectBy(name='bob')[0]
 
72
    assert bob.name == 'bob'
 
73
    assert bob.passwd == 'god'.encode('rot13')
 
74
 
 
75
class Student(SQLObject):
 
76
    is_smart = BoolCol()
 
77
 
 
78
def test_boolCol():
 
79
    setupClass(Student)
 
80
    student = Student(is_smart=False)
 
81
    assert student.is_smart == False
 
82
    student2 = Student(is_smart='false')
 
83
    assert student2.is_smart == True
 
84
 
 
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)
 
89
 
 
90
class TestSO4(SQLObject):
 
91
    me = StringCol(length=10)
 
92
 
 
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')
 
103
    tc3.other = tc4a
 
104
    assert tc3.other == tc4a
 
105
    assert tc3.otherID == tc4a.id
 
106
    tc4b = TestSO4(me='2')
 
107
    tc3.other = tc4b.id
 
108
    assert tc3.other == tc4b
 
109
    assert tc3.otherID == tc4b.id
 
110
    tc4c = TestSO4(me='3')
 
111
    tc3.other2 = tc4c
 
112
    assert tc3.other2 == tc4c
 
113
    assert tc3.other2ID == tc4c.id
 
114
    tc4d = TestSO4(me='4')
 
115
    tc3.other2 = tc4d.id
 
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
 
122
 
 
123
def test_selectBy():
 
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]
 
134
 
 
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)
 
139
 
 
140
class TestSO6(SQLObject):
 
141
    name = StringCol(length=10, dbName='name_col')
 
142
    other = ForeignKey('TestSO7', default=None, cascade=True)
 
143
 
 
144
class TestSO7(SQLObject):
 
145
    name = StringCol(length=10, dbName='name_col')
 
146
 
 
147
def test_foreignKeyDestroySelfCascade():
 
148
    setupClass([TestSO7, TestSO6, TestSO5])
 
149
 
 
150
    tc5 = TestSO5(name='a')
 
151
    tc6a = TestSO6(name='1')
 
152
    tc5.other = tc6a
 
153
    tc7a = TestSO7(name='2')
 
154
    tc6a.other = tc7a
 
155
    tc5.another = tc7a
 
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')
 
171
    tc6b.other = tc7b
 
172
    tc6c.other = tc7b
 
173
    assert TestSO5.select().count() == 1
 
174
    assert TestSO6.select().count() == 3
 
175
    assert TestSO7.select().count() == 2
 
176
    tc6b.destroySelf()
 
177
    assert TestSO5.select().count() == 1
 
178
    assert TestSO6.select().count() == 2
 
179
    assert TestSO7.select().count() == 2
 
180
    tc7b.destroySelf()
 
181
    assert TestSO5.select().count() == 1
 
182
    assert TestSO6.select().count() == 1
 
183
    assert TestSO7.select().count() == 1
 
184
    tc7a.destroySelf()
 
185
    assert TestSO5.select().count() == 0
 
186
    assert TestSO6.select().count() == 0
 
187
    assert TestSO7.select().count() == 0
 
188
 
 
189
def testForeignKeyDropTableCascade():
 
190
    if not supports('dropTableCascade'):
 
191
        return
 
192
    setupClass(TestSO7)
 
193
    setupClass(TestSO6)
 
194
    setupClass(TestSO5)
 
195
 
 
196
    tc5a = TestSO5(name='a')
 
197
    tc6a = TestSO6(name='1')
 
198
    tc5a.other = tc6a
 
199
    tc7a = TestSO7(name='2')
 
200
    tc6a.other = tc7a
 
201
    tc5a.another = tc7a
 
202
    tc5b = TestSO5(name='b')
 
203
    tc5c = TestSO5(name='c')
 
204
    tc6b = TestSO6(name='3')
 
205
    tc5c.other = tc6b
 
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
 
212
    tc6a.destroySelf()
 
213
    assert TestSO5.select().count() == 2
 
214
    assert TestSO6.select().count() == 1
 
215
    tc6b.destroySelf()
 
216
    assert TestSO5.select().count() == 1
 
217
    assert TestSO6.select().count() == 0
 
218
    assert iter(TestSO5.select()).next() == tc5b
 
219
    tc6c = TestSO6(name='3')
 
220
    tc5b.other = tc6c
 
221
    assert TestSO5.select().count() == 1
 
222
    assert TestSO6.select().count() == 1
 
223
    tc6c.destroySelf()
 
224
    assert TestSO5.select().count() == 0
 
225
    assert TestSO6.select().count() == 0
 
226
 
 
227
class TestSO8(SQLObject):
 
228
    name = StringCol(length=10, dbName='name_col')
 
229
    other = ForeignKey('TestSO9', default=None, cascade=False)
 
230
 
 
231
class TestSO9(SQLObject):
 
232
    name = StringCol(length=10, dbName='name_col')
 
233
 
 
234
def testForeignKeyDestroySelfRestrict():
 
235
    setupClass([TestSO9, TestSO8])
 
236
 
 
237
    tc8a = TestSO8(name='a')
 
238
    tc9a = TestSO9(name='1')
 
239
    tc8a.other = tc9a
 
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)
 
247
    tc9b.destroySelf()
 
248
    assert TestSO8.select().count() == 2
 
249
    assert TestSO9.select().count() == 1
 
250
    tc8a.destroySelf()
 
251
    tc8b.destroySelf()
 
252
    tc9a.destroySelf()
 
253
    assert TestSO8.select().count() == 0
 
254
    assert TestSO9.select().count() == 0
 
255
 
 
256
class TestSO10(SQLObject):
 
257
    name = StringCol()
 
258
 
 
259
class TestSO11(SQLObject):
 
260
    name = StringCol()
 
261
    other = ForeignKey('TestSO10', default=None, cascade='null')
 
262
 
 
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
 
272
    obj1.destroySelf()
 
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
 
278
 
 
279
def testAsDict():
 
280
    setupGetters(TestSO1)
 
281
    bob = TestSO1.selectBy(name='bob')[0]
 
282
    assert bob.sqlmeta.asDict() == {
 
283
        'passwd': 'tbq', 'name': 'bob', 'id': bob.id}
 
284
 
 
285
def test_nonexisting_attr():
 
286
    setupClass(Student)
 
287
    try:
 
288
        Student.select(Student.q.nonexisting)
 
289
    except AttributeError:
 
290
        pass
 
291
    else:
 
292
        assert 0, "Expected an AttributeError"