cubicweb/test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Mon, 18 Jan 2016 09:23:07 +0100
changeset 11072 8c3155a0ae5b
parent 11057 0b59724cb3f2
child 11279 e4f11ef1face
permissions -rw-r--r--
Handle virtualenv development mode Looking for VIRTUAL_ENV environment variable as a sign of virtualenv activation. This at least works for developing CubicWeb itself (not sure yet about cubes). And it makes `tox --develop` work as well. Add a test for cwconfig._find_prefix within a virtualenv. In tox.ini, use `develop option`_ accordingly. This makes tox running significantly faster since it avoids building and installing the sdist. Aside, the "develop" install is now performed *after* all test requirements are processed, which means that the cubicweb package should always be the one from source -- instead of a released version pulled by a cube through test dependencies, which may occur in "recreate" step. .. _`develop option`: <http://tox.readthedocs.org/en/latest/config.html#confval-usedevelop=BOOL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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()