author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 24 Feb 2010 15:00:37 +0100 | |
branch | stable |
changeset 4694 | c19366a12281 |
parent 4252 | 6c4f109c2b03 |
child 4890 | b54255f82812 |
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 |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList |
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 |
||
44 |
class SizeConstrainedListTC(TestCase): |
|
45 |
||
46 |
def test_append(self): |
|
47 |
l = SizeConstrainedList(10) |
|
48 |
for i in xrange(12): |
|
49 |
l.append(i) |
|
50 |
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
|
51 |
|
0 | 52 |
def test_extend(self): |
53 |
testdata = [(range(5), range(5)), |
|
54 |
(range(10), range(10)), |
|
55 |
(range(12), range(2, 12)), |
|
56 |
] |
|
57 |
for extension, expected in testdata: |
|
58 |
l = SizeConstrainedList(10) |
|
59 |
l.extend(extension) |
|
60 |
yield self.assertEquals, l, expected |
|
61 |
||
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
|
62 |
class JSONEncoerTests(TestCase): |
4694
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
63 |
def setUp(self): |
c19366a12281
simplejson may not be available with python 2.4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
|
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 |
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
|
68 |
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
|
69 |
|
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 |
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
|
71 |
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
|
72 |
'"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
|
73 |
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
|
74 |
'"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
|
75 |
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
|
76 |
'"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
|
77 |
|
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
|
78 |
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
|
79 |
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
|
80 |
|
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
|
81 |
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
|
82 |
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
|
83 |
|
0 | 84 |
if __name__ == '__main__': |
85 |
unittest_main() |