[Zope3-checkins] CVS: Zope3/src/zope/app/container/browser/tests - test_adding.py:1.5

Eckart Hertzler eckart at hertzler.de
Tue Apr 27 11:47:38 EDT 2004


Update of /cvs-repository/Zope3/src/zope/app/container/browser/tests
In directory cvs.zope.org:/tmp/cvs-serv7233/src/zope/app/container/browser/tests

Modified Files:
	test_adding.py 
Log Message:

- rearranged import statements
- renamed the test_constraint_driven_adding to  
  test_constraint_driven_addingInfo
- added a test for a constraint driven add
 


=== Zope3/src/zope/app/container/browser/tests/test_adding.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/container/browser/tests/test_adding.py:1.4	Fri Mar 19 15:26:24 2004
+++ Zope3/src/zope/app/container/browser/tests/test_adding.py	Tue Apr 27 11:47:37 2004
@@ -17,34 +17,35 @@
 """
 import unittest
 from zope.testing.doctestunit import DocTestSuite
-from zope.app import zapi
-from zope.app.tests import ztapi
-from zope.app.traversing.browser import AbsoluteURL
-from zope.app.container.browser.adding import Adding
-from zope.app.container.interfaces import IAdding
-from zope.app.container.interfaces import IObjectAddedEvent
-from zope.app.exception.interfaces import UserError
-from zope.app.traversing.interfaces import IContainmentRoot
-from zope.app.tests.placelesssetup import PlacelessSetup, setUp, tearDown
+
 from zope.component.interfaces import IFactory
 from zope.component.exceptions import ComponentLookupError
+import zope.interface
 from zope.interface import implements, Interface, directlyProvides
 from zope.publisher.browser import TestRequest
-from zope.app.publisher.browser import BrowserView
-from zope.app.container.contained import contained
 import zope.security.checker
 from zope.exceptions import ForbiddenAttribute
-from zope.app.container.interfaces import IWriteContainer
+from zope.app import zapi
+from zope.app.tests import ztapi
+from zope.app.tests.placelesssetup import PlacelessSetup, setUp, tearDown
+from zope.app.traversing.browser import AbsoluteURL
+from zope.app.traversing.interfaces import IContainmentRoot
+from zope.app.exception.interfaces import UserError
+from zope.app.publisher.browser import BrowserView
+from zope.app.container.interfaces import IAdding
+from zope.app.container.interfaces import IObjectAddedEvent
 from zope.app.container.interfaces import IContainerNamesContainer
-import zope.interface
 from zope.app.container.interfaces import INameChooser
 from zope.app.container.interfaces import IContainer
+from zope.app.container.contained import contained
+from zope.app.container.browser.adding import Adding
+from zope.app.container.sample import SampleContainer
 
 class Root:
     implements(IContainmentRoot)
 
-class Container(dict):
-    implements(IWriteContainer)
+class Container(SampleContainer):
+    pass
 
 class CreationView(BrowserView):
 
@@ -181,7 +182,7 @@
 
 
 
-def test_constraint_driven_adding():
+def test_constraint_driven_addingInfo():
     """
     >>> setUp()
     >>> serviceService = zapi.getService(None, zapi.servicenames.Services)
@@ -250,6 +251,70 @@
     >>> items[2]['title']
     'item3'
     >>> tearDown()    
+    """
+
+def test_constraint_driven_add():
+    """
+    >>> setUp()
+    >>> from zope.app.container.sample import SampleContainer
+    >>> from zope.app.container.browser.adding import Adding
+
+    >>> class F1:
+    ...     pass
+
+    >>> class F2:
+    ...     pass
+
+    >>> def pre(container, name, object):
+    ...     "a mock item constraint "
+    ...     if not isinstance(object, F1):
+    ...         raise zope.interface.Invalid('not a valid child')
+    
+    >>> class ITestContainer(zope.interface.Interface):
+    ...     def __setitem__(name, object):
+    ...         pass
+    ...     __setitem__.precondition = pre
+
+    >>> class Container(SampleContainer):
+    ...     zope.interface.implements(ITestContainer)
+
+    >>> adding = Adding(Container(), TestRequest())
+    >>> c = adding.add(F1())
+    
+    This test should fail, because the container only
+    accepts instances of F1
+    
+    >>> adding.add(F2())
+    Traceback (most recent call last):
+    ...
+    Invalid: not a valid child
+
+    >>> class ValidContainer(SampleContainer):
+    ...     zope.interface.implements(ITestContainer)
+
+    >>> def constr(container):
+    ...     "a mock container constraint"
+    ...     if not isinstance(container, ValidContainer):
+    ...         raise zope.interface.Invalid('not a valid container')
+    ...     return True
+
+    >>> class I2(zope.interface.Interface):
+    ...     __parent__ = zope.schema.Field(constraint = constr)
+
+    >>> zope.interface.classImplements(F1, I2)
+
+    This adding now fails, because the Container is not a valid
+    parent for F1 
+
+    >>> c = adding.add(F1())
+    Traceback (most recent call last):
+    ...
+    Invalid: not a valid container
+
+    >>> adding = Adding(ValidContainer(), TestRequest())
+    >>> c = adding.add(F1())
+
+    >>> tearDown()
     """
 
 def test_renderAddButton():




More information about the Zope3-Checkins mailing list