1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.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
#
##############################################################################
"""Traversal Namespace Tests
"""
import re
import unittest
from doctest import DocTestSuite
from zope.component.testing import PlacelessSetup
from zope.component.testing import setUp
from zope.component.testing import tearDown
from zope.location.interfaces import LocationError
from zope.testing.renormalizing import RENormalizing
from zope import component
from zope import interface
from zope.traversing import namespace
class TestSimpleHandler(unittest.TestCase):
def test_constructor(self):
h = namespace.SimpleHandler(42)
self.assertEqual(h.context, 42)
h = namespace.SimpleHandler(42, 43)
self.assertEqual(h.context, 42)
class TestFunctions(unittest.TestCase):
def test_getResource_not_found(self):
with self.assertRaises(LocationError):
namespace.getResource(None, '', None)
class TestAcquire(unittest.TestCase):
def test_excessive_depth_on_path_extension(self):
from zope.traversing.interfaces import ITraversable
from zope.traversing.namespace import ExcessiveDepth
@interface.implementer(ITraversable)
class Context(object):
max_call_count = 200
def traverse(self, name, path):
if self.max_call_count:
path.append("I added something")
self.max_call_count -= 1
acq = namespace.acquire(Context(), None)
with self.assertRaises(ExcessiveDepth):
acq.traverse(None, None)
class TestEtc(PlacelessSetup, unittest.TestCase):
def test_traverse_non_site_name(self):
with self.assertRaises(LocationError) as ctx:
namespace.etc(None, None).traverse('foobar', ())
ex = ctx.exception
self.assertEqual((None, 'foobar'), ex.args)
def test_traverse_site_no_manager(self):
test = self
class Context(object):
def __getattribute__(self, name):
test.assertEqual(name, 'getSiteManager')
return None
context = Context()
with self.assertRaises(LocationError) as ctx:
namespace.etc(context, None).traverse('site', ())
ex = ctx.exception
self.assertEqual((context, 'site'), ex.args)
def test_traverse_site_lookup_error(self):
class Context(object):
called = False
def getSiteManager(self):
self.called = True
from zope.component import ComponentLookupError
raise ComponentLookupError()
context = Context()
with self.assertRaises(LocationError) as ctx:
namespace.etc(context, None).traverse('site', ())
ex = ctx.exception
self.assertEqual((context, 'site'), ex.args)
self.assertTrue(context.called)
def test_traverse_utility(self):
from zope.traversing.interfaces import IEtcNamespace
component.provideUtility(self, provides=IEtcNamespace, name='my etc name')
result = namespace.etc(None, None).traverse('my etc name', ())
self.assertIs(result, self)
class TestView(unittest.TestCase):
def test_not_found(self):
with self.assertRaises(LocationError) as ctx:
namespace.view(None, None).traverse('name', ())
ex = ctx.exception
self.assertEqual((None, 'name'), ex.args)
class TestVh(unittest.TestCase):
assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex',
getattr(unittest.TestCase, 'assertRaisesRegexp'))
def test_invalid_vh(self):
with self.assertRaisesRegex(ValueError,
'Vhost directive should have the form'):
namespace.vh(None, None).traverse(u'invalid name', ())
def test_suite():
checker = RENormalizing([
# Python 3 includes module name in exceptions
(re.compile(r"zope.location.interfaces.LocationError"),
"LocationError"),
])
suite = unittest.defaultTestLoader.loadTestsFromName(__name__)
suite.addTest(DocTestSuite(
'zope.traversing.namespace',
setUp=setUp, tearDown=tearDown,
checker=checker))
return suite
|