[Zope3-checkins] CVS: Zope3/src/zope/component/tests -
test_presentation.py:1.1.2.1
Jim Fulton
cvs-admin at zope.org
Mon Nov 10 10:00:44 EST 2003
Update of /cvs-repository/Zope3/src/zope/component/tests
In directory cvs.zope.org:/tmp/cvs-serv19095/src/zope/component/tests
Added Files:
Tag: adaptergeddon-branch
test_presentation.py
Log Message:
Oops, forgot to add these files when I created the global presentation
service.
=== Added File Zope3/src/zope/component/tests/test_presentation.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.
$Id: test_presentation.py,v 1.1.2.1 2003/11/10 15:00:43 jim Exp $
"""
import unittest
from doctest import DocTestSuite
from zope.component.presentation import GlobalPresentationService
import zope.interface
class IRequest(zope.interface.Interface):
"Demonstration request type"
class Request:
zope.interface.implements(IRequest)
def getPresentationSkin(self):
return getattr(self, 'skin', None)
class IContact(zope.interface.Interface):
"Demonstration content type"
class Contact:
zope.interface.implements(IContact)
class MyView:
def __init__(self, context, request):
self.context, self.request = context, request
def test_view_lookup_fails_w_wrong_skin():
"""
>>> s = GlobalPresentationService()
>>> s.defineLayer('custom')
>>> s.defineSkin('custom', ['custom', 'default'])
>>> request = Request()
>>> s.provideAdapter(IRequest, [MyView], contexts=[IContact], name='foo',
... layer='custom')
>>> c = Contact()
>>> s.queryView(c, 'foo', request)
We don't get anothing because we registered in the custom layer,
but looked up in the default skin which has only the default layer.
"""
class ICompany(zope.interface.Interface):
"Demonstration content type"
class Company:
zope.interface.implements(ICompany)
class ContactInCompanyView:
def __init__(self, contact, company, request):
self.contact, self.company, self.request = contact, company, request
class IFamily(zope.interface.Interface):
"Demonstration content type"
class Family:
zope.interface.implements(IFamily)
class ContactInFamilyView:
def __init__(self, contact, family, request):
self.contact, self.family, self.request = contact, family, request
def test_multi_views():
"""A multi-view is a view on multiple objects
>>> s = GlobalPresentationService()
>>> request = Request()
>>> s.provideAdapter(IRequest, [ContactInCompanyView],
... contexts=[IContact, ICompany], name='foo')
>>> bob = Contact()
>>> acme = Company()
>>> v = s.queryMultiView((bob, acme), 'foo', request)
>>> v.__class__.__name__
'ContactInCompanyView'
>>> v.contact is bob
True
>>> v.company is acme
True
>>> v.request is request
True
>>> s.provideAdapter(IRequest, [ContactInFamilyView],
... contexts=[IContact, IFamily], name='foo')
>>> smith = Family()
>>> v = s.queryMultiView((bob, smith), 'foo', request)
>>> v.__class__.__name__
'ContactInFamilyView'
>>> v.contact is bob
True
>>> v.family is smith
True
>>> v.request is request
True
"""
def test_provideView():
"""
The provideView is a simpler and backward-compatible interface to
provideAdapter.
>>> s = GlobalPresentationService()
>>> request = Request()
>>> s.provideView(IContact, 'foo', IRequest, [MyView])
>>> c = Contact()
>>> v = s.queryView(c, 'foo', request)
>>> v.__class__.__name__
'MyView'
>>> v.request is request
True
>>> v.context is c
True
We can specify a layer and we can provide a view factory directly:
>>> s.defineLayer('custom')
>>> s.defineSkin('custom', ['custom', 'default'])
>>> s.provideView(IContact, 'index.html', IRequest, MyView, layer='custom')
>>> c = Contact()
>>> request.skin = 'custom'
>>> v = s.queryView(c, 'foo', request)
>>> v.__class__.__name__
'MyView'
>>> v.request is request
True
>>> v.context is c
True
"""
def test_default_view_names():
"""
>>> s = GlobalPresentationService()
>>> request = Request()
>>> c = Contact()
We haven't set a default view name:
>>> s.queryDefaultViewName(c, request)
Let's set a "default default":
>>> s.setDefaultViewName(None, IRequest, 'index.html')
And then we'll get it is we look something up:
>>> s.queryDefaultViewName(c, request)
'index.html'
Now we'll set a name for a specific interface. We'll also specify
a specifioc layer:
>>> s.defineLayer('custom')
>>> s.defineSkin('custom', ['custom', 'default'])
>>> s.setDefaultViewName(IContact, IRequest, 'hello.html', layer='custom')
If we don't specify the custum skin, we'll still get the default:
>>> s.queryDefaultViewName(c, request)
'index.html'
But if we specify a custom skin, we'll get the custom value for a contact:
>>> request.skin = 'custom'
>>> s.queryDefaultViewName(c, request)
'hello.html'
But not for something else:
>>> s.queryDefaultViewName(42, request)
'index.html'
"""
def test_default_skin_affects_lookup():
"""
>>> s = GlobalPresentationService()
>>> s.defineLayer('custom')
>>> s.defineSkin('custom', ['custom', 'default'])
>>> request = Request()
>>> class MyResource:
... def __init__(self, request):
... self.request = request
>>> s.provideAdapter(IRequest, [MyResource], name='foo', layer='custom')
>>> s.queryResource('foo', request)
>>> s.provideAdapter(IRequest, [MyView], contexts=[IContact], name='foo',
... layer='custom')
>>> c = Contact()
>>> v = s.queryView(c, 'foo', request)
>>> s.setDefaultSkin('custom')
>>> r = s.queryResource('foo', request)
>>> r.__class__.__name__
'MyResource'
>>> r.request is request
True
>>> v = s.queryView(c, 'foo', request)
>>> v.__class__.__name__
'MyView'
>>> v.request is request
True
>>> v.context is c
True
"""
def test_suite():
return unittest.TestSuite((
DocTestSuite('zope.component.presentation'),
DocTestSuite(),
))
if __name__ == '__main__': unittest.main()
More information about the Zope3-Checkins
mailing list