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