@@ -115,66 +115,98 @@ def test_key_completions(self):
115
115
116
116
def test_caching_on (self ):
117
117
# caching is turned on by default
118
+
119
+ # setup store
118
120
store = CountingDict ()
119
121
eq (0 , store .counter ['__getitem__' , 'attrs' ])
120
122
eq (0 , store .counter ['__setitem__' , 'attrs' ])
121
123
store ['attrs' ] = json .dumps (dict (foo = 'xxx' , bar = 42 )).encode ('ascii' )
122
124
eq (0 , store .counter ['__getitem__' , 'attrs' ])
123
125
eq (1 , store .counter ['__setitem__' , 'attrs' ])
126
+
127
+ # setup attributes
124
128
a = self .init_attributes (store )
129
+
130
+ # test __getitem__ causes all attributes to be cached
125
131
eq (a ['foo' ], 'xxx' )
126
132
eq (1 , store .counter ['__getitem__' , 'attrs' ])
127
133
eq (a ['bar' ], 42 )
128
134
eq (1 , store .counter ['__getitem__' , 'attrs' ])
129
135
eq (a ['foo' ], 'xxx' )
130
136
eq (1 , store .counter ['__getitem__' , 'attrs' ])
137
+
138
+ # test __setitem__ updates the cache
131
139
a ['foo' ] = 'yyy'
132
140
eq (2 , store .counter ['__getitem__' , 'attrs' ])
133
141
eq (2 , store .counter ['__setitem__' , 'attrs' ])
134
142
eq (a ['foo' ], 'yyy' )
135
143
eq (2 , store .counter ['__getitem__' , 'attrs' ])
136
144
eq (2 , store .counter ['__setitem__' , 'attrs' ])
145
+
146
+ # test update() updates the cache
137
147
a .update (foo = 'zzz' , bar = 84 )
138
148
eq (3 , store .counter ['__getitem__' , 'attrs' ])
139
149
eq (3 , store .counter ['__setitem__' , 'attrs' ])
140
150
eq (a ['foo' ], 'zzz' )
141
151
eq (a ['bar' ], 84 )
142
152
eq (3 , store .counter ['__getitem__' , 'attrs' ])
143
153
eq (3 , store .counter ['__setitem__' , 'attrs' ])
154
+
155
+ # test __contains__ uses the cache
144
156
assert 'foo' in a
145
157
eq (3 , store .counter ['__getitem__' , 'attrs' ])
146
158
eq (3 , store .counter ['__setitem__' , 'attrs' ])
147
159
assert 'spam' not in a
148
160
eq (3 , store .counter ['__getitem__' , 'attrs' ])
149
161
eq (3 , store .counter ['__setitem__' , 'attrs' ])
150
162
163
+ # test __delitem__ updates the cache
164
+ del a ['bar' ]
165
+ eq (4 , store .counter ['__getitem__' , 'attrs' ])
166
+ eq (4 , store .counter ['__setitem__' , 'attrs' ])
167
+ assert 'bar' not in a
168
+ eq (4 , store .counter ['__getitem__' , 'attrs' ])
169
+ eq (4 , store .counter ['__setitem__' , 'attrs' ])
170
+
151
171
def test_caching_off (self ):
172
+
173
+ # setup store
152
174
store = CountingDict ()
153
175
eq (0 , store .counter ['__getitem__' , 'attrs' ])
154
176
eq (0 , store .counter ['__setitem__' , 'attrs' ])
155
177
store ['attrs' ] = json .dumps (dict (foo = 'xxx' , bar = 42 )).encode ('ascii' )
156
178
eq (0 , store .counter ['__getitem__' , 'attrs' ])
157
179
eq (1 , store .counter ['__setitem__' , 'attrs' ])
180
+
181
+ # setup attributes
158
182
a = self .init_attributes (store , cache = False )
183
+
184
+ # test __getitem__
159
185
eq (a ['foo' ], 'xxx' )
160
186
eq (1 , store .counter ['__getitem__' , 'attrs' ])
161
187
eq (a ['bar' ], 42 )
162
188
eq (2 , store .counter ['__getitem__' , 'attrs' ])
163
189
eq (a ['foo' ], 'xxx' )
164
190
eq (3 , store .counter ['__getitem__' , 'attrs' ])
191
+
192
+ # test __setitem__
165
193
a ['foo' ] = 'yyy'
166
194
eq (4 , store .counter ['__getitem__' , 'attrs' ])
167
195
eq (2 , store .counter ['__setitem__' , 'attrs' ])
168
196
eq (a ['foo' ], 'yyy' )
169
197
eq (5 , store .counter ['__getitem__' , 'attrs' ])
170
198
eq (2 , store .counter ['__setitem__' , 'attrs' ])
199
+
200
+ # test update()
171
201
a .update (foo = 'zzz' , bar = 84 )
172
202
eq (6 , store .counter ['__getitem__' , 'attrs' ])
173
203
eq (3 , store .counter ['__setitem__' , 'attrs' ])
174
204
eq (a ['foo' ], 'zzz' )
175
205
eq (a ['bar' ], 84 )
176
206
eq (8 , store .counter ['__getitem__' , 'attrs' ])
177
207
eq (3 , store .counter ['__setitem__' , 'attrs' ])
208
+
209
+ # test __contains__
178
210
assert 'foo' in a
179
211
eq (9 , store .counter ['__getitem__' , 'attrs' ])
180
212
eq (3 , store .counter ['__setitem__' , 'attrs' ])
0 commit comments