-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_ForeignKey.py
More file actions
133 lines (104 loc) · 3.94 KB
/
test_ForeignKey.py
File metadata and controls
133 lines (104 loc) · 3.94 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
from formencode import validators
from sqlobject import ForeignKey, IntCol, SQLObject, StringCol
from sqlobject.tests.dbtest import getConnection, InstalledTestDatabase, \
raises, setupClass, setupCyclicClasses
class SOTestComposerKey(SQLObject):
name = StringCol()
id2 = IntCol(default=None, unique=True)
class SOTestWorkKey(SQLObject):
class sqlmeta:
idName = "work_id"
composer = ForeignKey('SOTestComposerKey', cascade=True)
title = StringCol()
class SOTestWorkKey2(SQLObject):
title = StringCol()
class SOTestOtherColumn(SQLObject):
key1 = ForeignKey('SOTestComposerKey', default=None)
key2 = ForeignKey('SOTestComposerKey', refColumn='id2', default=None)
def test1():
setupClass([SOTestComposerKey, SOTestWorkKey])
c = SOTestComposerKey(name='Mahler, Gustav')
w1 = SOTestWorkKey(composer=c, title='Symphony No. 9')
w2 = SOTestWorkKey(composer=None, title=None)
# Select by usual way
s = SOTestWorkKey.selectBy(composerID=c.id, title='Symphony No. 9')
assert s.count() == 1
assert s[0] == w1
# selectBy object.id
s = SOTestWorkKey.selectBy(composer=c.id, title='Symphony No. 9')
assert s.count() == 1
assert s[0] == w1
# selectBy object
s = SOTestWorkKey.selectBy(composer=c, title='Symphony No. 9')
assert s.count() == 1
assert s[0] == w1
# selectBy id
s = SOTestWorkKey.selectBy(id=w1.id)
assert s.count() == 1
assert s[0] == w1
# is None handled correctly?
s = SOTestWorkKey.selectBy(composer=None, title=None)
assert s.count() == 1
assert s[0] == w2
s = SOTestWorkKey.selectBy()
assert s.count() == 2
# select with objects
s = SOTestWorkKey.select(SOTestWorkKey.q.composerID == c.id)
assert s.count() == 1
assert s[0] == w1
s = SOTestWorkKey.select(SOTestWorkKey.q.composer == c.id)
assert s.count() == 1
assert s[0] == w1
s = SOTestWorkKey.select(SOTestWorkKey.q.composerID == c)
assert s.count() == 1
assert s[0] == w1
s = SOTestWorkKey.select(SOTestWorkKey.q.composer == c)
assert s.count() == 1
assert s[0] == w1
s = SOTestWorkKey.select(
(SOTestWorkKey.q.composer == c)
& (SOTestWorkKey.q.title == 'Symphony No. 9'))
assert s.count() == 1
assert s[0] == w1
def test2():
SOTestWorkKey._connection = getConnection()
InstalledTestDatabase.drop(SOTestWorkKey)
setupClass([SOTestComposerKey, SOTestWorkKey2], force=True)
SOTestWorkKey2.sqlmeta.addColumn(ForeignKey('SOTestComposerKey'),
changeSchema=True)
def test_otherColumn():
setupClass([SOTestComposerKey, SOTestOtherColumn])
test_composer1 = SOTestComposerKey(name='Test1')
test_composer2 = SOTestComposerKey(name='Test2', id2=2)
test_fkey = SOTestOtherColumn(key1=test_composer1)
test_other = SOTestOtherColumn(key2=test_composer2.id2)
getConnection().cache.clear()
assert test_fkey.key1 == test_composer1
assert test_other.key2 == test_composer2
class SOTestFKValidationA(SQLObject):
name = StringCol()
bfk = ForeignKey("SOTestFKValidationB")
cfk = ForeignKey("SOTestFKValidationC", default=None)
class SOTestFKValidationB(SQLObject):
name = StringCol()
afk = ForeignKey("SOTestFKValidationA")
class SOTestFKValidationC(SQLObject):
class sqlmeta:
idType = str
name = StringCol()
def test_foreignkey_validation():
setupCyclicClasses(SOTestFKValidationA, SOTestFKValidationB,
SOTestFKValidationC)
a = SOTestFKValidationA(name="testa", bfk=None)
b = SOTestFKValidationB(name="testb", afk=a)
c = SOTestFKValidationC(id='testc', name="testc")
a.bfk = b
a.cfk = c
assert a.bfk == b
assert a.cfk == c
assert b.afk == a
raises(validators.Invalid,
SOTestFKValidationA, name="testa", bfk='testb', cfk='testc')
a = SOTestFKValidationA(name="testa", bfk=1, cfk='testc')
assert a.bfkID == 1
assert a.cfkID == 'testc'