author | David Douard <david.douard@logilab.fr> |
Tue, 19 Jul 2016 19:30:10 +0200 | |
branch | 3.22 |
changeset 11438 | 828483a88825 |
parent 11274 | d0f6fe008ec4 |
permissions | -rw-r--r-- |
11274
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
1 |
# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
3 |
# |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
4 |
# This file is part of CubicWeb. |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
5 |
# |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
9 |
# any later version. |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
10 |
# |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
14 |
# details. |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
15 |
# |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
10616
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
18 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
19 |
from unittest import TestCase |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
20 |
import os.path as osp |
11274
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
21 |
import pickle |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
22 |
|
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
23 |
from six import PY2 |
10616
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
24 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
25 |
from logilab.common.shellutils import tempdir |
11274
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
26 |
|
10616
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
27 |
from cubicweb import Binary |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
28 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
29 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
30 |
class BinaryTC(TestCase): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
31 |
def test_init(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
32 |
Binary() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
33 |
Binary(b'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
34 |
Binary(bytearray(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
35 |
if PY2: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
36 |
Binary(buffer('toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
37 |
else: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
38 |
Binary(memoryview(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
39 |
with self.assertRaises((AssertionError, TypeError)): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
40 |
# TypeError is raised by BytesIO if python runs with -O |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
41 |
Binary(u'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
42 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
43 |
def test_write(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
44 |
b = Binary() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
45 |
b.write(b'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
46 |
b.write(bytearray(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
47 |
if PY2: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
48 |
b.write(buffer('toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
49 |
else: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
50 |
b.write(memoryview(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
51 |
with self.assertRaises((AssertionError, TypeError)): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
52 |
# TypeError is raised by BytesIO if python runs with -O |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
53 |
b.write(u'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
54 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
55 |
def test_gzpickle_roundtrip(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
56 |
old = (u'foo', b'bar', 42, {}) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
57 |
new = Binary.zpickle(old).unzpickle() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
58 |
self.assertEqual(old, new) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
59 |
self.assertIsNot(old, new) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
60 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
61 |
def test_from_file_to_file(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
62 |
with tempdir() as dpath: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
63 |
fpath = osp.join(dpath, 'binary.bin') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
64 |
with open(fpath, 'wb') as fobj: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
65 |
Binary(b'binaryblob').to_file(fobj) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
66 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
67 |
bobj = Binary.from_file(fpath) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
68 |
self.assertEqual(bobj.getvalue(), b'binaryblob') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
69 |
|
11274
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
70 |
def test_pickleable(self): |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
71 |
b = Binary(b'toto') |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
72 |
bb = pickle.loads(pickle.dumps(b)) |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
73 |
self.assertEqual(b, bb) |
d0f6fe008ec4
Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10616
diff
changeset
|
74 |
|
10616
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
75 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
76 |
if __name__ == '__main__': |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
77 |
from unittest import main |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
78 |
main() |