[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ZopePublication/TraversalViews/tests - __init__.py:1.1 testAbsoluteURL.py:1.1 testObjectName.py:1.1

Steve Alexander steve@cat-box.net
Thu, 13 Jun 2002 12:20:10 -0400


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

Added Files:
	__init__.py testAbsoluteURL.py testObjectName.py 
Log Message:
Moved AbsoluteURL into a TraversalViews package, to cover both
AbsoluteURL and also ObjectName.

ObjectName is sort of like a minimal version of AbsoluteURL in that it
gets you the name the current object was traversed via.
It can be used as a view, as @@object_name or by getting an adapter 
for IObjectName, as in getAdapter(obj, IObjectName)().

Also, I added a __call__ method to IAbsoluteURL, as it was needed for
security declarations.


=== Added File Zope3/lib/python/Zope/App/ZopePublication/TraversalViews/tests/__init__.py ===
# empty __init__.py file to make this a package


=== Added File Zope3/lib/python/Zope/App/ZopePublication/TraversalViews/tests/testAbsoluteURL.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""Test the AbsoluteURL view

Revision information:
$Id: testAbsoluteURL.py,v 1.1 2002/06/13 16:20:09 stevea Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup

from Zope.ComponentArchitecture import getService, getView
from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation
from Zope.Publisher.HTTP.tests.TestRequest import TestRequest
from Zope.Proxy.ContextWrapper import ContextWrapper
from Interface import Interface

class IRoot(Interface): pass

class Root:
    __implements__ = IRoot
    
class TrivialContent(object):
    """Trivial content object, used because instances of object are rocks."""

class Test(PlacelessSetup, TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        from Zope.App.ZopePublication.TraversalViews.AbsoluteURL \
             import AbsoluteURL, SiteAbsoluteURL
        provideView=getService(None,"Views").provideView
        provideView(None, 'absolute_url', IBrowserPresentation,
                    [AbsoluteURL])
        provideView(IRoot, 'absolute_url', IBrowserPresentation,
                    [SiteAbsoluteURL])

    def testBadObject(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)
        view = getView(None, 'absolute_url', request)
        self.assertRaises(TypeError, view.__str__)
        
    def testNoContext(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)
        view = getView(Root(), 'absolute_url', request)
        self.assertEqual(str(view), 'http://foobar.com')
        
    def testBasicContext(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)

        content = ContextWrapper(TrivialContent(), Root(), name='a')
        content = ContextWrapper(TrivialContent(), content, name='b')
        content = ContextWrapper(TrivialContent(), content, name='c')
        view = getView(content, 'absolute_url', request)
        self.assertEqual(str(view), 'http://foobar.com/a/b/c')

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')



=== Added File Zope3/lib/python/Zope/App/ZopePublication/TraversalViews/tests/testObjectName.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""Test the AbsoluteURL view

Revision information:
$Id: testObjectName.py,v 1.1 2002/06/13 16:20:09 stevea Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup

from Zope.ComponentArchitecture import getService, getView, getAdapter
from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation
from Zope.Publisher.HTTP.tests.TestRequest import TestRequest
from Zope.Proxy.ContextWrapper import ContextWrapper
from Interface import Interface

from Zope.App.ZopePublication.TraversalViews.ObjectName \
    import IObjectName, ObjectName, SiteObjectName


class IRoot(Interface): pass

class Root:
    __implements__ = IRoot
    
class TrivialContent(object):
    """Trivial content object, used because instances of object are rocks."""

class Test(PlacelessSetup, TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        provideView = getService(None, "Views").provideView
        provideView(None, 'object_name', IBrowserPresentation,
                    [ObjectName])
        provideView(IRoot, 'object_name', IBrowserPresentation,
                    [SiteObjectName])
                    
        provideAdapter = getService(None, "Adapters").provideAdapter
        provideAdapter(None, IObjectName, [ObjectName])
        provideAdapter(IRoot, IObjectName, [ObjectName])

    def testViewBadObject(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)
        view = getView(None, 'object_name', request)
        self.assertRaises(TypeError, view.__str__)
        
    def testViewNoContext(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)
        view = getView(Root(), 'object_name', request)
        self.assertRaises(TypeError, str(view))
        
    def testViewBasicContext(self):
        request = TestRequest()
        request.setViewType(IBrowserPresentation)

        content = ContextWrapper(TrivialContent(), Root(), name='a')
        content = ContextWrapper(TrivialContent(), content, name='b')
        content = ContextWrapper(TrivialContent(), content, name='c')
        view = getView(content, 'object_name', request)
        self.assertEqual(str(view), 'c')

    def testAdapterBadObject(self):
        adapter = getAdapter(None, IObjectName)
        self.assertRaises(TypeError, adapter)
        
    def testAdapterNoContext(self):
        adapter = getAdapter(Root(), IObjectName)
        self.assertRaises(TypeError, adapter)
    
    def testAdapterBasicContext(self):
        content = ContextWrapper(TrivialContent(), Root(), name='a')
        content = ContextWrapper(TrivialContent(), content, name='b')
        content = ContextWrapper(TrivialContent(), content, name='c')
        adapter = getAdapter(content, IObjectName)
        self.assertEqual(adapter(), 'c')
        
def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')