test/unittest_utils.py
changeset 5726 c3b99606644d
parent 5424 8ecbcbff9777
child 6142 8bc6eac1fac1
child 6340 470d8e828fda
equal deleted inserted replaced
5725:b5d595b66c35 5726:c3b99606644d
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    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/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """unit tests for module cubicweb.utils
    18 """unit tests for module cubicweb.utils"""
    19 
       
    20 """
       
    21 
    19 
    22 import re
    20 import re
    23 import decimal
    21 import decimal
    24 import datetime
    22 import datetime
    25 
    23 
    26 from logilab.common.testlib import TestCase, unittest_main
    24 from logilab.common.testlib import TestCase, unittest_main
       
    25 
    27 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList
    26 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList
       
    27 from cubicweb.entity import Entity
    28 
    28 
    29 try:
    29 try:
    30     from cubicweb.utils import CubicWebJsonEncoder, json
    30     from cubicweb.utils import CubicWebJsonEncoder, json
    31 except ImportError:
    31 except ImportError:
    32     json = None
    32     json = None
    97     def test_pop(self):
    97     def test_pop(self):
    98         l = RepeatList(3, (1, 3))
    98         l = RepeatList(3, (1, 3))
    99         l.pop(2)
    99         l.pop(2)
   100         self.assertEquals(l, [(1, 3)]*2)
   100         self.assertEquals(l, [(1, 3)]*2)
   101 
   101 
       
   102 
   102 class SizeConstrainedListTC(TestCase):
   103 class SizeConstrainedListTC(TestCase):
   103 
   104 
   104     def test_append(self):
   105     def test_append(self):
   105         l = SizeConstrainedList(10)
   106         l = SizeConstrainedList(10)
   106         for i in xrange(12):
   107         for i in xrange(12):
   114                     ]
   115                     ]
   115         for extension, expected in testdata:
   116         for extension, expected in testdata:
   116             l = SizeConstrainedList(10)
   117             l = SizeConstrainedList(10)
   117             l.extend(extension)
   118             l.extend(extension)
   118             yield self.assertEquals, l, expected
   119             yield self.assertEquals, l, expected
       
   120 
   119 
   121 
   120 class JSONEncoderTC(TestCase):
   122 class JSONEncoderTC(TestCase):
   121     def setUp(self):
   123     def setUp(self):
   122         if json is None:
   124         if json is None:
   123             self.skip('json not available')
   125             self.skip('json not available')
   134                           '"20:30:00"')
   136                           '"20:30:00"')
   135 
   137 
   136     def test_encoding_decimal(self):
   138     def test_encoding_decimal(self):
   137         self.assertEquals(self.encode(decimal.Decimal('1.2')), '1.2')
   139         self.assertEquals(self.encode(decimal.Decimal('1.2')), '1.2')
   138 
   140 
       
   141     def test_encoding_bare_entity(self):
       
   142         e = Entity(None)
       
   143         e['pouet'] = 'hop'
       
   144         e.eid = 2
       
   145         self.assertEquals(json.loads(self.encode(e)),
       
   146                           {'pouet': 'hop', 'eid': 2})
       
   147 
       
   148     def test_encoding_entity_in_list(self):
       
   149         e = Entity(None)
       
   150         e['pouet'] = 'hop'
       
   151         e.eid = 2
       
   152         self.assertEquals(json.loads(self.encode([e])),
       
   153                           [{'pouet': 'hop', 'eid': 2}])
       
   154 
   139     def test_encoding_unknown_stuff(self):
   155     def test_encoding_unknown_stuff(self):
   140         self.assertEquals(self.encode(TestCase), 'null')
   156         self.assertEquals(self.encode(TestCase), 'null')
   141 
   157 
   142 
   158 
   143 if __name__ == '__main__':
   159 if __name__ == '__main__':