cubicweb/test/unittest_binary.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 09 Mar 2017 16:36:33 +0100
changeset 12053 c3c9f2e1424c
parent 11279 e4f11ef1face
child 12355 c703dc95c82e
permissions -rw-r--r--
[pyramid] Add a "pyramid" instance configuration type In a new module 'cubicweb.pyramid.config' we define a "pyramid" instance configuration type. The noticeable feature of this configuration is that it manages a 'development.ini' file that gets installed in application home (along with `.conf` file). This file is templated and includes generated values for secrets of session and authtk tokens. This means that we can just call: pserve etc/cubicweb.d/<appname>/development.ini or gunicorn --paste etc/cubicweb.d/<appname>/development.ini -b :8080 just after instance creation to get a pyramid instance running without having to hack around a 'pyramid.ini' file. This patch drops 'development.ini' from skeleton and moves it in cubicweb/pyramid so that it gets installed at instance creation which is more appropriate than in cube creation. The new configuration class sets "cubicweb.bwcompat" setting to false so it is not intended to replace the "all-in-one" configuration type (which would require a bit more work). This configuration is close to the the 'repository' configuration type with just a couple of options from WebConfiguration that are needed for Pyramid (anonymous user/password plus some miscellaneous options that I'm not so sure are really needed). Note, in particular, that we do not pull CORS settings to be injected as a WSGI middleware like in wsgi_application_from_cwconfig() since I believe this should be left as an end-user responsibility and since this can be defined in a standard way in paste configuration. This configuration inherits from ServerConfiguration but registers the same appobjects as WebConfiguration. In cubicweb.web.request._CubicWebRequestBase, we guard against access to "uiprops" and "datadir_url" of the config because this new "pyramid" config does not have these (this does not make sense without bwcompat mode). At some point, we should either avoid using `cw_request`'s pyramid request attribute or make cubicweb's web request really independant of existing implementation and drop these assumptions.
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:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    36
            Binary(buffer('toto'))
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:
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents:
diff changeset
    48
            b.write(buffer('toto'))
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()