1##############################################################################
2#
3# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
4# All Rights Reserved.
5#
6# This software is subject to the provisions of the Zope Public License,
7# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11# FOR A PARTICULAR PURPOSE.
12#
13##############################################################################
14"""Request Data-Property Tests
15"""
16from unittest import TestCase, main, makeSuite
17
18from zope.interface.common.tests.basemapping \
19     import testIEnumerableMapping, testIReadMapping
20
21from zope.publisher.base \
22     import RequestDataProperty, RequestDataGetter, RequestDataMapper
23
24class TestDataGettr(RequestDataGetter): _gettrname = 'getSomething'
25class TestDataMapper(RequestDataMapper): _mapname = '_data'
26
27_marker = object()
28class Data(object):
29
30    def getSomething(self, name, default=_marker):
31        if name.startswith('Z'):
32            return "something %s" % name
33
34        if default is not _marker:
35            return default
36
37        raise KeyError(name)
38
39    something = RequestDataProperty(TestDataGettr)
40    somedata = RequestDataProperty(TestDataMapper)
41
42class Test(TestCase):
43
44    def testRequestDataGettr(self):
45        testIReadMapping(self, Data().something,
46                         {"Zope": "something Zope"}, ["spam"])
47
48    def testRequestDataMapper(self):
49        data = Data()
50        sample = {'foo': 'Foo', 'bar': 'Bar'}
51        data._data = sample
52        inst = data.somedata
53        testIReadMapping(self, inst, sample, ["spam"])
54        testIEnumerableMapping(self, inst, sample)
55
56    def testNoAssign(self):
57        data = Data()
58        try:
59            data.something = {}
60        except AttributeError:
61            pass
62        else:
63            raise AssertionError("Shouldn't be able to assign")
64
65        try:
66            data.somedata = {}
67        except AttributeError:
68            pass
69        else:
70            raise AssertionError("Shouldn't be able to assign")
71
72def test_suite():
73    return makeSuite(Test)
74
75if __name__=='__main__':
76    main(defaultTest='test_suite')
77