[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ZopePublication/tests - testZopePublication.py:1.1.2.1

Chris McDonough chrism@zope.com
Fri, 30 Nov 2001 14:59:47 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/ZopePublication/tests
In directory cvs.zope.org:/tmp/cvs-serv27401/tests

Added Files:
      Tag: Zope-3x-branch
	testZopePublication.py 
Log Message:
Added context wrapping of returned objects to traverseName and getDefaultTraversal.


=== Added File Zope3/lib/python/Zope/App/ZopePublication/tests/testZopePublication.py ===
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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.

import unittest

from Zope.Publisher.Publish import publish
from Zope.Publisher.BaseRequest import BaseRequest
from Zope.Publisher.BaseResponse import BaseResponse
from Zope.App.ZopePublication.ZopePublication import ZopePublication
from Zope.App.ZopePublication.ZopePublication import BrowserPublication
from Zope.Configuration.name import resolve
from Zope.Publisher.Browser.IBrowserPublisher import IBrowserPublisher
from Zope.Publisher.DefaultPublication import DefaultPublication
from Zope.Publisher.IPublication import IPublication
from Zope.ContextWrapper import Wrapper, getobject
import ZODB
from ZODB.MappingStorage import MappingStorage
from Zope.Publisher.Exceptions import Retry

from Interface import verify, instancesOfObjectImplements

from StringIO import StringIO

class DummyPublished:
    __implements__ = IBrowserPublisher
    def browser_traverse(self, request, name):
        if name == 'bruce':
            return self.__class__()
        raise KeyError, name

    def browser_default(self, request):
        return self, ['bruce']
    
class BasePublicationTests(unittest.TestCase):
    def setUp(self):
        self.db = ZODB.DB(MappingStorage('foo'))
    
    def testInterfacesVerify(self):
        for interface in instancesOfObjectImplements(self.klass):
            verify(interface, DefaultPublication)


class ZopePublicationTests(BasePublicationTests):
    klass = ZopePublication
    
class BrowserPublicationTests(BasePublicationTests):
    klass = BrowserPublication
    def testNativeTraverseNameWrapping(self):
        pub = self.klass(self.db)
        ob = DummyPublished()
        ob2 = pub.traverseName(self._createRequest('/bruce'), ob, 'bruce')
        self.failUnless(ob2 is not ob)
        self.failUnless(type(ob2) is Wrapper)

    def testAdaptedTraverseNameWrapping(self):
        from Interface import Interface
        class I1(Interface):
            pass

        class mydict(dict):
            __implements__ = I1
            
        class Adapter:
            __implements__ = IBrowserPublisher
            def __init__(self, context):
                self.context = context
                self.counter = 0
                
            def browser_traverse(self, request, name):
                self.counter+=1
                return self.context[name]

        from Zope.ComponentArchitecture import providePresentation
        providePresentation(I1, '_traverse', IBrowserPublisher, Adapter)
        ob = mydict()
        ob['bruce'] =  'bruce'
        ob['bruce2'] =  'bruce2'
        pub = self.klass(self.db)
        ob2 = pub.traverseName(self._createRequest('/bruce'), ob, 'bruce')
        self.failUnless(type(ob2) is Wrapper)
        unw = getobject(ob2)
        self.assertEqual(unw, 'bruce')

    def testAdaptedTraverseDefaultWrapping(self):
        from Interface import Interface
        class I1(Interface):
            pass

        class mydict(dict):
            __implements__ = I1
            
        class Adapter:
            __implements__ = IBrowserPublisher
            def __init__(self, context):
                self.context = context
                self.counter = 0
                
            def browser_traverse(self, request, name):
                self.counter+=1
                return self.context[name]

        from Zope.ComponentArchitecture import providePresentation
        providePresentation(I1, '_traverse', IBrowserPublisher, Adapter)
        ob = mydict()
        ob['bruce'] =  'bruce'
        ob['bruce2'] =  'bruce2'
        pub = self.klass(self.db)
        ob2,x = pub.getDefaultTraversal(self._createRequest('/bruce'), ob)
        self.failUnless(type(ob2) is Wrapper)
        unw = getobject(ob2)
        self.assertEqual(unw, 'bruce')

def test_suite():
    t1 = unittest.makeSuite(ZopePublicationTests, 'test')
    t2 = unittest.makeSuite(BrowserPublicationTests, 'test')
    return unittest.TestSuite((t1, t2))

if __name__=='__main__':
    unittest.TextTestRunner().run( test_suite() )