|
1 from six import PY2 |
|
2 |
|
3 from unittest import TestCase |
|
4 from tempfile import NamedTemporaryFile |
|
5 import os.path as osp |
|
6 |
|
7 from logilab.common.shellutils import tempdir |
|
8 from cubicweb import Binary |
|
9 |
|
10 |
|
11 class BinaryTC(TestCase): |
|
12 def test_init(self): |
|
13 Binary() |
|
14 Binary(b'toto') |
|
15 Binary(bytearray(b'toto')) |
|
16 if PY2: |
|
17 Binary(buffer('toto')) |
|
18 else: |
|
19 Binary(memoryview(b'toto')) |
|
20 with self.assertRaises((AssertionError, TypeError)): |
|
21 # TypeError is raised by BytesIO if python runs with -O |
|
22 Binary(u'toto') |
|
23 |
|
24 def test_write(self): |
|
25 b = Binary() |
|
26 b.write(b'toto') |
|
27 b.write(bytearray(b'toto')) |
|
28 if PY2: |
|
29 b.write(buffer('toto')) |
|
30 else: |
|
31 b.write(memoryview(b'toto')) |
|
32 with self.assertRaises((AssertionError, TypeError)): |
|
33 # TypeError is raised by BytesIO if python runs with -O |
|
34 b.write(u'toto') |
|
35 |
|
36 def test_gzpickle_roundtrip(self): |
|
37 old = (u'foo', b'bar', 42, {}) |
|
38 new = Binary.zpickle(old).unzpickle() |
|
39 self.assertEqual(old, new) |
|
40 self.assertIsNot(old, new) |
|
41 |
|
42 def test_from_file_to_file(self): |
|
43 with tempdir() as dpath: |
|
44 fpath = osp.join(dpath, 'binary.bin') |
|
45 with open(fpath, 'wb') as fobj: |
|
46 Binary(b'binaryblob').to_file(fobj) |
|
47 |
|
48 bobj = Binary.from_file(fpath) |
|
49 self.assertEqual(bobj.getvalue(), b'binaryblob') |
|
50 |
|
51 |
|
52 if __name__ == '__main__': |
|
53 from unittest import main |
|
54 main() |