comparison tests/test_fastpickle.py @ 450:298f8f46432a

internal: Add a `fastpickle` module to help with multiprocess serialization. Looks like we're wasting a lot of time serializing custom class information for the workers, so this new module helps to convert classes to standard structures (dicts, lists, etc).
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 06 Jul 2015 21:29:17 -0700
parents
children fcfbe103cfd1
comparison
equal deleted inserted replaced
449:30f2c2a595f5 450:298f8f46432a
1 import datetime
2 import pytest
3 from piecrust.fastpickle import pickle, unpickle
4
5
6 class Foo(object):
7 def __init__(self, name):
8 self.name = name
9 self.bars = []
10
11
12 class Bar(object):
13 def __init__(self, value):
14 self.value = value
15
16
17 @pytest.mark.parametrize(
18 'obj, expected',
19 [
20 (True, True),
21 (42, 42),
22 (3.14, 3.14),
23 (datetime.date(2015, 5, 21), datetime.date(2015, 5, 21)),
24 (datetime.datetime(2015, 5, 21, 12, 55, 32),
25 datetime.datetime(2015, 5, 21, 12, 55, 32)),
26 (datetime.time(9, 25, 57), datetime.time(9, 25, 57)),
27 ((1, 2, 3), (1, 2, 3)),
28 ([1, 2, 3], [1, 2, 3]),
29 ({'foo': 1, 'bar': 2}, {'foo': 1, 'bar': 2}),
30 (set([1, 2, 3]), set([1, 2, 3])),
31 ({'foo': [1, 2, 3], 'bar': {'one': 1, 'two': 2}},
32 {'foo': [1, 2, 3], 'bar': {'one': 1, 'two': 2}})
33 ])
34 def test_pickle_unpickle(obj, expected):
35 data = pickle(obj)
36 actual = unpickle(data)
37 assert actual == expected
38
39
40 def test_objects():
41 f = Foo('foo')
42 f.bars.append(Bar(1))
43 f.bars.append(Bar(2))
44
45 data = pickle(f)
46 o = unpickle(data)
47
48 assert type(o) == Foo
49 assert o.name == 'foo'
50 assert len(o.bars) == 2
51 for i in range(2):
52 assert f.bars[i].value == o.bars[i].value
53