Added tag cubicweb-version-3.10.1 for changeset 6c6859a67673
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""unit tests for module cubicweb.utils"""importreimportdecimalimportdatetimefromlogilab.common.testlibimportTestCase,unittest_mainfromcubicweb.utilsimportmake_uid,UStringIO,SizeConstrainedList,RepeatListfromcubicweb.entityimportEntitytry:fromcubicweb.utilsimportCubicWebJsonEncoder,jsonexceptImportError:json=NoneclassMakeUidTC(TestCase):deftest_1(self):self.assertNotEqual(make_uid('xyz'),make_uid('abcd'))self.assertNotEqual(make_uid('xyz'),make_uid('xyz'))deftest_2(self):d=set()whilelen(d)<10000:uid=make_uid('xyz')ifuidind:self.fail(len(d))ifre.match('\d',uid):self.fail('make_uid must not return something begining with ''some numeric character, got %s'%uid)d.add(uid)classUStringIOTC(TestCase):deftest_boolean_value(self):self.assert_(UStringIO())classRepeatListTC(TestCase):deftest_base(self):l=RepeatList(3,(1,3))self.assertEqual(l[0],(1,3))self.assertEqual(l[2],(1,3))self.assertEqual(l[-1],(1,3))self.assertEqual(len(l),3)# XXXself.assertEqual(l[4],(1,3))self.failIf(RepeatList(0,None))deftest_slice(self):l=RepeatList(3,(1,3))self.assertEqual(l[0:1],[(1,3)])self.assertEqual(l[0:4],[(1,3)]*3)self.assertEqual(l[:],[(1,3)]*3)deftest_iter(self):self.assertEqual(list(RepeatList(3,(1,3))),[(1,3)]*3)deftest_add(self):l=RepeatList(3,(1,3))self.assertEqual(l+[(1,4)],[(1,3)]*3+[(1,4)])self.assertEqual([(1,4)]+l,[(1,4)]+[(1,3)]*3)self.assertEqual(l+RepeatList(2,(2,3)),[(1,3)]*3+[(2,3)]*2)x=l+RepeatList(2,(1,3))self.assertIsInstance(x,RepeatList)self.assertEqual(len(x),5)self.assertEqual(x[0],(1,3))x=l+[(1,3)]*2self.assertEqual(x,[(1,3)]*5)deftest_eq(self):self.assertEqual(RepeatList(3,(1,3)),[(1,3)]*3)deftest_pop(self):l=RepeatList(3,(1,3))l.pop(2)self.assertEqual(l,[(1,3)]*2)classSizeConstrainedListTC(TestCase):deftest_append(self):l=SizeConstrainedList(10)foriinxrange(12):l.append(i)self.assertEqual(l,range(2,12))deftest_extend(self):testdata=[(range(5),range(5)),(range(10),range(10)),(range(12),range(2,12)),]forextension,expectedintestdata:l=SizeConstrainedList(10)l.extend(extension)yieldself.assertEqual,l,expectedclassJSONEncoderTC(TestCase):defsetUp(self):ifjsonisNone:self.skipTest('json not available')defencode(self,value):returnjson.dumps(value,cls=CubicWebJsonEncoder)deftest_encoding_dates(self):self.assertEqual(self.encode(datetime.datetime(2009,9,9,20,30)),'"2009/09/09 20:30:00"')self.assertEqual(self.encode(datetime.date(2009,9,9)),'"2009/09/09"')self.assertEqual(self.encode(datetime.time(20,30)),'"20:30:00"')deftest_encoding_decimal(self):self.assertEqual(self.encode(decimal.Decimal('1.2')),'1.2')deftest_encoding_bare_entity(self):e=Entity(None)e.cw_attr_cache['pouet']='hop'e.eid=2self.assertEqual(json.loads(self.encode(e)),{'pouet':'hop','eid':2})deftest_encoding_entity_in_list(self):e=Entity(None)e.cw_attr_cache['pouet']='hop'e.eid=2self.assertEqual(json.loads(self.encode([e])),[{'pouet':'hop','eid':2}])deftest_encoding_unknown_stuff(self):self.assertEqual(self.encode(TestCase),'null')if__name__=='__main__':unittest_main()