author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 23 Apr 2010 12:19:54 +0200 | |
branch | stable |
changeset 5389 | 809d3b5b3d31 |
parent 5377 | 84d14ddfae13 |
child 5421 | 8167de96c523 |
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: |
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
|
17 |
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
|
18 |
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
|
19 |
json = None |
0 | 20 |
|
21 |
class MakeUidTC(TestCase): |
|
22 |
def test_1(self): |
|
23 |
self.assertNotEquals(make_uid('xyz'), make_uid('abcd')) |
|
24 |
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
|
25 |
|
0 | 26 |
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
|
27 |
d = set() |
0 | 28 |
while len(d)<10000: |
29 |
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
|
30 |
if uid in d: |
0 | 31 |
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
|
32 |
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
|
33 |
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
|
34 |
'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
|
35 |
d.add(uid) |
0 | 36 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
720
diff
changeset
|
37 |
|
0 | 38 |
class UStringIOTC(TestCase): |
39 |
def test_boolean_value(self): |
|
40 |
self.assert_(UStringIO()) |
|
41 |
||
42 |
||
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
|
43 |
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
|
44 |
|
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 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
# 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
|
52 |
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
|
53 |
|
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 |
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
|
55 |
|
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 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
|
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 |
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
|
63 |
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
|
64 |
[(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
|
65 |
|
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 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
|
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 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
|
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 |
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
|
78 |
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
|
79 |
|
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
|
80 |
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
|
81 |
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
|
82 |
[(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
|
83 |
|
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 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
|
0 | 89 |
class SizeConstrainedListTC(TestCase): |
90 |
||
91 |
def test_append(self): |
|
92 |
l = SizeConstrainedList(10) |
|
93 |
for i in xrange(12): |
|
94 |
l.append(i) |
|
95 |
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
|
96 |
|
0 | 97 |
def test_extend(self): |
98 |
testdata = [(range(5), range(5)), |
|
99 |
(range(10), range(10)), |
|
100 |
(range(12), range(2, 12)), |
|
101 |
] |
|
102 |
for extension, expected in testdata: |
|
103 |
l = SizeConstrainedList(10) |
|
104 |
l.extend(extension) |
|
105 |
yield self.assertEquals, l, expected |
|
106 |
||
4890 | 107 |
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
|
108 |
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
|
109 |
if json is None: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5077
diff
changeset
|
110 |
self.skip('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
|
111 |
|
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 |
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
|
113 |
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
|
114 |
|
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 |
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
|
116 |
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
|
117 |
'"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
|
118 |
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
|
119 |
'"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
|
120 |
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
|
121 |
'"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
|
122 |
|
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 |
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
|
124 |
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
|
125 |
|
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 |
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
|
127 |
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
|
128 |
|
4890 | 129 |
|
0 | 130 |
if __name__ == '__main__': |
131 |
unittest_main() |