test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Wed, 16 Mar 2016 10:06:18 +0100
changeset 11213 5c4c0b96c522
parent 10616 f454404733c1
child 11274 d0f6fe008ec4
permissions -rw-r--r--
[pkg] Use Twisted < 16.0.0 We have some test failures with 16.0.0, such as: - test_put_content (unittest_server.ETwistHTTPTC) - test_base_url (unittest_httptest.TwistedCWAnonTC) (grafted from 10c12f31d0fe38c2e8814176fc42bf2ea210efdf)

from six import PY2

from unittest import TestCase
from tempfile import NamedTemporaryFile
import os.path as osp

from logilab.common.shellutils import tempdir
from cubicweb import Binary


class BinaryTC(TestCase):
    def test_init(self):
        Binary()
        Binary(b'toto')
        Binary(bytearray(b'toto'))
        if PY2:
            Binary(buffer('toto'))
        else:
            Binary(memoryview(b'toto'))
        with self.assertRaises((AssertionError, TypeError)):
            # TypeError is raised by BytesIO if python runs with -O
            Binary(u'toto')

    def test_write(self):
        b = Binary()
        b.write(b'toto')
        b.write(bytearray(b'toto'))
        if PY2:
            b.write(buffer('toto'))
        else:
            b.write(memoryview(b'toto'))
        with self.assertRaises((AssertionError, TypeError)):
            # TypeError is raised by BytesIO if python runs with -O
            b.write(u'toto')

    def test_gzpickle_roundtrip(self):
        old = (u'foo', b'bar', 42, {})
        new = Binary.zpickle(old).unzpickle()
        self.assertEqual(old, new)
        self.assertIsNot(old, new)

    def test_from_file_to_file(self):
        with tempdir() as dpath:
            fpath = osp.join(dpath, 'binary.bin')
            with open(fpath, 'wb') as fobj:
                Binary(b'binaryblob').to_file(fobj)

            bobj = Binary.from_file(fpath)
            self.assertEqual(bobj.getvalue(), b'binaryblob')


if __name__ == '__main__':
    from unittest import main
    main()