Back to index

plone3  3.1.7
test_utils.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """
00026 """
00027 
00028 
00029 from Products.Archetypes.tests.attestcase import ATTestCase
00030 from Products.Archetypes.utils import DisplayList
00031 from Products.Archetypes.utils import make_uuid
00032 
00033 class UidGeneratorTest(ATTestCase):
00034     """Some ppl have reported problems with uids. This test isn't mathematical
00035     correct but should show the issue on plattform. I suspect it's Windows :|
00036     """
00037     
00038     def test_uuid(self):
00039         uids = {}
00040         loop_length = 10**5 # about 1.5 seconds on a fast cpu
00041         for i in xrange(loop_length):
00042             uid = make_uuid()
00043             uids[uid] = 1
00044         self.failUnlessEqual(len(uids), loop_length)
00045 
00046 class DisplayListTest(ATTestCase):
00047 
00048     def test_cmp(self):
00049         ta = ('a', 'b', 'c')
00050         tb = ('a', 'c', 'b')
00051         tc = ('a', 'c', 'c')
00052         td = ('c', 'b', 'a')
00053         self.failUnless(DisplayList(zip(ta, ta)) == DisplayList(zip(ta, ta)))
00054         self.failIf(DisplayList(zip(ta, ta)) == DisplayList(zip(ta, tb)))
00055         self.failUnless(DisplayList(zip(ta, ta)) == DisplayList(zip(td, td)))
00056         self.failUnless(DisplayList(zip(tb, ta)) == DisplayList(zip(tb, ta)))
00057         self.assertRaises(TypeError, cmp, DisplayList(), '')
00058 
00059     def test_slice(self):
00060         ta = ('a', 'b', 'c')
00061         l = zip(ta, ta)
00062         sub = l[1:]
00063         self.failUnless(DisplayList(l)[1:] == sub)
00064 
00065     def test_item(self):
00066         ta = ('a', 'b', 'c')
00067         l = zip(ta, ta)
00068         for i in range(0,2):
00069             item = ta[i]
00070             self.failUnless(DisplayList(l)[i] == item)
00071 
00072     def test_add(self):
00073         ta = ('a', 'b', 'c')
00074         l = zip(ta, ta)
00075         dl = DisplayList(l)[:]
00076         self.failUnless(dl == l)
00077         l.append(('d', 'd'))
00078         dl.append(('d', 'd'))
00079         self.failUnless(dl == l)
00080 
00081     def test_len(self):
00082         ta = ('a', 'b', 'c')
00083         l = zip(ta, ta)
00084         dl = DisplayList(l)
00085         self.failUnless(len(dl) == len(l))
00086 
00087     def test_keys(self):
00088         ta = ('a', 'b', 'c')
00089         l = zip(ta, ta)
00090         dl = DisplayList(l)
00091         self.failUnless(tuple(dl.keys()) == ta)
00092 
00093     def test_values(self):
00094         ta = ('a', 'b', 'c')
00095         l = zip(ta, ta)
00096         dl = DisplayList(l)
00097         self.failUnless(tuple(dl.values()) == ta)
00098 
00099     def test_items(self):
00100         ta = ('a', 'b', 'c')
00101         l = zip(ta, ta)
00102         dl = DisplayList(l)
00103         self.failUnless(dl.items() == tuple(l))
00104 
00105     def test_repr(self):
00106         ta = ('a', 'b', 'c')
00107         l = zip(ta, ta)
00108         dl = DisplayList(l)
00109         self.failUnless(repr(dl).find(str(l)))
00110 
00111     def test_str(self):
00112         ta = ('a', 'b', 'c')
00113         l = zip(ta, ta)
00114         dl = DisplayList(l)
00115         self.failUnless(str(dl) == str(l))
00116 
00117     def test_call(self):
00118         ta = ('a', 'b', 'c')
00119         l = zip(ta, ta)
00120         dl = DisplayList(l)
00121         self.failUnless(dl == dl)
00122         self.failUnless(dl() == dl())
00123         self.failUnless(dl[:] == l)
00124         self.failUnless(dl()[:] == l)
00125 
00126     def test_getmsgid(self):
00127         ta = (('a','a',), ('b','b','bb'), ('c', 'c'))
00128         dl = DisplayList(ta)
00129         self.assertEquals(dl.getMsgId('a'), 'a')
00130         self.assertEquals(dl.getMsgId('b'), 'bb')
00131 
00132     def test_concat(self):
00133         a = (('a','a',), ('b','b','bb'), ('c', 'c'))
00134         b = (('a','a','aa'), ('b','b'), ('c', 'c'))
00135         bzz, jzz = DisplayList(a), DisplayList(b)
00136         wahaaa = bzz + jzz
00137         self.failUnless(wahaaa.getMsgId('b') == 'bb')
00138         self.failUnless(wahaaa.getMsgId('a') == 'aa')
00139 
00140     def test_sort(self):
00141         a = (('a','a',), ('b','b'), ('c', 'c'))
00142         b = (('z','Z',), ('y','Y'), ('x', 'X'))
00143         c = (('a','Z',), ('c','Y'), ('b', 'X'))
00144         dla = DisplayList(a)
00145         dlb = DisplayList(b)
00146         dlc = DisplayList(c)
00147 
00148         assert dla.values() == ['a', 'b', 'c']
00149         dlb_s = dlb.sortedByValue()
00150         assert dlb_s.values() == ['X', 'Y', 'Z']
00151         dlc_s = dlc.sortedByKey()
00152         assert dlc_s.values() == ['Z', 'X', 'Y']
00153         
00154 ##    def test_getIndex(self):
00155 ##        a = ((None,'a',), (2,'b'), ('c', 'c'))
00156 ##        dla = DisplayList(a)
00157 ##        
00158 ##        self.assertEquals(dla.getIndex(None), 1)
00159 ##        self.assertEquals(dla.getIndex(2), 2)
00160 ##        self.assertEquals(dla.getIndex('c'), 3)
00161 ##        
00162 ##    def test_getIndexesFromKeys(self):
00163 ##        a = ((None,'a',), (2,'b'), ('c', 'c'))
00164 ##        dla = DisplayList(a)
00165 ##        
00166 ##        self.assertEquals(dla.getIndexesFromKeys([None, 2]), [1,2])
00167 ##        self.failUnless(dla.getIndexesFromKeys([None, 'a']) == [1])
00168 ##        
00169 ##
00170 ##    def test_getKeysFromIndexes(self):
00171 ##        a = ((None,'a',), (2,'b'), ('c', 'c'))
00172 ##        dla = DisplayList(a)
00173 ##        
00174 ##        self.assertEquals(dla.getKeysFromIndexes([1,2]), [None, 2])
00175 ##        self.failUnless(dla.getKeysFromIndexes([1,5]) == [None])
00176     
00177 
00178 
00179 def test_suite():
00180     from unittest import TestSuite, makeSuite
00181     suite = TestSuite()
00182     suite.addTest(makeSuite(DisplayListTest))
00183     suite.addTest(makeSuite(UidGeneratorTest))
00184     return suite