Back to index

plone3  3.1.7
test_rename.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 Unittests for a renaming archetypes objects.
00027 
00028 $Id: test_rename.py 7768 2007-03-18 23:38:08Z nouri $
00029 """
00030 
00031 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00032 
00033 from Products.Archetypes.tests.utils import makeContent
00034 from Products.Archetypes.tests.utils import populateFolder
00035 
00036 from Products.Archetypes.tests.utils import wrap_method
00037 from Products.Archetypes.tests.utils import unwrap_method
00038 from Products.Archetypes.utils import shasattr
00039 from Products.Archetypes.atapi import BaseContent
00040 from Products.Archetypes.config import UUID_ATTR
00041 
00042 import transaction
00043 import warnings
00044 
00045 class Counter:
00046 
00047     def __init__(self):
00048         self.ids = {}
00049 
00050     def add(self, uid):
00051         if not self.ids.has_key(uid):
00052             self.ids[uid] = 0
00053         self.ids[uid] += 1
00054 
00055     def reset(self):
00056         self.__init__()
00057 
00058     def get(self, uid):
00059         if not self.ids.has_key(uid):
00060             self.ids[uid] = 0
00061         return self.ids[uid]
00062 
00063 ADD_COUNTER = Counter()
00064 DELETE_COUNTER = Counter()
00065 CLONE_COUNTER = Counter()
00066 
00067 WARNING_LEVEL = 2
00068 DEBUG_CALL = False
00069 
00070 def UID(obj):
00071     uid = shasattr(obj, UUID_ATTR) and obj.UID() or obj.absolute_url()
00072     return uid
00073 
00074 def manage_afterAdd(self, item, container):
00075     res = self.__test_manage_afterAdd__(item, container)
00076     uid = UID(self)
00077     ADD_COUNTER.add(uid)
00078     if DEBUG_CALL:
00079         warnings.warn("manage_afterAdd called: %s:%s" %
00080                       (uid, ADD_COUNTER.get(uid)),
00081                       UserWarning,
00082                       WARNING_LEVEL)
00083     return res
00084 
00085 def manage_beforeDelete(self, item, container):
00086     uid = UID(self)
00087     DELETE_COUNTER.add(uid)
00088     if DEBUG_CALL:
00089         warnings.warn("manage_beforeDelete called: %s:%s" %
00090                       (uid, DELETE_COUNTER.get(uid)),
00091                       UserWarning,
00092                       WARNING_LEVEL)
00093     return self.__test_manage_beforeDelete__(item, container)
00094 
00095 def manage_afterClone(self, item):
00096     uid = UID(self)
00097     CLONE_COUNTER.add(uid)
00098     if DEBUG_CALL:
00099         warnings.warn("manage_afterClone called: %s:%s" %
00100                       (uid, CLONE_COUNTER.get(uid)),
00101                       UserWarning,
00102                       WARNING_LEVEL)
00103     return self.__test_manage_afterClone__(item)
00104 
00105 counts = (ADD_COUNTER, DELETE_COUNTER, CLONE_COUNTER)
00106 meths = {'manage_afterAdd':manage_afterAdd,
00107          'manage_beforeDelete':manage_beforeDelete,
00108          'manage_afterClone':manage_afterClone
00109          }
00110 
00111 class RenameTests(ATSiteTestCase):
00112 
00113     def afterSetUp(self):
00114         ATSiteTestCase.afterSetUp(self)
00115         for c in counts:
00116             c.reset()
00117         for name, meth in meths.items():
00118             wrap_method(BaseContent, name, meth, pattern='__test_%s__')
00119 
00120     def beforeTearDown(self):
00121         ATSiteTestCase.beforeTearDown(self)
00122         for name in meths.keys():
00123             unwrap_method(BaseContent, name)
00124 
00125     def test_rename(self):
00126         obj_id = 'demodoc'
00127         new_id = 'new_demodoc'
00128         doc = makeContent(self.folder, portal_type='Fact', id=obj_id)
00129         content = 'The book is on the table!'
00130         doc.setQuote(content, mimetype="text/plain")
00131         self.failUnless(str(doc.getQuote()) == str(content))
00132         # make sure we have _p_jar
00133         transaction.savepoint(optimistic=True)
00134         self.folder.manage_renameObject(obj_id, new_id)
00135         doc = getattr(self.folder, new_id)
00136         self.failUnless(str(doc.getQuote()) == str(content))
00137         uid = UID(doc)
00138         # Should call afterAdd twice, one for the object
00139         # creation and another for the rename
00140         self.assertEquals(ADD_COUNTER.get(uid), 2)
00141         # Should call beforeDelete once, when renaming the object
00142         self.assertEquals(DELETE_COUNTER.get(uid), 1)
00143         # Should never call afterClone
00144         self.assertEquals(CLONE_COUNTER.get(uid), 0)
00145 
00146     def getCounts(self, obj):
00147         uid = UID(obj)
00148         return (ADD_COUNTER.get(uid),
00149                 DELETE_COUNTER.get(uid),
00150                 CLONE_COUNTER.get(uid))
00151 
00152     def test_recursive(self):
00153         # Test for recursive calling of manage_after{Add|Clone}
00154         # and manage_beforeDelete. (bug #905677)
00155         populateFolder(self.folder, 'SimpleFolder', 'DDocument')
00156         d = self.folder.folder2.folder22.folder221.doc2211
00157         uid = UID(d)
00158         # Called afterAdd once
00159         self.assertEquals(ADD_COUNTER.get(uid), 1)
00160         # Never called beforeDelete or afterClone
00161         self.assertEquals(DELETE_COUNTER.get(uid), 0)
00162         self.assertEquals(CLONE_COUNTER.get(uid), 0)
00163 
00164         # make sure we have _p_jar
00165         transaction.savepoint(optimistic=True)
00166 
00167         d_count = self.getCounts(d)
00168 
00169         # Rename the parent folder
00170         self.folder.folder2.folder22.manage_renameObject('folder221',
00171                                                          'new_folder221')
00172         expected = (d_count[0]+1, d_count[1]+1, d_count[2]+0)
00173         got = self.getCounts(d)
00174         self.assertEquals(got, expected)
00175 
00176         # Update base count
00177         d_count = got
00178 
00179         # Rename the root folder
00180         self.folder.manage_renameObject('folder2', 'new_folder2')
00181 
00182         expected = (d_count[0]+1, d_count[1]+1, d_count[2]+0)
00183         got = self.getCounts(d)
00184         self.assertEquals(got, expected)
00185 
00186         # Update base count
00187         d_count = got
00188 
00189         # Copy the root folder
00190         cb = self.folder.manage_copyObjects(['new_folder2'])
00191         self.folder.manage_pasteObjects(cb)
00192 
00193         # Should *not* call manage_afterAdd or manage_afterClone,
00194         # or to manage_beforeDelete for the source object.
00195         expected = (d_count[0], d_count[1], d_count[2])
00196         got = self.getCounts(d)
00197         self.assertEquals(got, expected)
00198 
00199         new_d = self.folder.copy_of_new_folder2.folder22.new_folder221.doc2211
00200         got = self.getCounts(new_d)
00201         # Should have called manage_afterAdd and manage_afterClone for
00202         # the *new* object.
00203         self.assertEquals(got, (1, 0, 1))
00204 
00205 def test_suite():
00206     from unittest import TestSuite, makeSuite
00207     suite = TestSuite()
00208     suite.addTest(makeSuite(RenameTests))
00209     return suite