cubicweb/test/unittest_binary.py
changeset 12567 26744ad37953
parent 12355 c703dc95c82e
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    18 
    18 
    19 from unittest import TestCase
    19 from unittest import TestCase
    20 import os.path as osp
    20 import os.path as osp
    21 import pickle
    21 import pickle
    22 
    22 
    23 from six import PY2
       
    24 
       
    25 from logilab.common.shellutils import tempdir
    23 from logilab.common.shellutils import tempdir
    26 
    24 
    27 from cubicweb import Binary
    25 from cubicweb import Binary
    28 
    26 
    29 
    27 
    30 class BinaryTC(TestCase):
    28 class BinaryTC(TestCase):
    31     def test_init(self):
    29     def test_init(self):
    32         Binary()
    30         Binary()
    33         Binary(b'toto')
    31         Binary(b'toto')
    34         Binary(bytearray(b'toto'))
    32         Binary(bytearray(b'toto'))
    35         if PY2:
    33         Binary(memoryview(b'toto'))
    36             Binary(buffer('toto'))  # noqa: F821
       
    37         else:
       
    38             Binary(memoryview(b'toto'))
       
    39         with self.assertRaises((AssertionError, TypeError)):
    34         with self.assertRaises((AssertionError, TypeError)):
    40             # TypeError is raised by BytesIO if python runs with -O
    35             # TypeError is raised by BytesIO if python runs with -O
    41             Binary(u'toto')
    36             Binary(u'toto')
    42 
    37 
    43     def test_write(self):
    38     def test_write(self):
    44         b = Binary()
    39         b = Binary()
    45         b.write(b'toto')
    40         b.write(b'toto')
    46         b.write(bytearray(b'toto'))
    41         b.write(bytearray(b'toto'))
    47         if PY2:
    42         b.write(memoryview(b'toto'))
    48             b.write(buffer('toto'))  # noqa: F821
       
    49         else:
       
    50             b.write(memoryview(b'toto'))
       
    51         with self.assertRaises((AssertionError, TypeError)):
    43         with self.assertRaises((AssertionError, TypeError)):
    52             # TypeError is raised by BytesIO if python runs with -O
    44             # TypeError is raised by BytesIO if python runs with -O
    53             b.write(u'toto')
    45             b.write(u'toto')
    54 
    46 
    55     def test_gzpickle_roundtrip(self):
    47     def test_gzpickle_roundtrip(self):