Back to index

plone3  3.1.7
test_uidgeneration.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Test the unique id generation.
00014 
00015 $Id: test_uidgeneration.py 66702 2006-04-09 00:54:39Z jens $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from BTrees.Length import Length
00022 
00023 from Products.CMFCore.tests.base.testcase import SecurityTest
00024 
00025 
00026 class UniqueIdGeneratorToolTests(SecurityTest):
00027 
00028     def _getTargetClass(self):
00029         from Products.CMFUid.UniqueIdGeneratorTool \
00030                 import UniqueIdGeneratorTool
00031 
00032         return UniqueIdGeneratorTool
00033 
00034     def setUp(self):
00035         SecurityTest.setUp(self)
00036         self.root._setObject('portal_uidgenerator', self._getTargetClass()())
00037 
00038     def test_z3interfaces(self):
00039         from zope.interface.verify import verifyClass
00040         from Products.CMFUid.interfaces import IUniqueIdGenerator
00041 
00042         verifyClass(IUniqueIdGenerator, self._getTargetClass())
00043 
00044     def test_returnedUidsAreValidAndDifferent(self):
00045         generator = self.root.portal_uidgenerator
00046         uid1 = generator()
00047         uid2 = generator()
00048         self.failIfEqual(uid1, uid2)
00049         self.failIfEqual(uid1, None)
00050 
00051     def test_converter(self):
00052         generator = self.root.portal_uidgenerator
00053         uid = generator()
00054         str_uid = str(uid)
00055         result = generator.convert(str_uid)
00056         self.assertEqual(result, uid)
00057 
00058     def test_migrationFromBTreeLengthToInteger(self):
00059         # For backwards compatibility with CMF 1.5.0 and 1.5.1, check if
00060         # the generator correctly replaces a ``BTree.Length.Length`` object
00061         # to an integer.
00062         generator = self.root.portal_uidgenerator
00063         uid1 = generator()
00064         generator._uid_counter = Length(uid1)
00065         self.failUnless(isinstance(generator._uid_counter, Length))
00066         uid2 = generator()
00067         self.failUnless(isinstance(generator._uid_counter, int))
00068         self.failIfEqual(uid1, uid2)
00069 
00070 
00071 def test_suite():
00072     return unittest.TestSuite((
00073         unittest.makeSuite(UniqueIdGeneratorToolTests),
00074         ))
00075 
00076 if __name__ == '__main__':
00077     unittest.main(defaultTest='test_suite')