cubicweb/test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Mon, 08 Apr 2019 11:24:53 +0200
branch3.26
changeset 12586 afafc8fd9a45
parent 12432 2fcb53ee5178
child 12567 26744ad37953
permissions -rw-r--r--
Account for new psycopg2 exception classes mapping From psycopg2 >= 2.8, specific exceptions are raised corresponding to postgresql errors. E.g. a CheckViolation exception is raised instead of a generic IntegrityError previously when a constraint violation occurs. The way we intercept database errors, especially for constraint violation, is not compliant with that because we do not catch subclasses of IntegrityError in native source's doexec() method. We fix this by checking for the presence of IntegrityError error in exception class's mro. This is still overcomplicated and clumsy, because we still use string comparison, but this is the best we can do as far as I know. (A better fix would be 'isinstance(ex, IntegrityError)' but we have no engine-independent error classes, so this is not possible. Something like sqlalchemy's DBAPI Errors [1] might help: https://docs.sqlalchemy.org/en/latest/errors.html#dbapi-errors)
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
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    23
from six import PY2
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    24
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    25
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
    26
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    27
from cubicweb import Binary
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    28
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    29
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    30
class BinaryTC(TestCase):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    31
    def test_init(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    32
        Binary()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    33
        Binary(b'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    34
        Binary(bytearray(b'toto'))
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    35
        if PY2:
12432
2fcb53ee5178 Fix flake8 issues since release 3.6.0
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11279
diff changeset
    36
            Binary(buffer('toto'))  # noqa: F821
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    37
        else:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    38
            Binary(memoryview(b'toto'))
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    39
        with self.assertRaises((AssertionError, TypeError)):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    40
            # 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
    41
            Binary(u'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    42
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    43
    def test_write(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    44
        b = Binary()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    45
        b.write(b'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    46
        b.write(bytearray(b'toto'))
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    47
        if PY2:
12432
2fcb53ee5178 Fix flake8 issues since release 3.6.0
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11279
diff changeset
    48
            b.write(buffer('toto'))  # noqa: F821
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    49
        else:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    50
            b.write(memoryview(b'toto'))
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    51
        with self.assertRaises((AssertionError, TypeError)):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    52
            # 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
    53
            b.write(u'toto')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    54
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    55
    def test_gzpickle_roundtrip(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    56
        old = (u'foo', b'bar', 42, {})
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    57
        new = Binary.zpickle(old).unzpickle()
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    58
        self.assertEqual(old, new)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    59
        self.assertIsNot(old, new)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    60
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    61
    def test_from_file_to_file(self):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    62
        with tempdir() as dpath:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    63
            fpath = osp.join(dpath, 'binary.bin')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    64
            with open(fpath, 'wb') as fobj:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    65
                Binary(b'binaryblob').to_file(fobj)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    66
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    67
            bobj = Binary.from_file(fpath)
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    68
            self.assertEqual(bobj.getvalue(), b'binaryblob')
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    69
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    70
    def test_pickleable(self):
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    71
        b = Binary(b'toto')
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    72
        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
    73
        self.assertEqual(b, bb)
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10616
diff changeset
    74
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    75
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    76
if __name__ == '__main__':
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    77
    from unittest import main
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    78
    main()