[Zope3-dev] namechooser : object deleted instead of being renamed

Marius Gedminas mgedmin at b4net.lt
Mon Jun 18 12:22:20 EDT 2007


On Mon, Jun 18, 2007 at 12:20:57PM +0200, Christophe Combelles wrote:
> Hi,
> 
> I've experienced a dangerous behaviour with namechoosers :
> 
> If the namechooser computes a name which is the same as the current name,
> the object is deleted!
> 
> This is simple to reproduce :
> - register a name chooser that only returns "foobar"
> - create your object → ok it's name is foobar
> - from the Contents view of it's container, try to rename it (with a 
> different name)
> - → deleted
> 
> Someone should try to reproduce that, to be sure that's not a side effect 
> of my app, but I don't think so.

I can reproduce it with a unit test:

mg at pitonas:~/src/Zope3 $ ./test.py -pv -s zope.copypastemove
Running tests at level 1
Running unit tests:
  Running:
    46/46 (100.0%) doctest_renaming_with_obstinate_name_chooser (zope.copypastemove.tests.test_rename)

Failure in test doctest_renaming_with_obstinate_name_chooser (zope.copypastemove.tests.test_rename)
Failed doctest test for zope.copypastemove.tests.test_rename.doctest_renaming_with_obstinate_name_chooser
  File "/home/mg/src/Zope3/src/zope/copypastemove/tests/test_rename.py", line 32, in doctest_renaming_with_obstinate_name_chooser

----------------------------------------------------------------------
File "/home/mg/src/Zope3/src/zope/copypastemove/tests/test_rename.py", line 67, in zope.copypastemove.tests.test_rename.doctest_renaming_with_obstinate_name_chooser
Failed example:
    list(container)
Expected:
    [u'foobar']
Got:
    []


  Ran 46 tests with 1 failures and 0 errors in 0.081 seconds.


Here's the test I added:


Index: src/zope/copypastemove/tests/test_rename.py
===================================================================
--- src/zope/copypastemove/tests/test_rename.py (revision 76767)
+++ src/zope/copypastemove/tests/test_rename.py (working copy)
@@ -28,10 +28,55 @@
     eventtesting.setUp()
     container_setup.setUp()
 
+
+def doctest_renaming_with_obstinate_name_chooser():
+    """Test ObjectMover when the name chooser returns the same name
+
+    This is a regression test for a bug reported by Christophe Combelles
+
+    Setup: register the IObjectMover adapter
+
+        >>> from zope.copypastemove import ContainerItemRenamer, ObjectMover
+        >>> from zope.component import adapts, provideAdapter
+        >>> provideAdapter(ObjectMover)
+
+    Suppose you have a NameChooser that always chooses the same name
+
+        >>> from zope.app.container.sample import SampleContainer
+        >>> class MyContainer(SampleContainer):
+        ...     pass
+        >>> from zope.app.container.contained import NameChooser
+        >>> class ObstinateNameChooser(NameChooser):
+        ...     adapts(MyContainer)
+        ...     def chooseName(self, name, object):
+        ...         return u'foobar'
+        >>> provideAdapter(ObstinateNameChooser)
+
+    Let's add an object to a container
+
+        >>> from zope.app.container.contained import Contained
+        >>> container = MyContainer()
+        >>> container[u'foobar'] = Contained()
+
+    Try to rename it
+
+        >>> ContainerItemRenamer(container).renameItem(u'foobar', u'newname')
+
+    The rename did not succeed, because the name chooser did not allow it:
+
+        >>> list(container)
+        [u'foobar']
+
+    There was a bug once: in this particular case the object mover adapter
+    would remove the item altogether.
+    """
+
+
 def test_suite():
     return unittest.TestSuite((
         DocTestSuite('zope.copypastemove',
                      setUp=setUp, tearDown=testing.tearDown),
+        DocTestSuite(setUp=setUp, tearDown=testing.tearDown),
         ))
 
 if __name__=='__main__':


Marius Gedminas
-- 
If nothing else helps, read the documentation.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20070618/af5b27d8/attachment.bin


More information about the Zope3-dev mailing list