source: trunk/tests/test_pickle.py @ 25

Revision 25, 8.7 KB checked in by xi, 9 years ago (diff)

Adding some docstrings.

RevLine 
[22]1# coding: utf-8
[21]2
3import unittest
4
5import syck
6
7SIMPLE = """
8- !python/none ~
9- !python/bool True
10- !python/bool False
11- !python/int 123
12- !python/long 12345678901234567890
13- !python/float 123.456
14- !python/float 123.4560000001
15- !python/str foo bar
16- !python/unicode FOO ЀУ bar бар
17- !python/list [1, 2, 3]
18- !python/tuple [foo, bar]
19- !python/dict {foo: bar}
20""", [
21    None,
22    True,
23    False,
24    123,
25    12345678901234567890L,
26    123.456,
27    123.4560000001,
28    'foo bar',
29    unicode('FOO ЀУ bar бар', 'utf-8'),
30    [1, 2, 3],
31    ('foo', 'bar'),
32    {'foo': 'bar'},
33]
34
35class AClass:
36    pass
37
38class ASubclass(AClass):
39    pass
40
41class AType(object):
42    pass
43
44class ASubtype(AType):
45    pass
46
47def a_function(*args, **kwds):
48    pass
49
50NAMES = """
51- !python/name:unittest.TestCase
52- !python/name:test_pickle.AClass
53- !python/name:test_pickle.ASubclass
54- !python/name:test_pickle.AType
55- !python/name:test_pickle.ASubtype
56- !python/name:test_pickle.a_function
57- !python/name:list
58- !python/name:__builtin__.dict
59- !python/name:file
60- !python/name:map
61- !python/name:type
62- !python/name:object
63""", [
64    unittest.TestCase,
65    AClass,
66    ASubclass,
67    AType,
68    ASubtype,
69    a_function,
70    list,
71    dict,
72    file,
73    map,
74    type,
75    object,
76]
77
78class AnObject(object):
79
80    def __new__(cls, foo=None, bar=None, baz=None):
81        self = object.__new__(cls)
82        self.foo = foo
83        self.bar = bar
84        self.baz = baz
85        return self
86
[24]87    def __cmp__(self, other):
88        return cmp((type(self), self.foo, self.bar, self.baz),
89                (type(other), other.foo, other.bar, other.baz))
90
[21]91    def __eq__(self, other):
92        return type(self) is type(other) and    \
93                (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz)
94
95class AnInstance:
96
97    def __init__(self, foo=None, bar=None, baz=None):
98        self.foo = foo
99        self.bar = bar
100        self.baz = baz
101
[24]102    def __cmp__(self, other):
103        return cmp((type(self), self.foo, self.bar, self.baz),
104                (type(other), other.foo, other.bar, other.baz))
105
[21]106    def __eq__(self, other):
107        return type(self) is type(other) and    \
108                (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz)
109
110class AState(AnInstance):
111
112    def __getstate__(self):
113        return {
114            '_foo': self.foo,
115            '_bar': self.bar,
116            '_baz': self.baz,
117        }
118
119    def __setstate__(self, state):
120        self.foo = state['_foo']
121        self.bar = state['_bar']
122        self.baz = state['_baz']
123
124OBJECTS = """
125- !python/object:test_pickle.AnObject
126    foo: 1
127    bar: two
128    baz: [3, 4, 5]
129- !python/object:test_pickle.AnInstance
130    foo: 1
131    bar: two
132    baz: [3, 4, 5]
133- !python/object:test_pickle.AState
134    _foo: 1
135    _bar: two
136    _baz: [3, 4, 5]
137""", [
138    AnObject(foo=1, bar='two', baz=[3,4,5]),
139    AnInstance(foo=1, bar='two', baz=[3,4,5]),
140    AState(foo=1, bar='two', baz=[3,4,5]),
141]
142
143class ACustomState(AnInstance):
144
145    def __getstate__(self):
146        return (self.foo, self.bar, self.baz)
147
148    def __setstate__(self, state):
149        self.foo, self.bar, self.baz = state
150
151class InitArgs(AnInstance):
152
153    def __getinitargs__(self):
154        return (self.foo, self.bar, self.baz)
155
156class InitArgsWithState(AnInstance):
157
158    def __getinitargs__(self):
159        return (self.foo, self.bar)
160
161    def __getstate__(self):
162        return self.baz[:]
163
164    def __setstate__(self, state):
165        self.baz = state[:]
166
167class NewArgs(AnObject):
168
169    def __getnewargs__(self):
170        return (self.foo, self.bar, self.baz)
171
172    def __getstate__(self):
173        return None
174
175class NewArgsWithState(AnObject):
176
177    def __getnewargs__(self):
178        return (self.foo, self.bar)
179
180    def __getstate__(self):
181        return self.baz[:]
182
183    def __setstate__(self, state):
184        self.baz = state[:]
185
186NEWS = """
187- !python/new:test_pickle.ACustomState
188    state: !python/tuple [1, two, [3, 4, 5]]
189- !python/new:test_pickle.InitArgs [1, two, [3, 4, 5]]
190- !python/new:test_pickle.InitArgs
191    args: [1]
192    kwds: {bar: two, baz: [3, 4, 5]}
193- !python/new:test_pickle.InitArgsWithState
194    args: [1, two, [3, 4, 5]]
195    state: [3, 4, 5]
196- !python/new:test_pickle.NewArgs [1, two, [3, 4, 5]]
197- !python/new:test_pickle.NewArgs
198    args: [1]
199    kwds: {bar: two, baz: [3, 4, 5]}
200- !python/new:test_pickle.NewArgsWithState
201    args: [1, two, [3, 4, 5]]
202    state: [3, 4, 5]
203""", [
204    ACustomState(foo=1, bar='two', baz=[3,4,5]),
205    InitArgs(foo=1, bar='two', baz=[3,4,5]),
206    InitArgs(foo=1, bar='two', baz=[3,4,5]),
207    InitArgsWithState(foo=1, bar='two', baz=[3,4,5]),
208    NewArgs(foo=1, bar='two', baz=[3,4,5]),
209    NewArgs(foo=1, bar='two', baz=[3,4,5]),
210    NewArgsWithState(foo=1, bar='two', baz=[3,4,5]),
211]
212
213class Reduce(AnObject):
214
215    def __reduce__(self):
216        return self.__class__, (self.foo, self.bar, self.baz)
217
218class ReduceWithState(AnObject):
219
220    def __reduce__(self):
221        return self.__class__, (self.foo, self.bar), self.baz[:]
222
223    def __setstate__(self, state):
224        self.baz = state[:]
225
226APPLIES = """
227- !python/apply:test_pickle.Reduce [1, two, [3, 4, 5]]
228- !python/apply:test_pickle.Reduce
229    args: [1]
230    kwds: {bar: two, baz: [3, 4, 5]}
231- !python/apply:test_pickle.ReduceWithState
232    args: [1, two]
233    state: [3, 4, 5]
234""", [
235    Reduce(foo=1, bar='two', baz=[3,4,5]),
236    Reduce(foo=1, bar='two', baz=[3,4,5]),
237    ReduceWithState(foo=1, bar='two', baz=[3,4,5]),
238]
239
240class TestPickle(unittest.TestCase):
241
242    def testSimple(self):
243        self._testPickle(SIMPLE)
244
245    def testNames(self):
246        self._testPickle(NAMES)
247
248    def testObjects(self):
249        self._testPickle(OBJECTS)
250
251    def testNews(self):
252        self._testPickle(NEWS)
253
254    def testApplies(self):
255        self._testPickle(APPLIES)
256
257    def _testPickle(self, (source, object)):
258        for left, right in zip(syck.load(source), object):
259            self.assertEqual(left, right)
260        for left, right in zip(syck.load(syck.dump(object)), object):
261            self.assertEqual(left, right)
262
[22]263class MyPrivateType(AnObject):
264    pass
265
266class MyPublicType(AnObject):
267    pass
268
269class MyMapping(AnObject):
270
271    def yaml_construct(cls, node):
272        return cls(**node.value)
273    yaml_construct = classmethod(yaml_construct)
274
275    def yaml_represent(self, node):
276        return syck.Map(self.__dict__.copy(), inline=True)
277
278EXTENSIONS = """
279- !!MyPrivateType [1, 2, 3]
280- !domain.tld,2005/MyPublicType [1, 2, 3]
281- !domain.tld,2005/AnotherPublicType [1, 2, 3]
282- {foo: 1, bar: 2, baz: 3}
283""", [
284    MyPrivateType(1,2,3),
285    MyPublicType(1,2,3),
286    MyPublicType(1,2,3),
287    MyMapping(1,2,3),
288]
289
[25]290NODES = """
291- foo
292- [bar, baz]
293"""
294
295BUGGY_NODES = """
296- foo
297- { bar: baz }
298"""
299
[22]300class ExLoader(syck.Loader):
301
302    def find_constructor(self, node):
303        if node.kind == 'map':
304            return MyMapping.yaml_construct
305        return super(ExLoader, self).find_constructor(node)
306
307    def make_mapping(self, node):
308        return MyMapping(**node.value)
309
310    def construct_private_MyPrivateType(self, node):
311        return MyPrivateType(*node.value)
312
313    def construct_domain_tld_2005(self, node):
314        return MyPublicType(*node.value)
315
316class ExDumper(syck.Dumper):
317
318    def find_representer(self, object):
319        if isinstance(object, MyMapping):
320            return object.yaml_represent
321        return super(ExDumper, self).find_representer(object)
322
323    def represent_test_pickle_MyPrivateType(self, object):
324        return syck.Seq([object.foo, object.bar, object.baz],
325                tag="x-private:MyPrivateType", inline=True)
326
327    def represent_test_pickle_MyPublicType(self, object):
328        return syck.Seq([object.foo, object.bar, object.baz],
329                tag="tag:domain.tld,2005:APublicType", inline=True)
330
331class TestExtensions(unittest.TestCase):
332
333    def testExtensions(self):
334        source = EXTENSIONS[0]
335        object = EXTENSIONS[1]
336        object2 = syck.load(source, Loader=ExLoader)
337        for left, right in zip(object, object2):
338            self.assertEqual(left, right)
339        source2 = syck.dump(object2, Dumper=ExDumper)
340        object3 = syck.load(source2, Loader=ExLoader)
341        for left, right in zip(object, object3):
342            self.assertEqual(left, right)
343
[25]344class TestNodesReduce(unittest.TestCase):
345
346    def testNodesReduce(self):
347        object = syck.load(NODES)
348        nodes = syck.parse(NODES)
349        output = syck.dump(nodes)
350        print output
351        nodes2 = syck.load(output)
352        output2 = syck.emit(nodes2)
353        object2 = syck.load(output2)
354        self.assertEqual(object, object2)
355
356    def testBuggyNodesReduce(self):
357        object = syck.load(BUGGY_NODES)
358        nodes = syck.parse(BUGGY_NODES)
359        output = syck.dump(nodes)
360        print output
361        nodes2 = syck.load(output)
362        output2 = syck.emit(nodes2)
363        object2 = syck.load(output2)
364        self.assertEqual(object, object2)
365
Note: See TracBrowser for help on using the repository browser.