Back to index

plone3  3.1.7
test_uidhandling.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 handling.
00014 
00015 $Id: test_uidhandling.py 77004 2007-06-24 08:57:54Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.component import getSiteManager
00022 from zope.testing.cleanup import cleanUp
00023 
00024 from Products.CMFCore.tests.base.dummy import DummyContent
00025 from Products.CMFCore.tests.base.dummy import DummyFolder
00026 from Products.CMFCore.tests.base.testcase import SecurityTest
00027 from Products.CMFUid.interfaces import IUniqueIdAnnotationManagement
00028 from Products.CMFUid.interfaces import IUniqueIdGenerator
00029 from Products.CMFUid.interfaces import IUniqueIdHandler
00030 
00031 
00032 class DummyUid:
00033     """A dummy uid that surely is of different type of the generated ones.
00034     """
00035     pass
00036 
00037 
00038 class UniqueIdHandlerTests(SecurityTest):
00039 
00040     def _getTargetClass(self):
00041         from Products.CMFUid.UniqueIdHandlerTool import UniqueIdHandlerTool
00042 
00043         return UniqueIdHandlerTool
00044 
00045     def setUp(self):
00046         from Products.CMFCore.CatalogTool import CatalogTool
00047         from Products.CMFUid.UniqueIdAnnotationTool \
00048                 import UniqueIdAnnotationTool
00049         from Products.CMFUid.UniqueIdGeneratorTool \
00050                 import UniqueIdGeneratorTool
00051         SecurityTest.setUp(self)
00052         self.root._setObject('portal_catalog', CatalogTool())
00053         self.root._setObject('portal_uidgenerator', UniqueIdGeneratorTool())
00054         self.root._setObject('portal_uidannotation', UniqueIdAnnotationTool())
00055         self.root._setObject('portal_uidhandler', self._getTargetClass()())
00056         self.root._setObject('dummy', DummyContent(id='dummy'))
00057         self.root._setObject('dummy2', DummyContent(id='dummy2'))
00058 
00059         sm = getSiteManager()
00060         sm.registerUtility( self.root.portal_uidannotation
00061                           , IUniqueIdAnnotationManagement
00062                           )
00063         sm.registerUtility(self.root.portal_uidgenerator, IUniqueIdGenerator)
00064 
00065     def tearDown(self):
00066         cleanUp()
00067         SecurityTest.tearDown(self)
00068 
00069     def test_z3interfaces(self):
00070         from zope.interface.verify import verifyClass
00071         from Products.CMFUid.interfaces import IUniqueIdBrainQuery
00072         from Products.CMFUid.interfaces import IUniqueIdHandler
00073         from Products.CMFUid.interfaces import IUniqueIdUnrestrictedQuery
00074 
00075         verifyClass(IUniqueIdBrainQuery, self._getTargetClass())
00076         verifyClass(IUniqueIdHandler, self._getTargetClass())
00077         verifyClass(IUniqueIdUnrestrictedQuery, self._getTargetClass())
00078 
00079     def test_getUidOfNotYetRegisteredObject(self):
00080         handler = self.root.portal_uidhandler
00081         dummy = self.root.dummy
00082         UniqueIdError = handler.UniqueIdError
00083 
00084         self.assertEqual(handler.queryUid(dummy, None), None)
00085         self.assertRaises(UniqueIdError, handler.getUid, dummy)
00086 
00087     def test_getInvalidUid(self):
00088         handler = self.root.portal_uidhandler
00089         dummy = self.root.dummy
00090         UniqueIdError = handler.UniqueIdError
00091 
00092         self.assertEqual(handler.queryObject(100, None), None)
00093         self.assertRaises(UniqueIdError, handler.getObject, 100)
00094         self.assertEqual(handler.unrestrictedQueryObject(100, None), None)
00095         self.assertRaises(UniqueIdError, handler.unrestrictedGetObject, 100)
00096 
00097         uid = handler.register(dummy)
00098         self.assertEqual(handler.queryObject(uid+1, None), None)
00099         self.assertRaises(UniqueIdError, handler.getObject, uid+1)
00100         self.assertEqual(handler.unrestrictedQueryObject(uid+1, None), None)
00101         self.assertRaises(UniqueIdError, handler.unrestrictedGetObject, uid+1)
00102 
00103     def test_getUidOfRegisteredObject(self):
00104         handler = self.root.portal_uidhandler
00105         dummy = self.root.dummy
00106 
00107         uid = handler.register(dummy)
00108         self.assertEqual(handler.getUid(dummy), uid)
00109 
00110     def test_getRegisteredObjectByUid(self):
00111         handler = self.root.portal_uidhandler
00112         dummy = self.root.dummy
00113 
00114         uid = handler.register(dummy)
00115         self.assertEqual(handler.getObject(uid), dummy)
00116         self.assertEqual(handler.unrestrictedGetObject(uid), dummy)
00117 
00118     def test_getUnregisteredObject(self):
00119         handler = self.root.portal_uidhandler
00120         dummy = self.root.dummy
00121         UniqueIdError = handler.UniqueIdError
00122 
00123         uid = handler.register(dummy)
00124         handler.unregister(dummy)
00125         self.assertEqual(handler.queryObject(uid, None), None)
00126         self.assertRaises(UniqueIdError, handler.getObject, uid)
00127         self.assertEqual(handler.unrestrictedQueryObject(uid, None), None)
00128         self.assertRaises(UniqueIdError, handler.unrestrictedGetObject, uid)
00129 
00130     def test_getUidOfUnregisteredObject(self):
00131         handler = self.root.portal_uidhandler
00132         dummy = self.root.dummy
00133         UniqueIdError = handler.UniqueIdError
00134 
00135         uid = handler.register(dummy)
00136         handler.unregister(dummy)
00137         self.assertEqual(handler.queryUid(dummy, None), None)
00138         self.assertRaises(UniqueIdError, handler.getUid, dummy)
00139 
00140     def test_reregisterObject(self):
00141         handler = self.root.portal_uidhandler
00142         dummy = self.root.dummy
00143 
00144         uid1_reg = handler.register(dummy)
00145         uid1_get = handler.getUid(dummy)
00146         uid2_reg = handler.register(dummy)
00147         uid2_get = handler.getUid(dummy)
00148         self.assertEqual(uid1_reg, uid2_reg)
00149         self.assertEqual(uid1_get, uid2_get)
00150         self.assertEqual(uid1_reg, uid1_get)
00151 
00152     def test_unregisterObjectWithoutUid(self):
00153         handler = self.root.portal_uidhandler
00154         dummy = self.root.dummy
00155         UniqueIdError = handler.UniqueIdError
00156 
00157         self.assertRaises(UniqueIdError, handler.unregister, dummy)
00158 
00159     def test_setNewUidByHandWithCheck(self):
00160         handler = self.root.portal_uidhandler
00161         dummy = self.root.dummy
00162 
00163         # registering and unregisterung a object just to get a free uid
00164         unused_uid = handler.register(dummy)
00165         handler.unregister(dummy)
00166 
00167         handler.setUid(dummy, unused_uid)
00168 
00169         result = handler.getUid(dummy)
00170         self.assertEqual(unused_uid, result)
00171 
00172     def test_setSameUidOnSameObjectWithCheck(self):
00173         handler = self.root.portal_uidhandler
00174         dummy = self.root.dummy
00175 
00176         uid = handler.register(dummy)
00177 
00178         # just setting the same uid another time is allowed
00179         handler.setUid(dummy, uid)
00180 
00181         result = handler.getUid(dummy)
00182         self.assertEqual(uid, result)
00183 
00184     def test_setExistingUidOnDifferentObjectWithCheckRaisesException(self):
00185         handler = self.root.portal_uidhandler
00186         dummy = self.root.dummy
00187         dummy2 = self.root.dummy2
00188         UniqueIdError = handler.UniqueIdError
00189 
00190         # registering and unregisterung a object just to get a free uid
00191         uid1_reg = handler.register(dummy)
00192         uid2_reg = handler.register(dummy2)
00193 
00194         self.assertRaises(UniqueIdError, handler.setUid, dummy2, uid1_reg)
00195 
00196     def test_setExistingUidOnDifferentObjectWithoutCheck(self):
00197         handler = self.root.portal_uidhandler
00198         dummy = self.root.dummy
00199         dummy2 = self.root.dummy2
00200         UniqueIdError = handler.UniqueIdError
00201 
00202         # registering and unregisterung a object just to get a free uid
00203         uid1_reg = handler.register(dummy)
00204         uid2_reg = handler.register(dummy2)
00205 
00206         # now lets double the unique id
00207         handler.setUid(dummy2, uid1_reg, check_uniqueness=False)
00208 
00209         # calling a getter returns one object and generates a log
00210         # we can't capture. So let's ask the catalog directly.
00211         catalog = self.root.portal_catalog
00212         result = catalog({handler.UID_ATTRIBUTE_NAME: uid1_reg})
00213         self.assertEqual(len(result), 2)
00214 
00215         # dummy2 shall not be reachable anymore by uid2_reg
00216         self.assertRaises(UniqueIdError, handler.getBrain, uid2_reg)
00217 
00218     def test_setNoneAsUidRaisesException(self):
00219         handler = self.root.portal_uidhandler
00220         dummy = self.root.dummy
00221         UniqueIdError = handler.UniqueIdError
00222 
00223         uid = handler.register(dummy)
00224 
00225         self.assertRaises(UniqueIdError, handler.setUid, dummy, None)
00226 
00227     def test_setArbitraryKindOfUidRaisesException(self):
00228         handler = self.root.portal_uidhandler
00229         dummy = self.root.dummy
00230         UniqueIdError = handler.UniqueIdError
00231 
00232         uid = handler.register(dummy)
00233 
00234         # As we don't know what kind of exception the implementation
00235         # throws lets check for all exceptions!
00236         # IMHO it makes sense here to catch exceptions in general here!
00237         self.assertRaises(Exception, handler.setUid, dummy, DummyUid())
00238 
00239     def test_UidCataloging(self):
00240         handler = self.root.portal_uidhandler
00241         catalog = self.root.portal_catalog
00242         dummy = self.root.dummy
00243 
00244         uid = handler.register(dummy)
00245         brains = catalog(cmf_uid=uid)
00246         self.assertEqual(len(brains), 1)
00247 
00248     def test_UidCatalogingDoesNotAcquireUid(self):
00249         handler = self.root.portal_uidhandler
00250         catalog = self.root.portal_catalog
00251         self.root._setObject('folder', DummyFolder('folder'))
00252         folder = self.root.folder
00253 
00254         uid = handler.register(folder)
00255         brains = catalog(cmf_uid=uid)
00256         self.assertEqual(len(brains), 1)
00257 
00258         # Now catalog an unregistered subobject of the folder.
00259         # It should not acquire the cmf_uid, obviously.
00260         folder._setObject('dummy', DummyContent(id='dummy'))
00261         folder.dummy.indexObject()
00262         brains = catalog(cmf_uid=uid)
00263         self.assertEqual(len(brains), 1)
00264 
00265 
00266 def test_suite():
00267     return unittest.TestSuite((
00268         unittest.makeSuite(UniqueIdHandlerTests),
00269         ))
00270 
00271 if __name__ == '__main__':
00272     unittest.main(defaultTest='test_suite')