author | Julien Cristau <julien.cristau@logilab.fr> |
Tue, 23 Feb 2016 12:09:01 +0100 | |
changeset 11150 | 6f0c9d5c2f85 |
parent 10616 | f454404733c1 |
child 11274 | d0f6fe008ec4 |
permissions | -rw-r--r-- |
10616
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
1 |
from six import PY2 |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
2 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
3 |
from unittest import TestCase |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
4 |
from tempfile import NamedTemporaryFile |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
5 |
import os.path as osp |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
6 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
7 |
from logilab.common.shellutils import tempdir |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
8 |
from cubicweb import Binary |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
9 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
10 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
11 |
class BinaryTC(TestCase): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
12 |
def test_init(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
13 |
Binary() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
14 |
Binary(b'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
15 |
Binary(bytearray(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
16 |
if PY2: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
17 |
Binary(buffer('toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
18 |
else: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
19 |
Binary(memoryview(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
20 |
with self.assertRaises((AssertionError, TypeError)): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
21 |
# 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
|
22 |
Binary(u'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
23 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
24 |
def test_write(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
25 |
b = Binary() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
26 |
b.write(b'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
27 |
b.write(bytearray(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
28 |
if PY2: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
29 |
b.write(buffer('toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
30 |
else: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
31 |
b.write(memoryview(b'toto')) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
32 |
with self.assertRaises((AssertionError, TypeError)): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
33 |
# 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
|
34 |
b.write(u'toto') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
35 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
36 |
def test_gzpickle_roundtrip(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
37 |
old = (u'foo', b'bar', 42, {}) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
38 |
new = Binary.zpickle(old).unzpickle() |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
39 |
self.assertEqual(old, new) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
40 |
self.assertIsNot(old, new) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
41 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
42 |
def test_from_file_to_file(self): |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
43 |
with tempdir() as dpath: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
44 |
fpath = osp.join(dpath, 'binary.bin') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
45 |
with open(fpath, 'wb') as fobj: |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
46 |
Binary(b'binaryblob').to_file(fobj) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
47 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
48 |
bobj = Binary.from_file(fpath) |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
49 |
self.assertEqual(bobj.getvalue(), b'binaryblob') |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
50 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
51 |
|
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
52 |
if __name__ == '__main__': |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
53 |
from unittest import main |
f454404733c1
Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff
changeset
|
54 |
main() |