author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 16 Feb 2010 09:23:35 +0100 | |
changeset 4591 | 47acae3cb778 |
parent 4252 | 6c4f109c2b03 |
child 4694 | c19366a12281 |
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 |
|
9 |
from logilab.common.testlib import TestCase, unittest_main |
|
10 |
||
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
|
11 |
import simplejson |
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 |
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
|
13 |
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
|
14 |
|
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
|
15 |
from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, CubicWebJsonEncoder |
0 | 16 |
|
17 |
||
18 |
class MakeUidTC(TestCase): |
|
19 |
def test_1(self): |
|
20 |
self.assertNotEquals(make_uid('xyz'), make_uid('abcd')) |
|
21 |
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
|
22 |
|
0 | 23 |
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
|
24 |
d = set() |
0 | 25 |
while len(d)<10000: |
26 |
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
|
27 |
if uid in d: |
0 | 28 |
self.fail(len(d)) |
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
|
29 |
d.add(uid) |
0 | 30 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
720
diff
changeset
|
31 |
|
0 | 32 |
class UStringIOTC(TestCase): |
33 |
def test_boolean_value(self): |
|
34 |
self.assert_(UStringIO()) |
|
35 |
||
36 |
||
37 |
class SizeConstrainedListTC(TestCase): |
|
38 |
||
39 |
def test_append(self): |
|
40 |
l = SizeConstrainedList(10) |
|
41 |
for i in xrange(12): |
|
42 |
l.append(i) |
|
43 |
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
|
44 |
|
0 | 45 |
def test_extend(self): |
46 |
testdata = [(range(5), range(5)), |
|
47 |
(range(10), range(10)), |
|
48 |
(range(12), range(2, 12)), |
|
49 |
] |
|
50 |
for extension, expected in testdata: |
|
51 |
l = SizeConstrainedList(10) |
|
52 |
l.extend(extension) |
|
53 |
yield self.assertEquals, l, expected |
|
54 |
||
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
|
55 |
class JSONEncoerTests(TestCase): |
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
|
56 |
|
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
|
57 |
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
|
58 |
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
|
59 |
|
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
|
60 |
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
|
61 |
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
|
62 |
'"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
|
63 |
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
|
64 |
'"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
|
65 |
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
|
66 |
'"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
|
67 |
|
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
|
68 |
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
|
69 |
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
|
70 |
|
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
|
71 |
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
|
72 |
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
|
73 |
|
0 | 74 |
if __name__ == '__main__': |
75 |
unittest_main() |