[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/tests - testRequestDataProperty.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 25 Mar 2002 18:27:09 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/tests
In directory cvs.zope.org:/tmp/cvs-serv30921/tests

Added Files:
      Tag: Zope3-publisher-refactor-branch
	testRequestDataProperty.py 
Log Message:
Added module to support read-only request properties

=== Added File Zope3/lib/python/Zope/Publisher/tests/testRequestDataProperty.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
# 
##############################################################################
"""

Revision information:
$Id: testRequestDataProperty.py,v 1.1.2.1 2002/03/25 23:27:09 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite

from Interface.Common.tests.BaseTestMapping \
     import testIEnumerableMapping, testIReadMapping

from Zope.Publisher.RequestDataProperty \
     import RequestDataProperty, RequestDataGetter, RequestDataMapper

class TestDataGettr(RequestDataGetter): _gettrname = 'getSomething'
class TestDataMapper(RequestDataMapper): _mapname = '_data'

_marker = object()
class Data(object):

    def getSomething(self, name, default=_marker):
        if name.startswith('Z'):
            return "something %s" % name

        if default is not _marker:
            return default

        raise KeyError, name

    something = RequestDataProperty(TestDataGettr)
    somedata = RequestDataProperty(TestDataMapper)

class Test(TestCase):

    def testRequestDataGettr(self):
        testIReadMapping(self, Data().something,
                         {"Zope": "something Zope"}, ["spam"])

    def testRequestDataMapper(self):
        data = Data()
        sample = {'foo': 'Foo', 'bar': 'Bar'}
        data._data = sample
        inst = data.somedata
        testIReadMapping(self, inst, sample, ["spam"])
        testIEnumerableMapping(self, inst, sample)

    def testNoAssign(self):
        data = Data()
        try: data.something = {}
        except AttributeError: pass
        else: raise """Shouldn't be able to assign"""
        try: data.somedata = {}
        except AttributeError: pass
        else: raise """Shouldn't be able to assign"""

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

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