commit | author | age
|
3fd912
|
1 |
import unittest |
CM |
2 |
|
971537
|
3 |
class TestRegistry(unittest.TestCase): |
CM |
4 |
def _getTargetClass(self): |
b60bdb
|
5 |
from pyramid.registry import Registry |
971537
|
6 |
return Registry |
CM |
7 |
|
f6af10
|
8 |
def _makeOne(self, *args, **kw): |
MM |
9 |
return self._getTargetClass()(*args, **kw) |
971537
|
10 |
|
79ef3d
|
11 |
def test___nonzero__(self): |
CM |
12 |
registry = self._makeOne() |
|
13 |
self.assertEqual(registry.__nonzero__(), True) |
|
14 |
|
c15cbc
|
15 |
def test__lock(self): |
CM |
16 |
registry = self._makeOne() |
|
17 |
self.assertTrue(registry._lock) |
|
18 |
|
|
19 |
def test_clear_view_cache_lookup(self): |
|
20 |
registry = self._makeOne() |
|
21 |
registry._view_lookup_cache[1] = 2 |
|
22 |
registry._clear_view_lookup_cache() |
|
23 |
self.assertEqual(registry._view_lookup_cache, {}) |
|
24 |
|
398465
|
25 |
def test_package_name(self): |
HB |
26 |
package_name = 'testing' |
f6af10
|
27 |
registry = self._makeOne(package_name) |
398465
|
28 |
self.assertEqual(registry.package_name, package_name) |
HB |
29 |
|
5e20f9
|
30 |
def test_default_package_name(self): |
f6af10
|
31 |
registry = self._makeOne() |
dd3cc8
|
32 |
self.assertEqual(registry.package_name, 'tests') |
5e20f9
|
33 |
|
2bb9b7
|
34 |
def test_registerHandler_and_notify(self): |
971537
|
35 |
registry = self._makeOne() |
7292d4
|
36 |
self.assertEqual(registry.has_listeners, False) |
971537
|
37 |
L = [] |
2bb9b7
|
38 |
def f(event): |
CM |
39 |
L.append(event) |
f850ec
|
40 |
registry.registerHandler(f, [IDummyEvent]) |
7292d4
|
41 |
self.assertEqual(registry.has_listeners, True) |
f850ec
|
42 |
event = DummyEvent() |
2bb9b7
|
43 |
registry.notify(event) |
CM |
44 |
self.assertEqual(L, [event]) |
|
45 |
|
f850ec
|
46 |
def test_registerSubscriptionAdapter(self): |
2bb9b7
|
47 |
registry = self._makeOne() |
7292d4
|
48 |
self.assertEqual(registry.has_listeners, False) |
2bb9b7
|
49 |
from zope.interface import Interface |
f850ec
|
50 |
registry.registerSubscriptionAdapter(DummyEvent, |
CM |
51 |
[IDummyEvent], Interface) |
7292d4
|
52 |
self.assertEqual(registry.has_listeners, True) |
971537
|
53 |
|
5a972b
|
54 |
def test__get_settings(self): |
CM |
55 |
registry = self._makeOne() |
|
56 |
registry._settings = 'foo' |
|
57 |
self.assertEqual(registry.settings, 'foo') |
|
58 |
|
|
59 |
def test__set_settings(self): |
|
60 |
registry = self._makeOne() |
|
61 |
registry.settings = 'foo' |
|
62 |
self.assertEqual(registry._settings, 'foo') |
|
63 |
|
f6af10
|
64 |
def test_init_forwards_args(self): |
MM |
65 |
from zope.interface import Interface |
|
66 |
from zope.interface.registry import Components |
|
67 |
dummy = object() |
|
68 |
c = Components() |
|
69 |
c.registerUtility(dummy, Interface) |
|
70 |
registry = self._makeOne('foo', (c,)) |
|
71 |
self.assertEqual(registry.__name__, 'foo') |
|
72 |
self.assertEqual(registry.getUtility(Interface), dummy) |
|
73 |
|
|
74 |
def test_init_forwards_kw(self): |
|
75 |
from zope.interface import Interface |
|
76 |
from zope.interface.registry import Components |
|
77 |
dummy = object() |
|
78 |
c = Components() |
|
79 |
c.registerUtility(dummy, Interface) |
|
80 |
registry = self._makeOne(bases=(c,)) |
|
81 |
self.assertEqual(registry.getUtility(Interface), dummy) |
|
82 |
|
57a0d7
|
83 |
class TestIntrospector(unittest.TestCase): |
57a9d6
|
84 |
def _getTargetClass(slf): |
57a0d7
|
85 |
from pyramid.registry import Introspector |
57a9d6
|
86 |
return Introspector |
CM |
87 |
|
|
88 |
def _makeOne(self): |
|
89 |
return self._getTargetClass()() |
|
90 |
|
|
91 |
def test_conformance(self): |
|
92 |
from zope.interface.verify import verifyClass |
|
93 |
from zope.interface.verify import verifyObject |
|
94 |
from pyramid.interfaces import IIntrospector |
|
95 |
verifyClass(IIntrospector, self._getTargetClass()) |
|
96 |
verifyObject(IIntrospector, self._makeOne()) |
57a0d7
|
97 |
|
CM |
98 |
def test_add(self): |
|
99 |
inst = self._makeOne() |
|
100 |
intr = DummyIntrospectable() |
|
101 |
inst.add(intr) |
|
102 |
self.assertEqual(intr.order, 0) |
|
103 |
category = {'discriminator':intr, 'discriminator_hash':intr} |
|
104 |
self.assertEqual(inst._categories, {'category':category}) |
|
105 |
|
|
106 |
def test_get_success(self): |
|
107 |
inst = self._makeOne() |
|
108 |
intr = DummyIntrospectable() |
|
109 |
inst.add(intr) |
|
110 |
self.assertEqual(inst.get('category', 'discriminator'), intr) |
|
111 |
|
|
112 |
def test_get_success_byhash(self): |
|
113 |
inst = self._makeOne() |
|
114 |
intr = DummyIntrospectable() |
|
115 |
inst.add(intr) |
|
116 |
self.assertEqual(inst.get('category', 'discriminator_hash'), intr) |
|
117 |
|
|
118 |
def test_get_fail(self): |
|
119 |
inst = self._makeOne() |
|
120 |
intr = DummyIntrospectable() |
|
121 |
inst.add(intr) |
|
122 |
self.assertEqual(inst.get('category', 'wontexist', 'foo'), 'foo') |
|
123 |
|
|
124 |
def test_get_category(self): |
|
125 |
inst = self._makeOne() |
|
126 |
intr = DummyIntrospectable() |
|
127 |
intr2 = DummyIntrospectable() |
|
128 |
intr2.discriminator = 'discriminator2' |
|
129 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
130 |
inst.add(intr2) |
|
131 |
inst.add(intr) |
|
132 |
expected = [ |
|
133 |
{'introspectable':intr2, 'related':[]}, |
|
134 |
{'introspectable':intr, 'related':[]}, |
|
135 |
] |
|
136 |
self.assertEqual(inst.get_category('category'), expected) |
|
137 |
|
79f34b
|
138 |
def test_get_category_returns_default_on_miss(self): |
CM |
139 |
inst = self._makeOne() |
|
140 |
self.assertEqual(inst.get_category('category', '123'), '123') |
|
141 |
|
57a0d7
|
142 |
def test_get_category_with_sortkey(self): |
CM |
143 |
import operator |
|
144 |
inst = self._makeOne() |
|
145 |
intr = DummyIntrospectable() |
|
146 |
intr.foo = 2 |
|
147 |
intr2 = DummyIntrospectable() |
|
148 |
intr2.discriminator = 'discriminator2' |
|
149 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
150 |
intr2.foo = 1 |
|
151 |
inst.add(intr) |
|
152 |
inst.add(intr2) |
|
153 |
expected = [ |
|
154 |
{'introspectable':intr2, 'related':[]}, |
|
155 |
{'introspectable':intr, 'related':[]}, |
|
156 |
] |
|
157 |
self.assertEqual( |
79f34b
|
158 |
inst.get_category('category', sort_key=operator.attrgetter('foo')), |
57a0d7
|
159 |
expected) |
CM |
160 |
|
|
161 |
def test_categorized(self): |
|
162 |
import operator |
|
163 |
inst = self._makeOne() |
|
164 |
intr = DummyIntrospectable() |
|
165 |
intr.foo = 2 |
|
166 |
intr2 = DummyIntrospectable() |
|
167 |
intr2.discriminator = 'discriminator2' |
|
168 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
169 |
intr2.foo = 1 |
|
170 |
inst.add(intr) |
|
171 |
inst.add(intr2) |
|
172 |
expected = [('category', [ |
|
173 |
{'introspectable':intr2, 'related':[]}, |
|
174 |
{'introspectable':intr, 'related':[]}, |
|
175 |
])] |
|
176 |
self.assertEqual( |
79f34b
|
177 |
inst.categorized(sort_key=operator.attrgetter('foo')), expected) |
57a0d7
|
178 |
|
CM |
179 |
def test_categories(self): |
|
180 |
inst = self._makeOne() |
|
181 |
inst._categories['a'] = 1 |
|
182 |
inst._categories['b'] = 2 |
|
183 |
self.assertEqual(list(inst.categories()), ['a', 'b']) |
|
184 |
|
|
185 |
def test_remove(self): |
|
186 |
inst = self._makeOne() |
|
187 |
intr = DummyIntrospectable() |
|
188 |
intr2 = DummyIntrospectable() |
|
189 |
intr2.category_name = 'category2' |
|
190 |
intr2.discriminator = 'discriminator2' |
|
191 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
192 |
inst.add(intr) |
|
193 |
inst.add(intr2) |
|
194 |
inst.relate(('category', 'discriminator'), |
|
195 |
('category2', 'discriminator2')) |
|
196 |
inst.remove('category', 'discriminator') |
|
197 |
self.assertEqual(inst._categories, |
|
198 |
{'category': |
|
199 |
{}, |
|
200 |
'category2': |
|
201 |
{'discriminator2': intr2, |
|
202 |
'discriminator2_hash': intr2} |
|
203 |
}) |
|
204 |
self.assertEqual(inst._refs.get(intr), None) |
|
205 |
self.assertEqual(inst._refs[intr2], []) |
|
206 |
|
|
207 |
def test_remove_fail(self): |
|
208 |
inst = self._makeOne() |
|
209 |
self.assertEqual(inst.remove('a', 'b'), None) |
|
210 |
|
|
211 |
def test_relate(self): |
|
212 |
inst = self._makeOne() |
|
213 |
intr = DummyIntrospectable() |
|
214 |
intr2 = DummyIntrospectable() |
|
215 |
intr2.category_name = 'category2' |
|
216 |
intr2.discriminator = 'discriminator2' |
|
217 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
218 |
inst.add(intr) |
|
219 |
inst.add(intr2) |
|
220 |
inst.relate(('category', 'discriminator'), |
|
221 |
('category2', 'discriminator2')) |
|
222 |
self.assertEqual(inst._categories, |
|
223 |
{'category': |
|
224 |
{'discriminator':intr, |
|
225 |
'discriminator_hash':intr}, |
|
226 |
'category2': |
|
227 |
{'discriminator2': intr2, |
|
228 |
'discriminator2_hash': intr2} |
|
229 |
}) |
|
230 |
self.assertEqual(inst._refs[intr], [intr2]) |
|
231 |
self.assertEqual(inst._refs[intr2], [intr]) |
|
232 |
|
|
233 |
def test_relate_fail(self): |
|
234 |
inst = self._makeOne() |
|
235 |
intr = DummyIntrospectable() |
|
236 |
inst.add(intr) |
|
237 |
self.assertRaises( |
|
238 |
KeyError, |
|
239 |
inst.relate, |
|
240 |
('category', 'discriminator'), |
|
241 |
('category2', 'discriminator2') |
|
242 |
) |
|
243 |
|
|
244 |
def test_unrelate(self): |
|
245 |
inst = self._makeOne() |
|
246 |
intr = DummyIntrospectable() |
|
247 |
intr2 = DummyIntrospectable() |
|
248 |
intr2.category_name = 'category2' |
|
249 |
intr2.discriminator = 'discriminator2' |
|
250 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
251 |
inst.add(intr) |
|
252 |
inst.add(intr2) |
|
253 |
inst.relate(('category', 'discriminator'), |
|
254 |
('category2', 'discriminator2')) |
|
255 |
inst.unrelate(('category', 'discriminator'), |
|
256 |
('category2', 'discriminator2')) |
|
257 |
self.assertEqual(inst._categories, |
|
258 |
{'category': |
|
259 |
{'discriminator':intr, |
|
260 |
'discriminator_hash':intr}, |
|
261 |
'category2': |
|
262 |
{'discriminator2': intr2, |
|
263 |
'discriminator2_hash': intr2} |
|
264 |
}) |
|
265 |
self.assertEqual(inst._refs[intr], []) |
|
266 |
self.assertEqual(inst._refs[intr2], []) |
|
267 |
|
|
268 |
def test_related(self): |
|
269 |
inst = self._makeOne() |
|
270 |
intr = DummyIntrospectable() |
|
271 |
intr2 = DummyIntrospectable() |
|
272 |
intr2.category_name = 'category2' |
|
273 |
intr2.discriminator = 'discriminator2' |
|
274 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
275 |
inst.add(intr) |
|
276 |
inst.add(intr2) |
|
277 |
inst.relate(('category', 'discriminator'), |
|
278 |
('category2', 'discriminator2')) |
|
279 |
self.assertEqual(inst.related(intr), [intr2]) |
|
280 |
|
|
281 |
def test_related_fail(self): |
|
282 |
inst = self._makeOne() |
|
283 |
intr = DummyIntrospectable() |
|
284 |
intr2 = DummyIntrospectable() |
|
285 |
intr2.category_name = 'category2' |
|
286 |
intr2.discriminator = 'discriminator2' |
|
287 |
intr2.discriminator_hash = 'discriminator2_hash' |
|
288 |
inst.add(intr) |
|
289 |
inst.add(intr2) |
|
290 |
inst.relate(('category', 'discriminator'), |
|
291 |
('category2', 'discriminator2')) |
|
292 |
del inst._categories['category'] |
|
293 |
self.assertRaises(KeyError, inst.related, intr) |
|
294 |
|
57a9d6
|
295 |
class TestIntrospectable(unittest.TestCase): |
CM |
296 |
def _getTargetClass(slf): |
|
297 |
from pyramid.registry import Introspectable |
|
298 |
return Introspectable |
|
299 |
|
|
300 |
def _makeOne(self, *arg, **kw): |
|
301 |
return self._getTargetClass()(*arg, **kw) |
|
302 |
|
|
303 |
def _makeOnePopulated(self): |
|
304 |
return self._makeOne('category', 'discrim', 'title', 'type') |
|
305 |
|
|
306 |
def test_conformance(self): |
|
307 |
from zope.interface.verify import verifyClass |
|
308 |
from zope.interface.verify import verifyObject |
|
309 |
from pyramid.interfaces import IIntrospectable |
|
310 |
verifyClass(IIntrospectable, self._getTargetClass()) |
|
311 |
verifyObject(IIntrospectable, self._makeOnePopulated()) |
|
312 |
|
|
313 |
def test_relate(self): |
|
314 |
inst = self._makeOnePopulated() |
|
315 |
inst.relate('a', 'b') |
|
316 |
self.assertEqual(inst._relations, [(True, 'a', 'b')]) |
|
317 |
|
|
318 |
def test_unrelate(self): |
|
319 |
inst = self._makeOnePopulated() |
|
320 |
inst.unrelate('a', 'b') |
|
321 |
self.assertEqual(inst._relations, [(False, 'a', 'b')]) |
|
322 |
|
|
323 |
def test_discriminator_hash(self): |
|
324 |
inst = self._makeOnePopulated() |
|
325 |
self.assertEqual(inst.discriminator_hash, hash(inst.discriminator)) |
|
326 |
|
|
327 |
def test___hash__(self): |
|
328 |
inst = self._makeOnePopulated() |
|
329 |
self.assertEqual(hash(inst), |
|
330 |
hash((inst.category_name,) + (inst.discriminator,))) |
|
331 |
|
|
332 |
def test___repr__(self): |
|
333 |
inst = self._makeOnePopulated() |
|
334 |
self.assertEqual( |
|
335 |
repr(inst), |
|
336 |
"<Introspectable category 'category', discriminator 'discrim'>") |
|
337 |
|
|
338 |
def test___nonzero__(self): |
|
339 |
inst = self._makeOnePopulated() |
|
340 |
self.assertEqual(inst.__nonzero__(), True) |
|
341 |
|
|
342 |
def test___bool__(self): |
|
343 |
inst = self._makeOnePopulated() |
|
344 |
self.assertEqual(inst.__bool__(), True) |
|
345 |
|
|
346 |
def test_register(self): |
|
347 |
introspector = DummyIntrospector() |
|
348 |
action_info = object() |
|
349 |
inst = self._makeOnePopulated() |
|
350 |
inst._relations.append((True, 'category1', 'discrim1')) |
|
351 |
inst._relations.append((False, 'category2', 'discrim2')) |
|
352 |
inst.register(introspector, action_info) |
|
353 |
self.assertEqual(inst.action_info, action_info) |
|
354 |
self.assertEqual(introspector.intrs, [inst]) |
|
355 |
self.assertEqual(introspector.relations, |
|
356 |
[(('category', 'discrim'), ('category1', 'discrim1'))]) |
|
357 |
self.assertEqual(introspector.unrelations, |
|
358 |
[(('category', 'discrim'), ('category2', 'discrim2'))]) |
|
359 |
|
|
360 |
class DummyIntrospector(object): |
|
361 |
def __init__(self): |
|
362 |
self.intrs = [] |
|
363 |
self.relations = [] |
|
364 |
self.unrelations = [] |
|
365 |
|
|
366 |
def add(self, intr): |
|
367 |
self.intrs.append(intr) |
|
368 |
|
|
369 |
def relate(self, *pairs): |
|
370 |
self.relations.append(pairs) |
|
371 |
|
|
372 |
def unrelate(self, *pairs): |
|
373 |
self.unrelations.append(pairs) |
57a0d7
|
374 |
|
58fdd1
|
375 |
class DummyModule: |
CM |
376 |
__path__ = "foo" |
|
377 |
__name__ = "dummy" |
|
378 |
__file__ = '' |
|
379 |
|
57a0d7
|
380 |
class DummyIntrospectable(object): |
CM |
381 |
category_name = 'category' |
|
382 |
discriminator = 'discriminator' |
|
383 |
title = 'title' |
|
384 |
type_name = 'type' |
|
385 |
order = None |
|
386 |
action_info = None |
|
387 |
discriminator_hash = 'discriminator_hash' |
|
388 |
|
|
389 |
def __hash__(self): |
|
390 |
return hash((self.category_name,) + (self.discriminator,)) |
|
391 |
|
|
392 |
|
f850ec
|
393 |
from zope.interface import Interface |
3b7334
|
394 |
from zope.interface import implementer |
f850ec
|
395 |
class IDummyEvent(Interface): |
CM |
396 |
pass |
58fdd1
|
397 |
|
3b7334
|
398 |
@implementer(IDummyEvent) |
f850ec
|
399 |
class DummyEvent(object): |
3b7334
|
400 |
pass |
CM |
401 |
|