[Zope3-checkins] CVS: Zope3/src/zope/app/tests - setup.py:1.5.2.1 test_context.py:1.4.4.1 placelesssetup.py:1.4.4.1 test_attributeannotations.py:1.3.10.1 test_clipboard.py:1.5.10.1 test_dependable.py:1.2.26.1 test_introspector.py:1.2.26.1
Grégoire Weber
zope@i-con.ch
Sun, 22 Jun 2003 10:24:04 -0400
Update of /cvs-repository/Zope3/src/zope/app/tests
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/app/tests
Modified Files:
Tag: cw-mail-branch
placelesssetup.py test_attributeannotations.py
test_clipboard.py test_dependable.py test_introspector.py
Added Files:
Tag: cw-mail-branch
setup.py test_context.py
Log Message:
Synced up with HEAD
=== Added File Zope3/src/zope/app/tests/setup.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Setting up an environment for testing context-dependent objects
$Id: setup.py,v 1.5.2.1 2003/06/22 14:23:32 gregweb Exp $
"""
import zope.component
from zope.app import zapi
from zope.component.adapter import provideAdapter
from zope.component.view import provideView
from zope.interface import classImplements
#------------------------------------------------------------------------
# Annotations
from zope.app.attributeannotations import AttributeAnnotations
from zope.app.interfaces.annotation import IAnnotations
from zope.app.interfaces.annotation import IAttributeAnnotatable
def setUpAnnotations():
provideAdapter(IAttributeAnnotatable, IAnnotations,
AttributeAnnotations)
#------------------------------------------------------------------------
# Dependencies
from zope.app.dependable import Dependable
from zope.app.interfaces.dependable import IDependable
def setUpDependable():
provideAdapter(IAttributeAnnotatable, IDependable,
Dependable)
#------------------------------------------------------------------------
# Traversal
from zope.app.browser.absoluteurl import SiteAbsoluteURL, AbsoluteURL
from zope.app.container.traversal import ContainerTraversable
from zope.app.interfaces.container import ISimpleReadContainer
from zope.app.interfaces.traversing import IContainmentRoot
from zope.app.interfaces.traversing import IPhysicallyLocatable
from zope.app.interfaces.traversing import ITraverser, ITraversable
from zope.app.traversing.adapters import DefaultTraversable
from zope.app.traversing.adapters import Traverser, RootPhysicallyLocatable
from zope.app.traversing.adapters import WrapperPhysicallyLocatable
from zope.app.traversing.namespace import etc, provideNamespaceHandler
from zope.publisher.interfaces.browser import IBrowserPresentation
def setUpTraversal():
provideAdapter(None, ITraverser, Traverser)
provideAdapter(None, ITraversable, DefaultTraversable)
provideAdapter(
ISimpleReadContainer, ITraversable, ContainerTraversable)
provideAdapter(
None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
provideAdapter(
IContainmentRoot, IPhysicallyLocatable, RootPhysicallyLocatable)
# set up etc namespace
provideNamespaceHandler("etc", etc)
provideView(None, "absolute_url", IBrowserPresentation,
AbsoluteURL)
provideView(IContainmentRoot, "absolute_url", IBrowserPresentation,
SiteAbsoluteURL)
#------------------------------------------------------------------------
# Use registration
from zope.app.interfaces.services.registration import IAttributeRegisterable
from zope.app.interfaces.services.registration import IRegistered
from zope.app.services.registration import Registered
def setUpRegistered():
provideAdapter(IAttributeRegisterable, IRegistered,
Registered)
#------------------------------------------------------------------------
# Placeful setup
from zope.app.component.hooks import getServiceManager_hook
from zope.app.tests.placelesssetup import setUp as placelessSetUp
from zope.app.tests.placelesssetup import tearDown as placelessTearDown
def placefulSetUp(site=False):
placelessSetUp()
zope.component.getServiceManager.sethook(getServiceManager_hook)
setUpAnnotations()
setUpDependable()
setUpTraversal()
setUpRegistered()
if site:
site = RootFolder()
createServiceManager(site)
return site
def placefulTearDown():
placelessTearDown()
zope.component.getServiceManager.reset()
from zope.app.content.folder import Folder, RootFolder
def buildSampleFolderTree():
# set up a reasonably complex folder structure
#
# ____________ rootFolder ____________
# / \
# folder1 __________________ folder2
# | \ |
# folder1_1 ____ folder1_2 folder2_1
# | \ | |
# folder1_1_1 folder1_1_2 folder1_2_1 folder2_1_1
root = RootFolder()
root.setObject('folder1', Folder())
root['folder1'].setObject('folder1_1', Folder())
root['folder1']['folder1_1'].setObject('folder1_1_1', Folder())
root['folder1']['folder1_1'].setObject('folder1_1_2', Folder())
root['folder1'].setObject('folder1_2', Folder())
root['folder1']['folder1_2'].setObject('folder1_2_1', Folder())
root.setObject('folder2', Folder())
root['folder2'].setObject('folder2_1', Folder())
root['folder2']['folder2_1'].setObject('folder2_1_1', Folder())
return root
from zope.app.services.service import ServiceManager
def createServiceManager(folder):
if not folder.hasServiceManager():
folder.setServiceManager(ServiceManager())
return zapi.traverse(folder, "++etc++site")
from zope.app.services.service import ServiceRegistration
from zope.app.interfaces.services.registration import ActiveStatus
def addService(servicemanager, name, service, suffix=''):
"""Add a service to a service manager
This utility is useful for tests that need to set up services.
"""
default = zapi.traverse(servicemanager, 'default')
default.setObject(name+suffix, service)
path = "%s/default/%s" % (zapi.getPath(servicemanager), name+suffix)
registration = ServiceRegistration(name, path, servicemanager)
key = default.getRegistrationManager().setObject("", registration)
zapi.traverse(default.getRegistrationManager(), key).status = ActiveStatus
return zapi.traverse(servicemanager, path)
from zope.component import getServiceManager
from zope.app.interfaces.services.hub import IObjectHub
from zope.app.interfaces.services.event import ISubscriptionService
from zope.app.services.event import EventService
from zope.app.services.hub import ObjectHub
from zope.app.services.servicenames import HubIds
from zope.app.services.servicenames import EventPublication, EventSubscription
def createStandardServices(folder, hubids=None):
'''Create a bunch of standard placeful services
Well, uh, 3
'''
sm = createServiceManager(folder)
defineService = getServiceManager(None).defineService
defineService(EventSubscription, ISubscriptionService)
# EventPublication service already defined by
# zope.app.events.tests.PlacelessSetup
defineService(HubIds, IObjectHub)
# EventService must be IAttributeAnnotatable so that it can support
# dependencies.
classImplements(EventService, IAttributeAnnotatable)
events = EventService()
addService(sm, EventPublication, events)
addService(sm, EventSubscription, events, suffix='sub')
if hubids is None:
hubids = ObjectHub()
addService(sm, HubIds, hubids)
=== Added File Zope3/src/zope/app/tests/test_context.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""XXX short summary goes here.
XXX longer description goes here.
$Id: test_context.py,v 1.4.4.1 2003/06/22 14:23:32 gregweb Exp $
"""
import pickle
import unittest
from zope.app.context import Wrapper
from zope.interface import Interface, implements, directlyProvides, providedBy
from zope.interface import directlyProvidedBy, implementedBy
from zope.testing.doctestunit import DocTestSuite
class I1(Interface):
pass
class I2(Interface):
pass
class I3(Interface):
pass
class I4(Interface):
pass
class D1(Wrapper):
implements(I1)
class D2(Wrapper):
implements(I2)
def test_providedBy_iter_w_new_style_class():
"""
>>> class X(object):
... implements(I3)
>>> x = X()
>>> directlyProvides(x, I4)
>>> [interface.__name__ for interface in list(providedBy(x))]
['I4', 'I3']
>>> [interface.__name__ for interface in list(providedBy(D1(x)))]
['I4', 'I3', 'I1']
>>> [interface.__name__ for interface in list(providedBy(D2(D1(x))))]
['I4', 'I3', 'I1', 'I2']
"""
def test_providedBy_signature_w_new_style_class():
"""
>>> class X(object):
... implements(I3)
>>> x = X()
>>> int(providedBy(x).__signature__ == implementedBy(X).__signature__)
1
>>> int(providedBy(Wrapper(x)).__signature__ ==
... implementedBy(X).__signature__)
1
>>> directlyProvides(x, I4)
>>> int(providedBy(x).__signature__ ==
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... )
... )
1
>>> int(providedBy(D1(x)).__signature__ ==
... (
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... ),
... implementedBy(D1).__signature__,
... )
... )
1
>>> int(providedBy(D2(D1(x))).__signature__ ==
... (
... (
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... ),
... implementedBy(D1).__signature__,
... ),
... implementedBy(D2).__signature__,
... )
... )
1
"""
def test_providedBy_signature_w_classic_class():
"""
>>> class X:
... implements(I3)
>>> x = X()
>>> int(providedBy(x).__signature__ == implementedBy(X).__signature__)
1
>>> int(providedBy(Wrapper(x)).__signature__ ==
... implementedBy(X).__signature__)
1
>>> directlyProvides(x, I4)
>>> int(providedBy(x).__signature__ ==
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... )
... )
1
>>> int(providedBy(D1(x)).__signature__ ==
... (
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... ),
... implementedBy(D1).__signature__,
... )
... )
1
>>> int(providedBy(D2(D1(x))).__signature__ ==
... (
... (
... (directlyProvidedBy(x).__signature__,
... implementedBy(X).__signature__,
... ),
... implementedBy(D1).__signature__,
... ),
... implementedBy(D2).__signature__,
... )
... )
1
"""
def test_ContextWrapper_basic():
"""
>>> from zope.security.checker import ProxyFactory
>>> from zope.context import ContainmentIterator
>>> from zope.app.context import ContextWrapper
>>> from zope.context import getWrapperData
>>> class C:
... def __init__(self, name): self.name = name
... def __repr__(self): return self.name
>>> c1 = C('c1')
>>> c2 = C('c2')
>>> p2 = ProxyFactory(c2)
>>> w2 = ContextWrapper(p2, c1, name=2)
>>> int(type(w2) is type(p2))
1
>>> getWrapperData(w2)
{'name': 2}
>>> c3 = C('c3')
>>> p3 = ProxyFactory(c3)
>>> w3 = ContextWrapper(p3, w2, name=3)
>>> int(type(w3) is type(p3))
1
>>> getWrapperData(w3)
{'name': 3}
>>> list(ContainmentIterator(w3))
[c3, c2, c1]
>>> w3x = ContextWrapper(w3, w2, name='x')
>>> int(w3x is w3)
1
>>> getWrapperData(w3)
{'name': 'x'}
"""
def test_ContextWrapper_w_adapter():
"""
>>> from zope.interface import *
>>> from zope.app.tests.placelesssetup import setUp, tearDown
>>> from zope.security.checker import ProxyFactory, NamesChecker
>>> from zope.context import ContainmentIterator
>>> from zope.app.context import ContextWrapper
>>> from zope.context import getWrapperData
>>> from zope.component.adapter import provideAdapter
>>> from zope.app.interfaces.context import IZopeContextWrapper
>>> setUp()
>>> checker = NamesChecker(['x'])
>>> class I(Interface):
... pass
>>> class D(Wrapper):
... x=1
>>> class C:
... implements(I)
... def __init__(self, name): self.name = name
... def __repr__(self): return self.name
>>> c1 = C('c1')
>>> c2 = C('c2')
>>> p2 = ProxyFactory(c2, checker)
No adapter, so we get the default Wrapper:
>>> w2 = ContextWrapper(p2, c1, name=2)
>>> int(type(w2) is type(p2))
1
>>> getWrapperData(w2)
{'name': 2}
which means we don't have an x attr
>>> getattr(w2, 'x', None)
No add an adapter:
>>> provideAdapter(I, IZopeContextWrapper, D)
>>> c3 = C('c3')
>>> p3 = ProxyFactory(c3, checker)
>>> w3 = ContextWrapper(p3, w2, name=3)
>>> int(type(w3) is type(p3))
1
>>> getWrapperData(w3)
{'name': 3}
Now we have an x, because we have D as our wrapper type:
>>> getattr(w3, 'x', None)
1
But note that if we get another wrapper around w2, we'll use the
same type:
>>> w = ContextWrapper(w2, C(''))
>>> getattr(w, 'x', None)
"""
class Thing:
pass
def test_pickle_prevention():
"""
>>> w = Wrapper(Thing(), 2)
>>> pickle.dumps(w)
Traceback (most recent call last):
...
PicklingError: Zope context wrappers cannot be pickled
"""
def test_reduce_in_subclass():
"""
>>> class CustomPicklingError(pickle.PicklingError):
... pass
>>> class WrapperWithReduce(Wrapper):
... def __reduce__(self):
... raise CustomPicklingError
... def __reduce_ex__(self, proto):
... raise CustomPicklingError
>>> w = WrapperWithReduce(Thing())
>>> pickle.dumps(w)
Traceback (most recent call last):
...
CustomPicklingError
"""
def test_SecurityCheckerDescriptor():
"""Descriptor for a Wrapper that provides a decorated security checker.
>>> from zope.security.checker import defineChecker, NamesChecker, NoProxy
>>> from zope.context import Wrapper
>>> from zope.app.context import DecoratedSecurityCheckerDescriptor
>>> class MyWrapper(Wrapper):
... __Security_checker__ = DecoratedSecurityCheckerDescriptor()
>>> class Foo:
... a = 1
... b = 2
... c = 3
>>> defineChecker(Foo, NamesChecker(['a']))
>>> defineChecker(MyWrapper, NoProxy)
>>> w = MyWrapper(Foo())
>>> from zope.security.checker import selectChecker
>>> print selectChecker(w)
None
>>> c = w.__Security_checker__
>>> print type(c)
<class 'zope.security.checker.Checker'>
>>> c.check_getattr(w, 'a')
>>> c.check_getattr(w, 'b')
Traceback (most recent call last):
...
ForbiddenAttribute: b
>>> c.check_getattr(w, 'c')
Traceback (most recent call last):
...
ForbiddenAttribute: c
>>> class MyWrapper2(Wrapper):
... __Security_checker__ = DecoratedSecurityCheckerDescriptor()
>>> defineChecker(MyWrapper2, NamesChecker(['b']))
>>> w = MyWrapper2(Foo())
>>> c = w.__Security_checker__
>>> print type(c)
<class 'zope.security.checker.CombinedChecker'>
>>> c.check_getattr(w, 'a')
>>> c.check_getattr(w, 'b')
>>> c.check_getattr(w, 'c')
Traceback (most recent call last):
...
ForbiddenAttribute: c
>>> w = MyWrapper(None)
>>> int(w.__Security_checker__ is None)
1
>>> w = MyWrapper2(None)
>>> type(w.__Security_checker__)
<class 'zope.security.checker.Checker'>
"""
def test_suite():
suite = DocTestSuite()
suite.addTest(DocTestSuite('zope.app.context'))
return suite
if __name__ == '__main__':
unittest.main()
=== Zope3/src/zope/app/tests/placelesssetup.py 1.4 => 1.4.4.1 ===
--- Zope3/src/zope/app/tests/placelesssetup.py:1.4 Sat May 3 12:33:39 2003
+++ Zope3/src/zope/app/tests/placelesssetup.py Sun Jun 22 10:23:32 2003
@@ -18,25 +18,34 @@
"""
from zope.component.tests.placelesssetup \
- import PlacelessSetup as CAPlacelessSetup
+ import PlacelessSetup as CAPlacelessSetup
from zope.app.component.tests.placelesssetup \
- import PlacelessSetup as ACAPlacelessSetup
+ import PlacelessSetup as ACAPlacelessSetup
from zope.app.event.tests.placelesssetup \
- import PlacelessSetup as EventPlacelessSetup
+ import PlacelessSetup as EventPlacelessSetup
from zope.app.i18n.tests.placelesssetup \
- import PlacelessSetup as I18nPlacelessSetup
+ import PlacelessSetup as I18nPlacelessSetup
+from zope.app.container.tests.placelesssetup \
+ import PlacelessSetup as ContainerPlaclessSetup
from zope.app.security._protections import protect
class PlacelessSetup(CAPlacelessSetup,
ACAPlacelessSetup,
EventPlacelessSetup,
I18nPlacelessSetup,
+ ContainerPlaclessSetup
):
def setUp(self):
CAPlacelessSetup.setUp(self)
+ ContainerPlaclessSetup.setUp(self)
ACAPlacelessSetup.setUp(self)
EventPlacelessSetup.setUp(self)
I18nPlacelessSetup.setUp(self)
# Register app-specific security declarations
protect()
+
+ps = PlacelessSetup()
+setUp = ps.setUp
+tearDown = ps.tearDown
+del ps
=== Zope3/src/zope/app/tests/test_attributeannotations.py 1.3 => 1.3.10.1 ===
--- Zope3/src/zope/app/tests/test_attributeannotations.py:1.3 Thu May 1 15:35:37 2003
+++ Zope3/src/zope/app/tests/test_attributeannotations.py Sun Jun 22 10:23:32 2003
@@ -22,9 +22,10 @@
from zope.app.tests.annotations import Annotations
from zope.app.attributeannotations import AttributeAnnotations
from zope.app.interfaces.annotation import IAttributeAnnotatable
+from zope.interface import implements
class Dummy:
- __implements__ = IAttributeAnnotatable
+ implements(IAttributeAnnotatable)
class Test(CleanUp, Annotations, TestCase):
=== Zope3/src/zope/app/tests/test_clipboard.py 1.5 => 1.5.10.1 ===
--- Zope3/src/zope/app/tests/test_clipboard.py:1.5 Thu May 1 15:35:37 2003
+++ Zope3/src/zope/app/tests/test_clipboard.py Sun Jun 22 10:23:32 2003
@@ -50,7 +50,7 @@
user = auth.getPrincipalByLogin('srichter')
annotationsvc = getService(self, 'PrincipalAnnotation')
- annotations = annotationsvc.getAnnotation(user)
+ annotations = annotationsvc.getAnnotations(user)
clipboard = getAdapter(annotations, IPrincipalClipboard)
clipboard.addItems('move', ['bla', 'bla/foo', 'bla/bar'])
expected = ({'action':'move', 'target':'bla'},
@@ -67,7 +67,7 @@
user = auth.getPrincipalByLogin('srichter')
annotationsvc = getService(self, 'PrincipalAnnotation')
- annotations = annotationsvc.getAnnotation(user)
+ annotations = annotationsvc.getAnnotations(user)
clipboard = getAdapter(annotations, IPrincipalClipboard)
expected = ({'action':'move', 'target':'bla'},
@@ -83,7 +83,7 @@
auth = self._auth
user = auth.getPrincipalByLogin('srichter')
annotationsvc = getService(self, 'PrincipalAnnotation')
- annotations = annotationsvc.getAnnotation(user)
+ annotations = annotationsvc.getAnnotations(user)
clipboard = getAdapter(annotations, IPrincipalClipboard)
clipboard.clearContents()
self.failUnless(clipboard.getContents() == ())
=== Zope3/src/zope/app/tests/test_dependable.py 1.2 => 1.2.26.1 ===
--- Zope3/src/zope/app/tests/test_dependable.py:1.2 Wed Dec 25 09:13:26 2002
+++ Zope3/src/zope/app/tests/test_dependable.py Sun Jun 22 10:23:32 2003
@@ -11,38 +11,36 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""XXX short summary goes here.
-
-XXX longer description goes here.
+"""Unit tests for Dependable class.
$Id$
"""
from unittest import TestCase, TestSuite, main, makeSuite
-from zope.app.attributeannotations \
- import AttributeAnnotations
+from zope.app.attributeannotations import AttributeAnnotations
from zope.app.tests.placelesssetup import PlacelessSetup
+
class C:pass
class Test(PlacelessSetup, TestCase):
-
- def _Test__new(self):
+ def factory(self):
from zope.app.dependable import Dependable
return Dependable(AttributeAnnotations(C()))
def testVerifyInterface(self):
from zope.interface.verify import verifyObject
from zope.app.interfaces.dependable import IDependable
- object = self._Test__new()
+ object = self.factory()
verifyObject(IDependable, object)
- def test(self):
- dependable = self._Test__new()
+ def testBasic(self):
+ dependable = self.factory()
self.failIf(dependable.dependents())
dependable.addDependent('/a/b')
dependable.addDependent('/c/d')
dependable.addDependent('/c/e')
+ dependable.addDependent('/c/d')
dependents = list(dependable.dependents())
dependents.sort()
self.assertEqual(dependents, ['/a/b', '/c/d', '/c/e'])
@@ -50,6 +48,24 @@
dependents = list(dependable.dependents())
dependents.sort()
self.assertEqual(dependents, ['/a/b', '/c/e'])
+ dependable.removeDependent('/c/d')
+ dependents = list(dependable.dependents())
+ dependents.sort()
+ self.assertEqual(dependents, ['/a/b', '/c/e'])
+
+ def testRelativeAbsolute(self):
+ obj = self.factory()
+ # Hack the object to have a parent path
+ obj.pp = "/a/"
+ obj.pplen = len(obj.pp)
+ obj.addDependent("foo")
+ self.assertEqual(obj.dependents(), ("/a/foo",))
+ obj.removeDependent("/a/foo")
+ self.assertEqual(obj.dependents(), ())
+ obj.addDependent("/a/bar")
+ self.assertEqual(obj.dependents(), ("/a/bar",))
+ obj.removeDependent("bar")
+ self.assertEqual(obj.dependents(), ())
def test_suite():
return TestSuite((
=== Zope3/src/zope/app/tests/test_introspector.py 1.2 => 1.2.26.1 ===
--- Zope3/src/zope/app/tests/test_introspector.py:1.2 Wed Dec 25 09:13:26 2002
+++ Zope3/src/zope/app/tests/test_introspector.py Sun Jun 22 10:23:32 2003
@@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-from zope.interface import Interface, Attribute
+from zope.interface import Interface, Attribute, implements
class ITestClass(Interface):
def drool():
@@ -23,7 +23,7 @@
class TestClass(BaseTestClass):
"""This is my stupid doc string"""
- __implements__ = ITestClass
+ implements(ITestClass)
def drool(self):
pass