File: test_registry.py

package info (click to toggle)
python-pyramid 1.10.2+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,052 kB
  • sloc: python: 42,982; makefile: 41
file content (452 lines) | stat: -rw-r--r-- 14,094 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import unittest
from zope.interface import Interface
from zope.interface import implementer


class TestRegistry(unittest.TestCase):
    def _getTargetClass(self):
        from pyramid.registry import Registry

        return Registry

    def _makeOne(self, *args, **kw):
        return self._getTargetClass()(*args, **kw)

    def test___nonzero__(self):
        registry = self._makeOne()
        self.assertEqual(registry.__nonzero__(), True)

    def test__lock(self):
        registry = self._makeOne()
        self.assertTrue(registry._lock)

    def test_clear_view_cache_lookup(self):
        registry = self._makeOne()
        registry._view_lookup_cache[1] = 2
        registry._clear_view_lookup_cache()
        self.assertEqual(registry._view_lookup_cache, {})

    def test_package_name(self):
        package_name = 'testing'
        registry = self._makeOne(package_name)
        self.assertEqual(registry.package_name, package_name)

    def test_default_package_name(self):
        registry = self._makeOne()
        self.assertEqual(registry.package_name, 'tests')

    def test_registerHandler_and_notify(self):
        registry = self._makeOne()
        self.assertEqual(registry.has_listeners, False)
        L = []

        def f(event):
            L.append(event)

        registry.registerHandler(f, [IDummyEvent])
        self.assertEqual(registry.has_listeners, True)
        event = DummyEvent()
        registry.notify(event)
        self.assertEqual(L, [event])

    def test_registerSubscriptionAdapter(self):
        registry = self._makeOne()
        self.assertEqual(registry.has_listeners, False)
        from zope.interface import Interface

        registry.registerSubscriptionAdapter(
            DummyEvent, [IDummyEvent], Interface
        )
        self.assertEqual(registry.has_listeners, True)

    def test__get_settings(self):
        registry = self._makeOne()
        registry._settings = 'foo'
        self.assertEqual(registry.settings, 'foo')

    def test__set_settings(self):
        registry = self._makeOne()
        registry.settings = 'foo'
        self.assertEqual(registry._settings, 'foo')

    def test_init_forwards_args(self):
        from zope.interface import Interface
        from zope.interface.registry import Components

        dummy = object()
        c = Components()
        c.registerUtility(dummy, Interface)
        registry = self._makeOne('foo', (c,))
        self.assertEqual(registry.__name__, 'foo')
        self.assertEqual(registry.getUtility(Interface), dummy)

    def test_init_forwards_kw(self):
        from zope.interface import Interface
        from zope.interface.registry import Components

        dummy = object()
        c = Components()
        c.registerUtility(dummy, Interface)
        registry = self._makeOne(bases=(c,))
        self.assertEqual(registry.getUtility(Interface), dummy)


class TestIntrospector(unittest.TestCase):
    def _getTargetClass(slf):
        from pyramid.registry import Introspector

        return Introspector

    def _makeOne(self):
        return self._getTargetClass()()

    def test_conformance(self):
        from zope.interface.verify import verifyClass
        from zope.interface.verify import verifyObject
        from pyramid.interfaces import IIntrospector

        verifyClass(IIntrospector, self._getTargetClass())
        verifyObject(IIntrospector, self._makeOne())

    def test_add(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        inst.add(intr)
        self.assertEqual(intr.order, 0)
        category = {'discriminator': intr, 'discriminator_hash': intr}
        self.assertEqual(inst._categories, {'category': category})

    def test_get_success(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        inst.add(intr)
        self.assertEqual(inst.get('category', 'discriminator'), intr)

    def test_get_success_byhash(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        inst.add(intr)
        self.assertEqual(inst.get('category', 'discriminator_hash'), intr)

    def test_get_fail(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        inst.add(intr)
        self.assertEqual(inst.get('category', 'wontexist', 'foo'), 'foo')

    def test_get_category(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr2)
        inst.add(intr)
        expected = [
            {'introspectable': intr2, 'related': []},
            {'introspectable': intr, 'related': []},
        ]
        self.assertEqual(inst.get_category('category'), expected)

    def test_get_category_returns_default_on_miss(self):
        inst = self._makeOne()
        self.assertEqual(inst.get_category('category', '123'), '123')

    def test_get_category_with_sortkey(self):
        import operator

        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr.foo = 2
        intr2 = DummyIntrospectable()
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        intr2.foo = 1
        inst.add(intr)
        inst.add(intr2)
        expected = [
            {'introspectable': intr2, 'related': []},
            {'introspectable': intr, 'related': []},
        ]
        self.assertEqual(
            inst.get_category('category', sort_key=operator.attrgetter('foo')),
            expected,
        )

    def test_categorized(self):
        import operator

        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr.foo = 2
        intr2 = DummyIntrospectable()
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        intr2.foo = 1
        inst.add(intr)
        inst.add(intr2)
        expected = [
            (
                'category',
                [
                    {'introspectable': intr2, 'related': []},
                    {'introspectable': intr, 'related': []},
                ],
            )
        ]
        self.assertEqual(
            inst.categorized(sort_key=operator.attrgetter('foo')), expected
        )

    def test_categories(self):
        inst = self._makeOne()
        inst._categories['a'] = 1
        inst._categories['b'] = 2
        self.assertEqual(list(inst.categories()), ['a', 'b'])

    def test_remove(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.category_name = 'category2'
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr)
        inst.add(intr2)
        inst.relate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        inst.remove('category', 'discriminator')
        self.assertEqual(
            inst._categories,
            {
                'category': {},
                'category2': {
                    'discriminator2': intr2,
                    'discriminator2_hash': intr2,
                },
            },
        )
        self.assertEqual(inst._refs.get(intr), None)
        self.assertEqual(inst._refs[intr2], [])

    def test_remove_fail(self):
        inst = self._makeOne()
        self.assertEqual(inst.remove('a', 'b'), None)

    def test_relate(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.category_name = 'category2'
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr)
        inst.add(intr2)
        inst.relate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        self.assertEqual(
            inst._categories,
            {
                'category': {
                    'discriminator': intr,
                    'discriminator_hash': intr,
                },
                'category2': {
                    'discriminator2': intr2,
                    'discriminator2_hash': intr2,
                },
            },
        )
        self.assertEqual(inst._refs[intr], [intr2])
        self.assertEqual(inst._refs[intr2], [intr])

    def test_relate_fail(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        inst.add(intr)
        self.assertRaises(
            KeyError,
            inst.relate,
            ('category', 'discriminator'),
            ('category2', 'discriminator2'),
        )

    def test_unrelate(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.category_name = 'category2'
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr)
        inst.add(intr2)
        inst.relate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        inst.unrelate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        self.assertEqual(
            inst._categories,
            {
                'category': {
                    'discriminator': intr,
                    'discriminator_hash': intr,
                },
                'category2': {
                    'discriminator2': intr2,
                    'discriminator2_hash': intr2,
                },
            },
        )
        self.assertEqual(inst._refs[intr], [])
        self.assertEqual(inst._refs[intr2], [])

    def test_related(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.category_name = 'category2'
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr)
        inst.add(intr2)
        inst.relate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        self.assertEqual(inst.related(intr), [intr2])

    def test_related_fail(self):
        inst = self._makeOne()
        intr = DummyIntrospectable()
        intr2 = DummyIntrospectable()
        intr2.category_name = 'category2'
        intr2.discriminator = 'discriminator2'
        intr2.discriminator_hash = 'discriminator2_hash'
        inst.add(intr)
        inst.add(intr2)
        inst.relate(
            ('category', 'discriminator'), ('category2', 'discriminator2')
        )
        del inst._categories['category']
        self.assertRaises(KeyError, inst.related, intr)


class TestIntrospectable(unittest.TestCase):
    def _getTargetClass(slf):
        from pyramid.registry import Introspectable

        return Introspectable

    def _makeOne(self, *arg, **kw):
        return self._getTargetClass()(*arg, **kw)

    def _makeOnePopulated(self):
        return self._makeOne('category', 'discrim', 'title', 'type')

    def test_conformance(self):
        from zope.interface.verify import verifyClass
        from zope.interface.verify import verifyObject
        from pyramid.interfaces import IIntrospectable

        verifyClass(IIntrospectable, self._getTargetClass())
        verifyObject(IIntrospectable, self._makeOnePopulated())

    def test_relate(self):
        inst = self._makeOnePopulated()
        inst.relate('a', 'b')
        self.assertEqual(inst._relations, [(True, 'a', 'b')])

    def test_unrelate(self):
        inst = self._makeOnePopulated()
        inst.unrelate('a', 'b')
        self.assertEqual(inst._relations, [(False, 'a', 'b')])

    def test_discriminator_hash(self):
        inst = self._makeOnePopulated()
        self.assertEqual(inst.discriminator_hash, hash(inst.discriminator))

    def test___hash__(self):
        inst = self._makeOnePopulated()
        self.assertEqual(
            hash(inst), hash((inst.category_name,) + (inst.discriminator,))
        )

    def test___repr__(self):
        inst = self._makeOnePopulated()
        self.assertEqual(
            repr(inst),
            "<Introspectable category 'category', discriminator 'discrim'>",
        )

    def test___nonzero__(self):
        inst = self._makeOnePopulated()
        self.assertEqual(inst.__nonzero__(), True)

    def test___bool__(self):
        inst = self._makeOnePopulated()
        self.assertEqual(inst.__bool__(), True)

    def test_register(self):
        introspector = DummyIntrospector()
        action_info = object()
        inst = self._makeOnePopulated()
        inst._relations.append((True, 'category1', 'discrim1'))
        inst._relations.append((False, 'category2', 'discrim2'))
        inst.register(introspector, action_info)
        self.assertEqual(inst.action_info, action_info)
        self.assertEqual(introspector.intrs, [inst])
        self.assertEqual(
            introspector.relations,
            [(('category', 'discrim'), ('category1', 'discrim1'))],
        )
        self.assertEqual(
            introspector.unrelations,
            [(('category', 'discrim'), ('category2', 'discrim2'))],
        )


class DummyIntrospector(object):
    def __init__(self):
        self.intrs = []
        self.relations = []
        self.unrelations = []

    def add(self, intr):
        self.intrs.append(intr)

    def relate(self, *pairs):
        self.relations.append(pairs)

    def unrelate(self, *pairs):
        self.unrelations.append(pairs)


class DummyModule:
    __path__ = "foo"
    __name__ = "dummy"
    __file__ = ''


class DummyIntrospectable(object):
    category_name = 'category'
    discriminator = 'discriminator'
    title = 'title'
    type_name = 'type'
    order = None
    action_info = None
    discriminator_hash = 'discriminator_hash'

    def __hash__(self):
        return hash((self.category_name,) + (self.discriminator,))


class IDummyEvent(Interface):
    pass


@implementer(IDummyEvent)
class DummyEvent(object):
    pass