/
test_persistence.py
322 lines (233 loc) · 8.86 KB
/
test_persistence.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# stdlib imports
import unittest
import warnings
import persistent
from persistent import Persistent
from persistent.wref import WeakRef as PWeakRef
from nti.externalization.persistence import PersistentExternalizableList
from nti.externalization.persistence import PersistentExternalizableDictionary
from nti.externalization.persistence import PersistentExternalizableWeakList
from nti.externalization.persistence import getPersistentState
from nti.externalization.persistence import setPersistentStateChanged
from nti.externalization.persistence import NoPickle
from nti.externalization.tests import ExternalizationLayerTest
from hamcrest import assert_that
from hamcrest import calling
from hamcrest import is_
from hamcrest import is_not
from hamcrest import raises
from hamcrest import has_length
# disable: accessing protected members, too many methods
# pylint: disable=W0212,R0904
class TestPersistentExternalizableList(ExternalizationLayerTest):
def test_externalize(self):
obj = PersistentExternalizableList([1, 2, None, 3])
assert_that(obj.toExternalList(), is_([1, 2, 3]))
assert_that(obj.toExternalList(), is_(list))
def test_values(self):
obj = PersistentExternalizableList([1, 2, None, 3])
assert_that(obj.values(), is_(obj))
assert_that(list(iter(obj.values())), is_([1, 2, None, 3]))
class TestPersistentExternalizableWeakList(ExternalizationLayerTest):
def test_mutate(self):
obj = PersistentExternalizableWeakList()
# Cannot set non-persistent objects
assert_that(calling(obj.append).with_args(object()),
raises(AttributeError))
pers = Persistent()
obj.append(pers)
assert_that(obj[0], is_(pers))
pers2 = Persistent()
obj[0] = pers2
assert_that(obj[0], is_(pers2))
assert_that(obj.count(pers2), is_(1))
assert_that(obj.count(pers), is_(0))
# iteration
for x in obj:
assert_that(x, is_(pers2))
assert_that(obj.index(pers2), is_(0))
assert_that(obj.pop(), is_(pers2))
assert_that(calling(obj.pop), raises(IndexError))
assert_that(obj, is_(obj))
obj.append(pers2)
# mul
assert_that(obj * 2,
is_(PersistentExternalizableWeakList([pers2, pers2])))
# imul
obj *= 2
assert_that(obj, is_(PersistentExternalizableWeakList([pers2, pers2])))
obj.pop()
# insert
obj.insert(1, pers2)
assert_that(obj, is_(PersistentExternalizableWeakList([pers2, pers2])))
assert_that(obj, is_([pers2, pers2]))
assert_that(obj, is_not([pers2, pers]))
assert_that(obj, is_not(pers))
obj.remove(pers2)
obj.remove(pers2)
assert_that(obj, is_([]))
def test_equality(self):
obj = PersistentExternalizableWeakList()
pers = Persistent()
obj.append(pers)
assert_that(obj, is_([pers]))
assert_that(obj, is_not([pers, pers]))
assert_that(obj, is_not([]))
assert_that(obj, is_not([self]))
class TestPersistentExternalizableDict(unittest.TestCase):
def test_to_external_dict(self):
class Obj(object):
def toExternalObject(self, **kw):
return "hi"
d = PersistentExternalizableDictionary(key=Obj())
assert_that(d.toExternalDictionary(), is_({'key': 'hi'}))
# Now minimal
d.__external_use_minimal_base__ = True
assert_that(d.toExternalDictionary(), is_({'key': 'hi'}))
class TestGetPersistentState(unittest.TestCase):
def test_without_jar(self):
class P(object):
_p_state = persistent.UPTODATE
_p_jar = None
assert_that(getPersistentState(P), is_(persistent.CHANGED))
def test_with_proxy_p_changed(self):
from zope.proxy import ProxyBase
class P(object):
_p_state = persistent.UPTODATE
_p_jar = None
class MyProxy(ProxyBase):
@property
def _p_changed(self):
raise AttributeError()
_p_state = _p_changed
proxy = MyProxy(P())
assert_that(getPersistentState(proxy), is_(persistent.CHANGED))
setPersistentStateChanged(proxy) # Does nothing
def test_with_proxy_p_state(self):
from zope.proxy import ProxyBase
class P(object):
_p_state = persistent.CHANGED
_p_jar = None
class MyProxy(ProxyBase):
@property
def _p_state(self):
raise AttributeError()
proxy = MyProxy(P())
assert_that(getPersistentState(proxy), is_(persistent.CHANGED))
setPersistentStateChanged(proxy) # Does nothing
class TestWeakRef(unittest.TestCase):
def test_to_externalObject(self):
class P(Persistent):
def toExternalObject(self, **kwargs):
return {'a': 42}
wref = PWeakRef(P())
assert_that(wref.toExternalObject(), is_({'a': 42}))
def test_to_externalOID(self):
class P(Persistent):
def toExternalOID(self, **kwargs):
return b'abc'
wref = PWeakRef(P())
assert_that(wref.toExternalOID(), is_(b'abc'))
with warnings.catch_warnings():
warnings.simplefilter('ignore')
@NoPickle
class GlobalPersistentNoPickle(Persistent):
pass
class GlobalSubclassPersistentNoPickle(GlobalPersistentNoPickle):
pass
@NoPickle
class GlobalNoPickle(object):
pass
class GlobalSubclassNoPickle(GlobalNoPickle):
pass
class GlobalNoPicklePersistentMixin1(GlobalNoPickle,
Persistent):
pass
class GlobalNoPicklePersistentMixin2(Persistent,
GlobalNoPickle):
pass
class GlobalNoPicklePersistentMixin3(GlobalSubclassNoPickle,
Persistent):
pass
class TestNoPickle(unittest.TestCase):
def _persist_zodb(self, obj):
from ZODB import DB
from ZODB.MappingStorage import MappingStorage
import transaction
db = DB(MappingStorage())
conn = db.open()
try:
conn.root.key = obj
transaction.commit()
finally:
conn.close()
db.close()
transaction.abort()
def _persist_pickle(self, obj):
import pickle
pickle.dumps(obj)
def _persist_cpickle(self, obj):
try:
import cPickle
except ImportError: # pragma: no cover
# Python 3
raise TypeError("Not allowed to pickle")
else:
cPickle.dumps(obj)
def _all_persists_fail(self, factory):
for meth in (self._persist_zodb,
self._persist_pickle,
self._persist_cpickle):
__traceback_info__ = meth
assert_that(calling(meth).with_args(factory()),
raises(TypeError, "Not allowed to pickle"))
def test_plain_object(self):
self._all_persists_fail(GlobalNoPickle)
def test_subclass_plain_object(self):
self._all_persists_fail(GlobalSubclassNoPickle)
def test_persistent(self):
self._all_persists_fail(GlobalPersistentNoPickle)
def test_subclass_persistent(self):
self._all_persists_fail(GlobalSubclassPersistentNoPickle)
def test_persistent_mixin1(self):
self._all_persists_fail(GlobalNoPicklePersistentMixin1)
def test_persistent_mixin2(self):
# Putting Persistent first works for zodb.
factory = GlobalNoPicklePersistentMixin2
self._persist_zodb(factory())
# But plain pickle still fails
with self.assertRaises(TypeError):
self._persist_pickle(factory())
def test_persistent_mixin3(self):
self._all_persists_fail(GlobalNoPicklePersistentMixin3)
def _check_emits_warning(self, kind):
with warnings.catch_warnings(record=True) as w:
NoPickle(kind)
assert_that(w, has_length(1))
assert_that(w[0].message, is_(RuntimeWarning))
self.assertIn("Using @NoPickle",
str(w[0].message))
def test_persistent_emits_warning(self):
class P(Persistent):
pass
self._check_emits_warning(P)
def test_getstate_emits_warning(self):
class P(object):
def __getstate__(self):
"Does nothing"
self._check_emits_warning(P)
def test_reduce_emits_warning(self):
class P(object):
def __reduce__(self):
"Does nothing"
self._check_emits_warning(P)
def test_reduce_ex_emits_warning(self):
class P(object):
def __reduce_ex__(self):
"Does nothing"
self._check_emits_warning(P)