-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_persistence.py
188 lines (132 loc) · 5.24 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
#!/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 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.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
# 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'))