test/unittest_binary.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 10 Dec 2015 16:58:45 +0100
changeset 10997 da712d3f0601
parent 10616 f454404733c1
child 11274 d0f6fe008ec4
permissions -rw-r--r--
Bring back the separate web-side entity cache Prior to changeset 635cfac73d28 "[repoapi] fold ClientConnection into Connection", we had two entity caches: one on the client/dbapi/web side, and one on the server/repo side. This is a waste, but it is actually needed as long as we have the magic _cw attribute on entities which must sometimes be a request and sometimes a cnx. Removing the duplication caused weird problems with entity._cw alternating between both types of objects, which is unexpected by both the repo and the web sides. We add an entity cache on ConnectionCubicWebRequestBase, separate from the Connection's, and bring back the _cw_update_attr_cache/dont-cache-attrs mechanism, to let the server/edition code let caches know which attributes have been modified Entity.as_rset can be cached again, as ResultSet no longer modifies the entity when fetching it from a cache. Contrary to the pre-3.21 code, _cw_update_attr_cache now handles web requests and connections in the same way (otherwise the cache ends up with wrong values if a hook modifies attributes), but dont-cache-attrs is never set for (inlined) relations. Closes #6863543
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()