cubicweb/test/unittest_utils.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 12308 cbbcfa69a0e7
child 12573 9b883489279f
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:
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
     1
# copyright 2003-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""unit tests for module cubicweb.utils"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
    20
import base64
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    21
import datetime
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
    22
import decimal
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    23
import doctest
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    24
import re
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12308
diff changeset
    25
from unittest import TestCase
7569
02c338197322 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7277 7568
diff changeset
    26
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
    27
from cubicweb import Binary, Unauthorized
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
    28
from cubicweb.devtools.testlib import CubicWebTC
10933
830f1ea52789 [utils] Drop now-unused SizeConstrainedList
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10609
diff changeset
    29
from cubicweb.utils import (make_uid, UStringIO, RepeatList, HTMLHead,
12032
c16c1805e973 [cleanup] Drop no more used parse_repo_uri function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11917
diff changeset
    30
                            QueryCache)
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    31
from cubicweb.entity import Entity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
4694
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    33
try:
5389
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    34
    from cubicweb.utils import CubicWebJsonEncoder, json
4694
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    35
except ImportError:
5377
84d14ddfae13 [python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5077
diff changeset
    36
    json = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
    38
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
class MakeUidTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    def test_1(self):
6415
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
    41
        self.assertNotEqual(make_uid('xyz'), make_uid('abcd'))
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
    42
        self.assertNotEqual(make_uid('xyz'), make_uid('xyz'))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 720
diff changeset
    43
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def test_2(self):
3146
cfc4344023f2 have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents: 2635
diff changeset
    45
        d = set()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        while len(d)<10000:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
            uid = make_uid('xyz')
3146
cfc4344023f2 have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents: 2635
diff changeset
    48
            if uid in d:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
                self.fail(len(d))
4694
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    50
            if re.match('\d', uid):
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    51
                self.fail('make_uid must not return something begining with '
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    52
                          'some numeric character, got %s' % uid)
3146
cfc4344023f2 have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents: 2635
diff changeset
    53
            d.add(uid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7954
diff changeset
    55
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    56
class TestQueryCache(TestCase):
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    57
    def test_querycache(self):
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    58
        c = QueryCache(ceiling=20)
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    59
        # write only
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    60
        for x in range(10):
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    61
            c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    62
        self.assertEqual(c._usage_report(),
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    63
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    64
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    65
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    66
        c = QueryCache(ceiling=10)
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    67
        # we should also get a warning
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    68
        for x in range(20):
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    69
            c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    70
        self.assertEqual(c._usage_report(),
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    71
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    72
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    73
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    74
        # write + reads
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    75
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    76
        for n in range(4):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    77
            for x in range(10):
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    78
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    79
                c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    80
        self.assertEqual(c._usage_report(),
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    81
                         {'transientcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    82
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    83
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    84
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    85
        for n in range(17):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    86
            for x in range(10):
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    87
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    88
                c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    89
        self.assertEqual(c._usage_report(),
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    90
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    91
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    92
                          'permanentcount': 10})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    93
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    94
        for n in range(17):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    95
            for x in range(10):
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    96
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    97
                if n % 2:
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    98
                    c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    99
                if x % 2:
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   100
                    c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   101
        self.assertEqual(c._usage_report(),
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   102
                         {'transientcount': 5,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   103
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   104
                          'permanentcount': 5})
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 720
diff changeset
   105
12303
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   106
    def test_clear_on_overflow(self):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   107
        """Tests that only non-permanent items in the cache are wiped-out on ceiling overflow
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   108
        """
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   109
        c = QueryCache(ceiling=10)
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   110
        # set 10 values
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   111
        for x in range(10):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   112
            c[x] = x
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   113
        # arrange for the first 5 to be permanent
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   114
        for x in range(5):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   115
            for r in range(QueryCache._maxlevel + 2):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   116
                v = c[x]
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   117
                self.assertEqual(v, x)
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   118
        # Add the 11-th
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   119
        c[10] = 10
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   120
        self.assertEqual(c._usage_report(),
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   121
                         {'transientcount': 0,
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   122
                          'itemcount': 6,
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   123
                          'permanentcount': 5})
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   124
12305
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   125
    def test_get_with_default(self):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   126
        """
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   127
        Tests the capability of QueryCache for retrieving items with a default value
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   128
        """
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   129
        c = QueryCache(ceiling=20)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   130
        # set 10 values
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   131
        for x in range(10):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   132
            c[x] = x
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   133
        # arrange for the first 5 to be permanent
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   134
        for x in range(5):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   135
            for r in range(QueryCache._maxlevel + 2):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   136
                v = c[x]
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   137
                self.assertEqual(v, x)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   138
        self.assertEqual(c._usage_report(),
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   139
                         {'transientcount': 0,
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   140
                          'itemcount': 10,
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   141
                          'permanentcount': 5})
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   142
        # Test defaults for existing (including in permanents)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   143
        for x in range(10):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   144
            v = c.get(x, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   145
            self.assertEqual(v, x)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   146
        # Test defaults for others
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   147
        for x in range(10, 15):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   148
            v = c.get(x, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   149
            self.assertEqual(v, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   150
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   151
    def test_iterkeys(self):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   152
        """
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   153
        Tests the iterating on keys in the cache
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   154
        """
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   155
        c = QueryCache(ceiling=20)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   156
        # set 10 values
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   157
        for x in range(10):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   158
            c[x] = x
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   159
        # arrange for the first 5 to be permanent
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   160
        for x in range(5):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   161
            for r in range(QueryCache._maxlevel + 2):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   162
                v = c[x]
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   163
                self.assertEqual(v, x)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   164
        self.assertEqual(c._usage_report(),
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   165
                         {'transientcount': 0,
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   166
                          'itemcount': 10,
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   167
                          'permanentcount': 5})
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   168
        keys = sorted(c)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   169
        for x in range(10):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   170
            self.assertEquals(x, keys[x])
12303
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   171
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   172
    def test_items(self):
12306
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   173
        """
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   174
        Tests the iterating on key-value couples in the cache
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   175
        """
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   176
        c = QueryCache(ceiling=20)
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   177
        # set 10 values
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   178
        for x in range(10):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   179
            c[x] = x
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   180
        # arrange for the first 5 to be permanent
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   181
        for x in range(5):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   182
            for r in range(QueryCache._maxlevel + 2):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   183
                v = c[x]
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   184
                self.assertEqual(v, x)
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   185
        self.assertEqual(c._usage_report(),
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   186
                         {'transientcount': 0,
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   187
                          'itemcount': 10,
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   188
                          'permanentcount': 5})
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   189
        content = sorted(c.items())
12306
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   190
        for x in range(10):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   191
            self.assertEquals(x, content[x][0])
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   192
            self.assertEquals(x, content[x][1])
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   193
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   194
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
class UStringIOTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    def test_boolean_value(self):
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10236
diff changeset
   197
        self.assertTrue(UStringIO())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   200
class RepeatListTC(TestCase):
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   201
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   202
    def test_base(self):
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   203
        l = RepeatList(3, (1, 3))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   204
        self.assertEqual(l[0], (1, 3))
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   205
        self.assertEqual(l[2], (1, 3))
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   206
        self.assertEqual(l[-1], (1, 3))
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   207
        self.assertEqual(len(l), 3)
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   208
        # XXX
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   209
        self.assertEqual(l[4], (1, 3))
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   210
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7762
diff changeset
   211
        self.assertFalse(RepeatList(0, None))
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   212
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   213
    def test_slice(self):
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   214
        l = RepeatList(3, (1, 3))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   215
        self.assertEqual(l[0:1], [(1, 3)])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   216
        self.assertEqual(l[0:4], [(1, 3)]*3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   217
        self.assertEqual(l[:], [(1, 3)]*3)
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   218
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   219
    def test_iter(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   220
        self.assertEqual(list(RepeatList(3, (1, 3))),
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   221
                          [(1, 3)]*3)
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   222
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   223
    def test_add(self):
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   224
        l = RepeatList(3, (1, 3))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   225
        self.assertEqual(l + [(1, 4)], [(1, 3)]*3  + [(1, 4)])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   226
        self.assertEqual([(1, 4)] + l, [(1, 4)] + [(1, 3)]*3)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   227
        self.assertEqual(l + RepeatList(2, (2, 3)), [(1, 3)]*3 + [(2, 3)]*2)
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   228
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   229
        x = l + RepeatList(2, (1, 3))
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   230
        self.assertIsInstance(x, RepeatList)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   231
        self.assertEqual(len(x), 5)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   232
        self.assertEqual(x[0], (1, 3))
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   233
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   234
        x = l + [(1, 3)] * 2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   235
        self.assertEqual(x, [(1, 3)] * 5)
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   236
5077
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   237
    def test_eq(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   238
        self.assertEqual(RepeatList(3, (1, 3)),
5077
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   239
                          [(1, 3)]*3)
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   240
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   241
    def test_pop(self):
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   242
        l = RepeatList(3, (1, 3))
dc448c9ad9dd [rset] cubes such as iliane hi-jack rset description by poping some lines from it. Handle this on RepeatList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
   243
        l.pop(2)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   244
        self.assertEqual(l, [(1, 3)]*2)
5069
135c5d7b89d0 [querier] introduce RepeatList class, used to optimize size of data returned for result set description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4890
diff changeset
   245
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   246
4890
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4694
diff changeset
   247
class JSONEncoderTC(TestCase):
4694
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   248
    def setUp(self):
5377
84d14ddfae13 [python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5077
diff changeset
   249
        if json is None:
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   250
            self.skipTest('json not available')
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   251
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   252
    def encode(self, value):
5377
84d14ddfae13 [python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5077
diff changeset
   253
        return json.dumps(value, cls=CubicWebJsonEncoder)
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   254
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   255
    def test_encoding_dates(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   256
        self.assertEqual(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   257
                          '"2009/09/09 20:30:00"')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   258
        self.assertEqual(self.encode(datetime.date(2009, 9, 9)),
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   259
                          '"2009/09/09"')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   260
        self.assertEqual(self.encode(datetime.time(20, 30)),
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   261
                          '"20:30:00"')
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   262
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   263
    def test_encoding_decimal(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   264
        self.assertEqual(self.encode(decimal.Decimal('1.2')), '1.2')
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   265
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   266
    def test_encoding_bare_entity(self):
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   267
        e = Entity(None)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   268
        e.cw_attr_cache['pouet'] = 'hop'
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   269
        e.eid = 2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   270
        self.assertEqual(json.loads(self.encode(e)),
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   271
                          {'pouet': 'hop', 'eid': 2})
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   272
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   273
    def test_encoding_entity_in_list(self):
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   274
        e = Entity(None)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   275
        e.cw_attr_cache['pouet'] = 'hop'
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   276
        e.eid = 2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   277
        self.assertEqual(json.loads(self.encode([e])),
5726
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   278
                          [{'pouet': 'hop', 'eid': 2}])
c3b99606644d [json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   279
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   280
    def test_encoding_binary(self):
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   281
        for content in (b'he he', b'h\xe9 hxe9'):
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   282
            with self.subTest(content=content):
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   283
                encoded = self.encode(Binary(content))
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   284
                self.assertEqual(base64.b64decode(encoded), content)
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   285
3231
3ee43e2f8560 [utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2635
diff changeset
   286
    def test_encoding_unknown_stuff(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   287
        self.assertEqual(self.encode(TestCase), 'null')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 720
diff changeset
   288
11714
73201a210007 [test] Make cubicweb/test/unittest_utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11078
diff changeset
   289
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   290
class HTMLHeadTC(CubicWebTC):
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   291
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   292
    def htmlhead(self, datadir_url):
9657
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   293
        with self.admin_access.web_request() as req:
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   294
            base_url = u'http://test.fr/data/'
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   295
            req.datadir_url = base_url
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   296
            head = HTMLHead(req)
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   297
            return head
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   298
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   299
    def test_concat_urls(self):
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   300
        base_url = u'http://test.fr/data/'
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   301
        head = self.htmlhead(base_url)
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   302
        urls = [base_url + u'bob1.js',
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   303
                base_url + u'bob2.js',
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   304
                base_url + u'bob3.js']
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   305
        result = head.concat_urls(urls)
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   306
        expected = u'http://test.fr/data/??bob1.js,bob2.js,bob3.js'
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   307
        self.assertEqual(result, expected)
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   308
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   309
    def test_group_urls(self):
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   310
        base_url = u'http://test.fr/data/'
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   311
        head = self.htmlhead(base_url)
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   312
        urls_spec = [(base_url + u'bob0.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   313
                     (base_url + u'bob1.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   314
                     (u'http://ext.com/bob2.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   315
                     (u'http://ext.com/bob3.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   316
                     (base_url + u'bob4.css', 'all'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   317
                     (base_url + u'bob5.css', 'all'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   318
                     (base_url + u'bob6.css', 'print'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   319
                     (base_url + u'bob7.css', 'print'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   320
                     (base_url + u'bob8.css', ('all', u'[if IE 8]')),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   321
                     (base_url + u'bob9.css', ('print', u'[if IE 8]'))
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   322
                     ]
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   323
        result = head.group_urls(urls_spec)
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   324
        expected = [(base_url + u'??bob0.js,bob1.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   325
                    (u'http://ext.com/bob2.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   326
                    (u'http://ext.com/bob3.js', None),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   327
                    (base_url + u'??bob4.css,bob5.css', 'all'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   328
                    (base_url + u'??bob6.css,bob7.css', 'print'),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   329
                    (base_url + u'bob8.css', ('all', u'[if IE 8]')),
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   330
                    (base_url + u'bob9.css', ('print', u'[if IE 8]'))
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   331
                    ]
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   332
        self.assertEqual(list(result), expected)
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   333
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   334
    def test_getvalue_with_concat(self):
9599
13dd3a57dabb [test/utils] repair concat-resources test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8682
diff changeset
   335
        self.config.global_set_option('concat-resources', True)
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   336
        base_url = u'http://test.fr/data/'
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   337
        head = self.htmlhead(base_url)
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   338
        head.add_js(base_url + u'bob0.js')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   339
        head.add_js(base_url + u'bob1.js')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   340
        head.add_js(u'http://ext.com/bob2.js')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   341
        head.add_js(u'http://ext.com/bob3.js')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   342
        head.add_css(base_url + u'bob4.css')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   343
        head.add_css(base_url + u'bob5.css')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   344
        head.add_css(base_url + u'bob6.css', 'print')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   345
        head.add_css(base_url + u'bob7.css', 'print')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   346
        head.add_ie_css(base_url + u'bob8.css')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   347
        head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   348
        result = head.getvalue()
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   349
        expected = u"""<head>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   350
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/??bob4.css,bob5.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   351
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/??bob6.css,bob7.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   352
<!--[if lt IE 8]>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   353
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob8.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   354
<!--[if lt IE 7]>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   355
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob9.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   356
<![endif]--> 
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   357
<script type="text/javascript" src="http://test.fr/data/??bob0.js,bob1.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   358
<script type="text/javascript" src="http://ext.com/bob2.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   359
<script type="text/javascript" src="http://ext.com/bob3.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   360
</head>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   361
"""
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   362
        self.assertEqual(result, expected)
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   363
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   364
    def test_getvalue_without_concat(self):
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   365
        self.config.global_set_option('concat-resources', False)
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   366
        try:
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   367
            base_url = u'http://test.fr/data/'
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   368
            head = self.htmlhead(base_url)
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   369
            head.add_js(base_url + u'bob0.js')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   370
            head.add_js(base_url + u'bob1.js')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   371
            head.add_js(u'http://ext.com/bob2.js')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   372
            head.add_js(u'http://ext.com/bob3.js')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   373
            head.add_css(base_url + u'bob4.css')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   374
            head.add_css(base_url + u'bob5.css')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   375
            head.add_css(base_url + u'bob6.css', 'print')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   376
            head.add_css(base_url + u'bob7.css', 'print')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   377
            head.add_ie_css(base_url + u'bob8.css')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   378
            head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   379
            result = head.getvalue()
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   380
            expected = u"""<head>
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   381
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   382
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   383
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   384
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob7.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   385
<!--[if lt IE 8]>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   386
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob8.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   387
<!--[if lt IE 7]>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   388
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob9.css"/>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   389
<![endif]--> 
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   390
<script type="text/javascript" src="http://test.fr/data/bob0.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   391
<script type="text/javascript" src="http://test.fr/data/bob1.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   392
<script type="text/javascript" src="http://ext.com/bob2.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   393
<script type="text/javascript" src="http://ext.com/bob3.js"></script>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   394
</head>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   395
"""
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   396
            self.assertEqual(result, expected)
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   397
        finally:
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   398
            self.config.global_set_option('concat-resources', True)
4890
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4694
diff changeset
   399
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   400
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   401
def UnauthorizedTC(TestCase):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   402
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   403
    def _test(self, func):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   404
        self.assertEqual(func(Unauthorized()),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   405
                         'You are not allowed to perform this operation')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   406
        self.assertEqual(func(Unauthorized('a')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   407
                         'a')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   408
        self.assertEqual(func(Unauthorized('a', 'b')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   409
                         'You are not allowed to perform a operation on b')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   410
        self.assertEqual(func(Unauthorized('a', 'b', 'c')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   411
                         'a b c')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   412
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   413
    def test_str(self):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   414
        self._test(str)
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   415
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   416
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   417
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   418
def load_tests(loader, tests, ignore):
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   419
    import cubicweb.utils
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   420
    tests.addTests(doctest.DocTestSuite(cubicweb.utils))
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   421
    return tests
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   422
7568
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6415
diff changeset
   423
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
if __name__ == '__main__':
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   425
    import unittest
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   426
    unittest.main()