test/unittest_utils.py
brancholdstable
changeset 5422 0865e1e90674
parent 5421 8167de96c523
child 5424 8ecbcbff9777
equal deleted inserted replaced
4985:02b52bf9f5f8 5422:0865e1e90674
       
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
     1 """unit tests for module cubicweb.utils
    18 """unit tests for module cubicweb.utils
     2 
    19 
     3 :organization: Logilab
       
     4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     7 """
    20 """
     8 
    21 
     9 import re
    22 import re
    10 import decimal
    23 import decimal
    11 import datetime
    24 import datetime
    12 
    25 
    13 from logilab.common.testlib import TestCase, unittest_main
    26 from logilab.common.testlib import TestCase, unittest_main
    14 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList
    27 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList
    15 
    28 
    16 try:
    29 try:
    17     import simplejson
    30     from cubicweb.utils import CubicWebJsonEncoder, json
    18     from cubicweb.utils import CubicWebJsonEncoder
       
    19 except ImportError:
    31 except ImportError:
    20     simplejson = None
    32     json = None
    21 
    33 
    22 class MakeUidTC(TestCase):
    34 class MakeUidTC(TestCase):
    23     def test_1(self):
    35     def test_1(self):
    24         self.assertNotEquals(make_uid('xyz'), make_uid('abcd'))
    36         self.assertNotEquals(make_uid('xyz'), make_uid('abcd'))
    25         self.assertNotEquals(make_uid('xyz'), make_uid('xyz'))
    37         self.assertNotEquals(make_uid('xyz'), make_uid('xyz'))
    39 class UStringIOTC(TestCase):
    51 class UStringIOTC(TestCase):
    40     def test_boolean_value(self):
    52     def test_boolean_value(self):
    41         self.assert_(UStringIO())
    53         self.assert_(UStringIO())
    42 
    54 
    43 
    55 
       
    56 class RepeatListTC(TestCase):
       
    57 
       
    58     def test_base(self):
       
    59         l = RepeatList(3, (1, 3))
       
    60         self.assertEquals(l[0], (1, 3))
       
    61         self.assertEquals(l[2], (1, 3))
       
    62         self.assertEquals(l[-1], (1, 3))
       
    63         self.assertEquals(len(l), 3)
       
    64         # XXX
       
    65         self.assertEquals(l[4], (1, 3))
       
    66 
       
    67         self.failIf(RepeatList(0, None))
       
    68 
       
    69     def test_slice(self):
       
    70         l = RepeatList(3, (1, 3))
       
    71         self.assertEquals(l[0:1], [(1, 3)])
       
    72         self.assertEquals(l[0:4], [(1, 3)]*3)
       
    73         self.assertEquals(l[:], [(1, 3)]*3)
       
    74 
       
    75     def test_iter(self):
       
    76         self.assertEquals(list(RepeatList(3, (1, 3))),
       
    77                           [(1, 3)]*3)
       
    78 
       
    79     def test_add(self):
       
    80         l = RepeatList(3, (1, 3))
       
    81         self.assertEquals(l + [(1, 4)], [(1, 3)]*3  + [(1, 4)])
       
    82         self.assertEquals([(1, 4)] + l, [(1, 4)] + [(1, 3)]*3)
       
    83         self.assertEquals(l + RepeatList(2, (2, 3)), [(1, 3)]*3 + [(2, 3)]*2)
       
    84 
       
    85         x = l + RepeatList(2, (1, 3))
       
    86         self.assertIsInstance(x, RepeatList)
       
    87         self.assertEquals(len(x), 5)
       
    88         self.assertEquals(x[0], (1, 3))
       
    89 
       
    90         x = l + [(1, 3)] * 2
       
    91         self.assertEquals(x, [(1, 3)] * 5)
       
    92 
       
    93     def test_eq(self):
       
    94         self.assertEquals(RepeatList(3, (1, 3)),
       
    95                           [(1, 3)]*3)
       
    96 
       
    97     def test_pop(self):
       
    98         l = RepeatList(3, (1, 3))
       
    99         l.pop(2)
       
   100         self.assertEquals(l, [(1, 3)]*2)
       
   101 
    44 class SizeConstrainedListTC(TestCase):
   102 class SizeConstrainedListTC(TestCase):
    45 
   103 
    46     def test_append(self):
   104     def test_append(self):
    47         l = SizeConstrainedList(10)
   105         l = SizeConstrainedList(10)
    48         for i in xrange(12):
   106         for i in xrange(12):
    59             l.extend(extension)
   117             l.extend(extension)
    60             yield self.assertEquals, l, expected
   118             yield self.assertEquals, l, expected
    61 
   119 
    62 class JSONEncoderTC(TestCase):
   120 class JSONEncoderTC(TestCase):
    63     def setUp(self):
   121     def setUp(self):
    64         if simplejson is None:
   122         if json is None:
    65             self.skip('simplejson not available')
   123             self.skip('json not available')
    66 
   124 
    67     def encode(self, value):
   125     def encode(self, value):
    68         return simplejson.dumps(value, cls=CubicWebJsonEncoder)
   126         return json.dumps(value, cls=CubicWebJsonEncoder)
    69 
   127 
    70     def test_encoding_dates(self):
   128     def test_encoding_dates(self):
    71         self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),
   129         self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),
    72                           '"2009/09/09 20:30:00"')
   130                           '"2009/09/09 20:30:00"')
    73         self.assertEquals(self.encode(datetime.date(2009, 9, 9)),
   131         self.assertEquals(self.encode(datetime.date(2009, 9, 9)),