cubicweb/test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10616 test/unittest_binary.py@f454404733c1
child 11279 e4f11ef1face
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile

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()