cubicweb/test/unittest_utils.py
author Laurent Wouters <lwouters@cenotelie.fr>
Wed, 25 Apr 2018 15:29:25 +0200
changeset 12308 cbbcfa69a0e7
parent 12306 c96dd92e480e
child 12567 26744ad37953
permissions -rw-r--r--
[utils] Fixes compatiblity of QueryCache with expected dict interface QueryCache is expected to mimick the interface of the dict built-in. The current __iter__ implementation breaks this expectation by iterating over key, value pairs instead of only the keys. This changeset fixes this issue by changing the __iter__ implementation to iterate over the keys in the cache and providing an implementation of the items method with a contract identical to the dict build-in (return a copy of the key- value pairs within the dictionary as a list).
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
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
    25
try:
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
    26
    from unittest2 import TestCase
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
    27
except ImportError:  # Python3
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
    28
    from unittest import TestCase
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
    29
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
    30
from six import PY2
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    31
from six.moves import range
7569
02c338197322 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7277 7568
diff changeset
    32
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
    33
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
    34
from cubicweb.devtools.testlib import CubicWebTC
10933
830f1ea52789 [utils] Drop now-unused SizeConstrainedList
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10609
diff changeset
    35
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
    36
                            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
    37
from cubicweb.entity import Entity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
4694
c19366a12281 simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    39
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
    40
    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
    41
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
    42
    json = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
    44
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
class MakeUidTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    def test_1(self):
6415
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
    47
        self.assertNotEqual(make_uid('xyz'), make_uid('abcd'))
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
    48
        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
    49
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    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
    51
        d = set()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        while len(d)<10000:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
            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
    54
            if uid in d:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
                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
    56
            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
    57
                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
    58
                          '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
    59
            d.add(uid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7954
diff changeset
    61
7954
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    62
class TestQueryCache(TestCase):
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    63
    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
    64
        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
    65
        # write only
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    66
        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
    67
            c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    68
        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
    69
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    70
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    71
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    72
        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
    73
        # 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
    74
        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
    75
            c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    76
        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
    77
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    78
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    79
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    80
        # write + reads
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    81
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    82
        for n in range(4):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    83
            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
    84
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    85
                c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    86
        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
    87
                         {'transientcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    88
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    89
                          'permanentcount': 0})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    90
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    91
        for n in range(17):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    92
            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
    93
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    94
                c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    95
        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
    96
                         {'transientcount': 0,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    97
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    98
                          'permanentcount': 10})
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
    99
        c = QueryCache(ceiling=20)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   100
        for n in range(17):
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   101
            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
   102
                c[x] = x
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   103
                if n % 2:
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   104
                    c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   105
                if x % 2:
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   106
                    c[x]
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   107
        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
   108
                         {'transientcount': 5,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   109
                          'itemcount': 10,
a3d3220669d6 [cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7791
diff changeset
   110
                          'permanentcount': 5})
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 720
diff changeset
   111
12303
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   112
    def test_clear_on_overflow(self):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   113
        """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
   114
        """
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   115
        c = QueryCache(ceiling=10)
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   116
        # set 10 values
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   117
        for x in range(10):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   118
            c[x] = x
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   119
        # arrange for the first 5 to be permanent
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   120
        for x in range(5):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   121
            for r in range(QueryCache._maxlevel + 2):
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   122
                v = c[x]
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   123
                self.assertEqual(v, x)
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   124
        # Add the 11-th
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   125
        c[10] = 10
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   126
        self.assertEqual(c._usage_report(),
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   127
                         {'transientcount': 0,
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   128
                          'itemcount': 6,
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   129
                          'permanentcount': 5})
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   130
12305
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   131
    def test_get_with_default(self):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   132
        """
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   133
        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
   134
        """
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   135
        c = QueryCache(ceiling=20)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   136
        # set 10 values
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   137
        for x in range(10):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   138
            c[x] = x
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   139
        # 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
   140
        for x in range(5):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   141
            for r in range(QueryCache._maxlevel + 2):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   142
                v = c[x]
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   143
                self.assertEqual(v, x)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   144
        self.assertEqual(c._usage_report(),
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   145
                         {'transientcount': 0,
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   146
                          'itemcount': 10,
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   147
                          'permanentcount': 5})
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   148
        # Test defaults for existing (including in permanents)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   149
        for x in range(10):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   150
            v = c.get(x, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   151
            self.assertEqual(v, x)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   152
        # Test defaults for others
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   153
        for x in range(10, 15):
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   154
            v = c.get(x, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   155
            self.assertEqual(v, -1)
9fd7d496e27e [utils] Added new get method to QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12303
diff changeset
   156
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   157
    def test_iterkeys(self):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   158
        """
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   159
        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
   160
        """
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   161
        c = QueryCache(ceiling=20)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   162
        # set 10 values
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   163
        for x in range(10):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   164
            c[x] = x
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   165
        # 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
   166
        for x in range(5):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   167
            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
   168
                v = c[x]
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   169
                self.assertEqual(v, x)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   170
        self.assertEqual(c._usage_report(),
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   171
                         {'transientcount': 0,
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   172
                          'itemcount': 10,
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   173
                          'permanentcount': 5})
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   174
        keys = sorted(c)
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   175
        for x in range(10):
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   176
            self.assertEquals(x, keys[x])
12303
198cb7d7b4ac [utils] Fixed issue in clearing QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12032
diff changeset
   177
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   178
    def test_items(self):
12306
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   179
        """
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   180
        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
   181
        """
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   182
        c = QueryCache(ceiling=20)
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   183
        # set 10 values
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   184
        for x in range(10):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   185
            c[x] = x
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   186
        # arrange for the first 5 to be permanent
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   187
        for x in range(5):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   188
            for r in range(QueryCache._maxlevel + 2):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   189
                v = c[x]
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   190
                self.assertEqual(v, x)
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   191
        self.assertEqual(c._usage_report(),
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   192
                         {'transientcount': 0,
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   193
                          'itemcount': 10,
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   194
                          'permanentcount': 5})
12308
cbbcfa69a0e7 [utils] Fixes compatiblity of QueryCache with expected dict interface
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12306
diff changeset
   195
        content = sorted(c.items())
12306
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   196
        for x in range(10):
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   197
            self.assertEquals(x, content[x][0])
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   198
            self.assertEquals(x, content[x][1])
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   199
c96dd92e480e [utils] Implements __iter__ on QueryCache
Laurent Wouters <lwouters@cenotelie.fr>
parents: 12305
diff changeset
   200
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
class UStringIOTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    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
   203
        self.assertTrue(UStringIO())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
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
   206
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
   207
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
    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
   209
        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
   210
        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
   211
        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
   212
        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
   213
        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
   214
        # 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
   215
        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
   216
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7762
diff changeset
   217
        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
   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_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
   220
        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
   221
        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
   222
        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
   223
        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
   224
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
   225
    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
   226
        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
   227
                          [(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
   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
    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
   230
        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
   231
        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
   232
        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
   233
        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
   234
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
   235
        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
   236
        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
   237
        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
   238
        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
   239
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
   240
        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
   241
        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
   242
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
   243
    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
   244
        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
   245
                          [(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
   246
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
   247
    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
   248
        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
   249
        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
   250
        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
   251
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
   252
4890
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4694
diff changeset
   253
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
   254
    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
   255
        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
   256
            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
   257
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
   258
    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
   259
        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
   260
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
    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
   262
        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
   263
                          '"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
   264
        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
   265
                          '"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
   266
        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
   267
                          '"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
   268
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
   269
    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
   270
        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
   271
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
   272
    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
   273
        e = Entity(None)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   274
        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
   275
        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
   276
        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
   277
                          {'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
   278
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
    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
   280
        e = Entity(None)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5726
diff changeset
   281
        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
   282
        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
   283
        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
   284
                          [{'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
   285
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   286
    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
   287
        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
   288
            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
   289
                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
   290
                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
   291
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
   292
    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
   293
        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
   294
11714
73201a210007 [test] Make cubicweb/test/unittest_utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11078
diff changeset
   295
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   296
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
   297
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   298
    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
   299
        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
   300
            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
   301
            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
   302
            head = HTMLHead(req)
3ff4c6f80c37 [test] update unittest_utils to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9599
diff changeset
   303
            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
   304
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   305
    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
   306
        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
   307
        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
   308
        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
   309
                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
   310
                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
   311
        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
   312
        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
   313
        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
   314
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   315
    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
   316
        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
   317
        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
   318
        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
   319
                     (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
   320
                     (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
   321
                     (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
   322
                     (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
   323
                     (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
   324
                     (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
   325
                     (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
   326
                     (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
   327
                     (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
   328
                     ]
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   329
        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
   330
        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
   331
                    (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
   332
                    (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
   333
                    (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
   334
                    (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
   335
                    (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
   336
                    (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
   337
                    ]
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   338
        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
   339
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   340
    def test_getvalue_with_concat(self):
9599
13dd3a57dabb [test/utils] repair concat-resources test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8682
diff changeset
   341
        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
   342
        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
   343
        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
   344
        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
   345
        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
   346
        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
   347
        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
   348
        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
   349
        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
   350
        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
   351
        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
   352
        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
   353
        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
   354
        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
   355
        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
   356
<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
   357
<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
   358
<!--[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
   359
<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
   360
<!--[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
   361
<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
   362
<![endif]--> 
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   363
<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
   364
<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
   365
<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
   366
</head>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   367
"""
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   368
        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
   369
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   370
    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
   371
        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
   372
        try:
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   373
            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
   374
            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
   375
            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
   376
            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
   377
            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
   378
            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
   379
            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
   380
            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
   381
            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
   382
            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
   383
            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
   384
            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
   385
            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
   386
            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
   387
<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
   388
<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
   389
<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
   390
<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
   391
<!--[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
   392
<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
   393
<!--[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
   394
<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
   395
<![endif]--> 
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   396
<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
   397
<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
   398
<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
   399
<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
   400
</head>
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 6415
diff changeset
   401
"""
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   402
            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
   403
        finally:
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7569
diff changeset
   404
            self.config.global_set_option('concat-resources', True)
4890
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4694
diff changeset
   405
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   406
11917
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   407
def UnauthorizedTC(TestCase):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   408
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   409
    def _test(self, func):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   410
        self.assertEqual(func(Unauthorized()),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   411
                         'You are not allowed to perform this operation')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   412
        self.assertEqual(func(Unauthorized('a')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   413
                         'a')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   414
        self.assertEqual(func(Unauthorized('a', 'b')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   415
                         '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
   416
        self.assertEqual(func(Unauthorized('a', 'b', 'c')),
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   417
                         'a b c')
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   418
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   419
    def test_str(self):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   420
        self._test(str)
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   421
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   422
    if PY2:
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   423
        def test_unicode(self):
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   424
            self._test(unicode)
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   425
c38e13988c10 Fix str(Unauthorized)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11715
diff changeset
   426
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   427
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
   428
    import cubicweb.utils
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   429
    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
   430
    return tests
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   431
7568
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6415
diff changeset
   432
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
if __name__ == '__main__':
11715
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   434
    import unittest
760d5c0ae08f [utils] Encode Binary value in base64 for JSON export
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11714
diff changeset
   435
    unittest.main()