cubicweb/test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 12355 c703dc95c82e
permissions -rw-r--r--
Drop python2 support This mostly consists in removing the dependency on "six" and updating the code to use only Python3 idioms. Notice that we previously used TemporaryDirectory from cubicweb.devtools.testlib for compatibility with Python2. We now directly import it from tempfile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     1
# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     3
#
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     4
# This file is part of CubicWeb.
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     5
#
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
     9
# any later version.
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    10
#
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    14
# details.
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    15
#
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    18
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    19
from unittest import TestCase
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    20
import os.path as osp
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    21
import pickle
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    22
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    23
from logilab.common.shellutils import tempdir
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    24
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    25
from cubicweb import Binary
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    26
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    27
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    28
class BinaryTC(TestCase):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    29
    def test_init(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    30
        Binary()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    31
        Binary(b'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    32
        Binary(bytearray(b'toto'))
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12355
diff changeset
    33
        Binary(memoryview(b'toto'))
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    34
        with self.assertRaises((AssertionError, TypeError)):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    35
            # 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
    36
            Binary(u'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    37
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    38
    def test_write(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    39
        b = Binary()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    40
        b.write(b'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    41
        b.write(bytearray(b'toto'))
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12355
diff changeset
    42
        b.write(memoryview(b'toto'))
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    43
        with self.assertRaises((AssertionError, TypeError)):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    44
            # 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
    45
            b.write(u'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    46
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    47
    def test_gzpickle_roundtrip(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    48
        old = (u'foo', b'bar', 42, {})
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    49
        new = Binary.zpickle(old).unzpickle()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    50
        self.assertEqual(old, new)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    51
        self.assertIsNot(old, new)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    52
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    53
    def test_from_file_to_file(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    54
        with tempdir() as dpath:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    55
            fpath = osp.join(dpath, 'binary.bin')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    56
            with open(fpath, 'wb') as fobj:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    57
                Binary(b'binaryblob').to_file(fobj)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    58
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    59
            bobj = Binary.from_file(fpath)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    60
            self.assertEqual(bobj.getvalue(), b'binaryblob')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    61
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    62
    def test_pickleable(self):
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    63
        b = Binary(b'toto')
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    64
        bb = pickle.loads(pickle.dumps(b))
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    65
        self.assertEqual(b, bb)
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    66
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    67
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    68
if __name__ == '__main__':
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    69
    from unittest import main
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    70
    main()