-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_lazy.py
More file actions
165 lines (149 loc) · 5.41 KB
/
Copy pathtest_lazy.py
File metadata and controls
165 lines (149 loc) · 5.41 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
from sqlobject import SQLObject, StringCol
from sqlobject.tests.dbtest import setupClass
########################################
# Lazy updates
########################################
class Lazy(SQLObject):
class sqlmeta:
lazyUpdate = True
name = StringCol()
other = StringCol(default='nothing')
third = StringCol(default='third')
class TestLazyTest:
def setup_method(self, meth):
# All this stuff is so that we can track when the connection
# does an actual update; we put in a new _SO_update method
# that calls the original and sets an instance variable that
# we can later check.
setupClass(Lazy)
self.conn = Lazy._connection
self.conn.didUpdate = False
self._oldUpdate = self.conn._SO_update
newUpdate = (
lambda so, values, s=self, c=self.conn, o=self._oldUpdate:
self._alternateUpdate(so, values, c, o))
self.conn._SO_update = newUpdate
def teardown_method(self, meth):
self.conn._SO_update = self._oldUpdate
del self._oldUpdate
def _alternateUpdate(self, so, values, conn, oldUpdate):
conn.didUpdate = True
return oldUpdate(so, values)
def test_lazy(self):
assert not self.conn.didUpdate
obj = Lazy(name='tim')
# We just did an insert, but not an update:
assert not self.conn.didUpdate
obj.set(name='joe')
assert obj.sqlmeta.dirty
assert obj.name == 'joe'
assert not self.conn.didUpdate
obj.syncUpdate()
assert obj.name == 'joe'
assert self.conn.didUpdate
assert not obj.sqlmeta.dirty
assert obj.name == 'joe'
self.conn.didUpdate = False
obj = Lazy(name='frank')
obj.name = 'joe'
assert not self.conn.didUpdate
assert obj.sqlmeta.dirty
assert obj.name == 'joe'
obj.name = 'joe2'
assert not self.conn.didUpdate
assert obj.sqlmeta.dirty
assert obj.name == 'joe2'
obj.syncUpdate()
assert obj.name == 'joe2'
assert not obj.sqlmeta.dirty
assert self.conn.didUpdate
self.conn.didUpdate = False
obj = Lazy(name='loaded')
assert not obj.sqlmeta.dirty
assert not self.conn.didUpdate
assert obj.name == 'loaded'
obj.name = 'unloaded'
assert obj.sqlmeta.dirty
assert obj.name == 'unloaded'
assert not self.conn.didUpdate
obj.sync()
assert not obj.sqlmeta.dirty
assert obj.name == 'unloaded'
assert self.conn.didUpdate
self.conn.didUpdate = False
obj.name = 'whatever'
assert obj.sqlmeta.dirty
assert obj.name == 'whatever'
assert not self.conn.didUpdate
obj._SO_loadValue('name')
assert obj.sqlmeta.dirty
assert obj.name == 'whatever'
assert not self.conn.didUpdate
obj._SO_loadValue('other')
assert obj.name == 'whatever'
assert not self.conn.didUpdate
obj.syncUpdate()
assert self.conn.didUpdate
self.conn.didUpdate = False
# Now, check that get() doesn't screw
# cached objects' validator state.
obj_id = obj.id
old_state = obj._SO_validatorState
obj = Lazy.get(obj_id)
assert not obj.sqlmeta.dirty
assert not self.conn.didUpdate
assert obj._SO_validatorState is old_state
assert obj.name == 'whatever'
obj.name = 'unloaded'
assert obj.name == 'unloaded'
assert obj.sqlmeta.dirty
assert not self.conn.didUpdate
# Fetch the object again with get() and
# make sure sqlmeta.dirty is still set, as the
# object should come from the cache.
obj = Lazy.get(obj_id)
assert obj.sqlmeta.dirty
assert not self.conn.didUpdate
assert obj.name == 'unloaded'
obj.syncUpdate()
assert self.conn.didUpdate
assert not obj.sqlmeta.dirty
self.conn.didUpdate = False
# Then clear the cache, and try a get()
# again, to make sure stuf like _SO_createdValues
# is properly initialized.
self.conn.cache.clear()
obj = Lazy.get(obj_id)
assert not obj.sqlmeta.dirty
assert not self.conn.didUpdate
assert obj.name == 'unloaded'
obj.name = 'spongebob'
assert obj.name == 'spongebob'
assert obj.sqlmeta.dirty
assert not self.conn.didUpdate
obj.syncUpdate()
assert self.conn.didUpdate
assert not obj.sqlmeta.dirty
self.conn.didUpdate = False
obj = Lazy(name='last')
assert not obj.sqlmeta.dirty
obj.syncUpdate()
assert not self.conn.didUpdate
assert not obj.sqlmeta.dirty
# Check that setting multiple values
# actually works. This was broken
# and just worked because we were testing
# only one value at a time, so 'name'
# had the right value after the for loop *wink*
# Also, check that passing a name that is not
# a valid column doesn't break, but instead
# just does a plain setattr.
obj.set(name='first', other='who', third='yes')
assert obj.name == 'first'
assert obj.other == 'who'
assert obj.third == 'yes'
assert obj.sqlmeta.dirty
assert not self.conn.didUpdate
obj.syncUpdate()
assert self.conn.didUpdate
assert not obj.sqlmeta.dirty