author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 30 Mar 2010 13:27:30 +0200 | |
branch | stable |
changeset 5077 | dc448c9ad9dd |
parent 5069 | 135c5d7b89d0 |
child 5377 | 84d14ddfae13 |
permissions | -rw-r--r-- |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
1 |
"""unit tests for module cubicweb.utils |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
""" |
0 | 8 |
|
4694
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
import re |
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
|
10 |
import decimal |
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
|
11 |
import datetime |
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
|
12 |
|
4694
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
from logilab.common.testlib import TestCase, unittest_main |
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
|
14 |
from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList |
0 | 15 |
|
4694
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
try: |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
import simplejson |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
18 |
from cubicweb.utils import CubicWebJsonEncoder |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
19 |
except ImportError: |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
20 |
simplejson = None |
0 | 21 |
|
22 |
class MakeUidTC(TestCase): |
|
23 |
def test_1(self): |
|
24 |
self.assertNotEquals(make_uid('xyz'), make_uid('abcd')) |
|
25 |
self.assertNotEquals(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
|
26 |
|
0 | 27 |
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
|
28 |
d = set() |
0 | 29 |
while len(d)<10000: |
30 |
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
|
31 |
if uid in d: |
0 | 32 |
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
|
33 |
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
|
34 |
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
|
35 |
'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
|
36 |
d.add(uid) |
0 | 37 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
720
diff
changeset
|
38 |
|
0 | 39 |
class UStringIOTC(TestCase): |
40 |
def test_boolean_value(self): |
|
41 |
self.assert_(UStringIO()) |
|
42 |
||
43 |
||
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
|
44 |
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
|
45 |
|
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
|
46 |
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
|
47 |
l = RepeatList(3, (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
|
48 |
self.assertEquals(l[0], (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
|
49 |
self.assertEquals(l[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
|
50 |
self.assertEquals(l[-1], (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
|
51 |
self.assertEquals(len(l), 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
|
52 |
# XXX |
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
|
53 |
self.assertEquals(l[4], (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
|
54 |
|
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
|
55 |
self.failIf(RepeatList(0, None)) |
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
|
56 |
|
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
|
57 |
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
|
58 |
l = RepeatList(3, (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
|
59 |
self.assertEquals(l[0:1], [(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
|
60 |
self.assertEquals(l[0:4], [(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
|
61 |
self.assertEquals(l[:], [(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
|
62 |
|
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
|
63 |
def test_iter(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
|
64 |
self.assertEquals(list(RepeatList(3, (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
|
65 |
[(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
|
66 |
|
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
|
67 |
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
|
68 |
l = RepeatList(3, (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
|
69 |
self.assertEquals(l + [(1, 4)], [(1, 3)]*3 + [(1, 4)]) |
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
|
70 |
self.assertEquals([(1, 4)] + l, [(1, 4)] + [(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
|
71 |
self.assertEquals(l + RepeatList(2, (2, 3)), [(1, 3)]*3 + [(2, 3)]*2) |
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
|
72 |
|
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
|
73 |
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
|
74 |
self.assertIsInstance(x, RepeatList) |
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
|
75 |
self.assertEquals(len(x), 5) |
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
|
76 |
self.assertEquals(x[0], (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
|
77 |
|
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
|
78 |
x = l + [(1, 3)] * 2 |
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
|
79 |
self.assertEquals(x, [(1, 3)] * 5) |
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
|
80 |
|
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
|
81 |
def test_eq(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
|
82 |
self.assertEquals(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
|
83 |
[(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
|
84 |
|
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
|
85 |
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
|
86 |
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
|
87 |
l.pop(2) |
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
|
88 |
self.assertEquals(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
|
89 |
|
0 | 90 |
class SizeConstrainedListTC(TestCase): |
91 |
||
92 |
def test_append(self): |
|
93 |
l = SizeConstrainedList(10) |
|
94 |
for i in xrange(12): |
|
95 |
l.append(i) |
|
96 |
self.assertEquals(l, range(2, 12)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
720
diff
changeset
|
97 |
|
0 | 98 |
def test_extend(self): |
99 |
testdata = [(range(5), range(5)), |
|
100 |
(range(10), range(10)), |
|
101 |
(range(12), range(2, 12)), |
|
102 |
] |
|
103 |
for extension, expected in testdata: |
|
104 |
l = SizeConstrainedList(10) |
|
105 |
l.extend(extension) |
|
106 |
yield self.assertEquals, l, expected |
|
107 |
||
4890 | 108 |
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
|
109 |
def setUp(self): |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
110 |
if simplejson is None: |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
111 |
self.skip('simplejson 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
|
112 |
|
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
|
113 |
def encode(self, value): |
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
|
114 |
return simplejson.dumps(value, cls=CubicWebJsonEncoder) |
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
|
115 |
|
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
|
116 |
def test_encoding_dates(self): |
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
|
117 |
self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)), |
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
|
118 |
'"2009/09/09 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
|
119 |
self.assertEquals(self.encode(datetime.date(2009, 9, 9)), |
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
|
120 |
'"2009/09/09"') |
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
|
121 |
self.assertEquals(self.encode(datetime.time(20, 30)), |
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
|
122 |
'"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
|
123 |
|
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
|
124 |
def test_encoding_decimal(self): |
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
|
125 |
self.assertEquals(self.encode(decimal.Decimal('1.2')), '1.2') |
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
|
126 |
|
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
|
127 |
def test_encoding_unknown_stuff(self): |
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
|
128 |
self.assertEquals(self.encode(TestCase), 'null') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
720
diff
changeset
|
129 |
|
4890 | 130 |
|
0 | 131 |
if __name__ == '__main__': |
132 |
unittest_main() |