Michael Merickel
2018-10-15 bda1306749c62ef4f11cfe567ed7d56c8ad94240
commit | author | age
8517d4 1 import unittest
CM 2
dd3cc8 3 from . import dummy_tween_factory
MM 4 from . import dummy_tween_factory2
49f082 5
79ef3d 6 from pyramid.exceptions import ConfigurationConflictError
CM 7
0c29cf 8
49f082 9 class TestTweensConfiguratorMixin(unittest.TestCase):
CM 10     def _makeOne(self, *arg, **kw):
11         from pyramid.config import Configurator
0c29cf 12
49f082 13         config = Configurator(*arg, **kw)
CM 14         return config
15
16     def test_add_tweens_names_distinct(self):
17         from pyramid.interfaces import ITweens
18         from pyramid.tweens import excview_tween_factory
0c29cf 19
10ddb6 20         def factory1(handler, registry):  # pragma: no cover
0c29cf 21             return handler
MM 22
10ddb6 23         def factory2(handler, registry):  # pragma: no cover
0c29cf 24             return handler
MM 25
49f082 26         config = self._makeOne()
0c29cf 27         config.add_tween('tests.test_config.dummy_tween_factory')
MM 28         config.add_tween('tests.test_config.dummy_tween_factory2')
49f082 29         config.commit()
CM 30         tweens = config.registry.queryUtility(ITweens)
31         implicit = tweens.implicit()
32         self.assertEqual(
33             implicit,
34             [
0c29cf 35                 (
MM 36                     'tests.test_config.dummy_tween_factory2',
37                     dummy_tween_factory2,
38                 ),
39                 ('tests.test_config.dummy_tween_factory', dummy_tween_factory),
40                 (
41                     'pyramid.tweens.excview_tween_factory',
42                     excview_tween_factory,
43                 ),
44             ],
45         )
49f082 46
CM 47     def test_add_tweens_names_with_underover(self):
48         from pyramid.interfaces import ITweens
49         from pyramid.tweens import excview_tween_factory
50         from pyramid.tweens import MAIN
0c29cf 51
49f082 52         config = self._makeOne()
0c29cf 53         config.add_tween('tests.test_config.dummy_tween_factory', over=MAIN)
49f082 54         config.add_tween(
dd3cc8 55             'tests.test_config.dummy_tween_factory2',
49f082 56             over=MAIN,
0c29cf 57             under='tests.test_config.dummy_tween_factory',
MM 58         )
49f082 59         config.commit()
CM 60         tweens = config.registry.queryUtility(ITweens)
61         implicit = tweens.implicit()
62         self.assertEqual(
63             implicit,
64             [
0c29cf 65                 (
MM 66                     'pyramid.tweens.excview_tween_factory',
67                     excview_tween_factory,
68                 ),
69                 ('tests.test_config.dummy_tween_factory', dummy_tween_factory),
70                 (
71                     'tests.test_config.dummy_tween_factory2',
72                     dummy_tween_factory2,
73                 ),
74             ],
75         )
49f082 76
CM 77     def test_add_tweens_names_with_under_nonstringoriter(self):
78         from pyramid.exceptions import ConfigurationError
0c29cf 79
49f082 80         config = self._makeOne()
CM 81         self.assertRaises(
0c29cf 82             ConfigurationError,
MM 83             config.add_tween,
dd3cc8 84             'tests.test_config.dummy_tween_factory',
0c29cf 85             under=False,
MM 86         )
49f082 87
CM 88     def test_add_tweens_names_with_over_nonstringoriter(self):
89         from pyramid.exceptions import ConfigurationError
0c29cf 90
49f082 91         config = self._makeOne()
CM 92         self.assertRaises(
0c29cf 93             ConfigurationError,
MM 94             config.add_tween,
dd3cc8 95             'tests.test_config.dummy_tween_factory',
0c29cf 96             over=False,
MM 97         )
49f082 98
CM 99     def test_add_tween_dottedname(self):
100         from pyramid.interfaces import ITweens
101         from pyramid.tweens import excview_tween_factory
0c29cf 102
49f082 103         config = self._makeOne()
dd3cc8 104         config.add_tween('tests.test_config.dummy_tween_factory')
49f082 105         config.commit()
CM 106         tweens = config.registry.queryUtility(ITweens)
107         self.assertEqual(
108             tweens.implicit(),
109             [
0c29cf 110                 ('tests.test_config.dummy_tween_factory', dummy_tween_factory),
MM 111                 (
112                     'pyramid.tweens.excview_tween_factory',
113                     excview_tween_factory,
114                 ),
115             ],
116         )
49f082 117
CM 118     def test_add_tween_instance(self):
119         from pyramid.exceptions import ConfigurationError
0c29cf 120
MM 121         class ATween(object):
122             pass
123
49f082 124         atween = ATween()
CM 125         config = self._makeOne()
126         self.assertRaises(ConfigurationError, config.add_tween, atween)
127
128     def test_add_tween_unsuitable(self):
129         from pyramid.exceptions import ConfigurationError
dd3cc8 130         import tests.test_config
0c29cf 131
49f082 132         config = self._makeOne()
0c29cf 133         self.assertRaises(
MM 134             ConfigurationError, config.add_tween, tests.test_config
135         )
49f082 136
CM 137     def test_add_tween_name_ingress(self):
138         from pyramid.exceptions import ConfigurationError
139         from pyramid.tweens import INGRESS
0c29cf 140
49f082 141         config = self._makeOne()
CM 142         self.assertRaises(ConfigurationError, config.add_tween, INGRESS)
143
144     def test_add_tween_name_main(self):
145         from pyramid.exceptions import ConfigurationError
146         from pyramid.tweens import MAIN
0c29cf 147
49f082 148         config = self._makeOne()
CM 149         self.assertRaises(ConfigurationError, config.add_tween, MAIN)
150
151     def test_add_tweens_conflict(self):
152         config = self._makeOne()
dd3cc8 153         config.add_tween('tests.test_config.dummy_tween_factory')
MM 154         config.add_tween('tests.test_config.dummy_tween_factory')
49f082 155         self.assertRaises(ConfigurationConflictError, config.commit)
CM 156
157     def test_add_tween_over_ingress(self):
158         from pyramid.exceptions import ConfigurationError
159         from pyramid.tweens import INGRESS
0c29cf 160
49f082 161         config = self._makeOne()
CM 162         self.assertRaises(
163             ConfigurationError,
164             config.add_tween,
dd3cc8 165             'tests.test_config.dummy_tween_factory',
0c29cf 166             over=INGRESS,
MM 167         )
49f082 168
CM 169     def test_add_tween_over_ingress_iterable(self):
170         from pyramid.exceptions import ConfigurationError
171         from pyramid.tweens import INGRESS
0c29cf 172
49f082 173         config = self._makeOne()
CM 174         self.assertRaises(
175             ConfigurationError,
176             config.add_tween,
dd3cc8 177             'tests.test_config.dummy_tween_factory',
0c29cf 178             over=('a', INGRESS),
MM 179         )
49f082 180
CM 181     def test_add_tween_under_main(self):
182         from pyramid.exceptions import ConfigurationError
183         from pyramid.tweens import MAIN
0c29cf 184
49f082 185         config = self._makeOne()
CM 186         self.assertRaises(
187             ConfigurationError,
188             config.add_tween,
dd3cc8 189             'tests.test_config.dummy_tween_factory',
0c29cf 190             under=MAIN,
MM 191         )
49f082 192
CM 193     def test_add_tween_under_main_iterable(self):
194         from pyramid.exceptions import ConfigurationError
195         from pyramid.tweens import MAIN
0c29cf 196
49f082 197         config = self._makeOne()
CM 198         self.assertRaises(
199             ConfigurationError,
200             config.add_tween,
dd3cc8 201             'tests.test_config.dummy_tween_factory',
0c29cf 202             under=('a', MAIN),
MM 203         )
204
49f082 205
8517d4 206 class TestTweens(unittest.TestCase):
CM 207     def _makeOne(self):
b397ac 208         from pyramid.config.tweens import Tweens
0c29cf 209
8517d4 210         return Tweens()
CM 211
212     def test_add_explicit(self):
213         tweens = self._makeOne()
214         tweens.add_explicit('name', 'factory')
0c29cf 215         self.assertEqual(tweens.explicit, [('name', 'factory')])
8517d4 216         tweens.add_explicit('name2', 'factory2')
0c29cf 217         self.assertEqual(
MM 218             tweens.explicit, [('name', 'factory'), ('name2', 'factory2')]
219         )
8517d4 220
4f070b 221     def test_add_implicit(self):
8517d4 222         tweens = self._makeOne()
CM 223         tweens.add_implicit('name', 'factory')
224         tweens.add_implicit('name2', 'factory2')
0c29cf 225         self.assertEqual(
MM 226             tweens.sorter.sorted(),
227             [('name2', 'factory2'), ('name', 'factory')],
228         )
8517d4 229
CM 230     def test___call___explicit(self):
231         tweens = self._makeOne()
0c29cf 232
8517d4 233         def factory1(handler, registry):
CM 234             return handler
0c29cf 235
8517d4 236         def factory2(handler, registry):
CM 237             return '123'
0c29cf 238
8517d4 239         tweens.explicit = [('name', factory1), ('name', factory2)]
CM 240         self.assertEqual(tweens(None, None), '123')
241
242     def test___call___implicit(self):
243         tweens = self._makeOne()
0c29cf 244
8517d4 245         def factory1(handler, registry):
CM 246             return handler
0c29cf 247
8517d4 248         def factory2(handler, registry):
CM 249             return '123'
0c29cf 250
fc3f23 251         tweens.add_implicit('name2', factory2)
CM 252         tweens.add_implicit('name1', factory1)
8517d4 253         self.assertEqual(tweens(None, None), '123')
CM 254
255     def test_implicit_ordering_1(self):
256         tweens = self._makeOne()
257         tweens.add_implicit('name1', 'factory1')
258         tweens.add_implicit('name2', 'factory2')
0c29cf 259         self.assertEqual(
MM 260             tweens.implicit(), [('name2', 'factory2'), ('name1', 'factory1')]
261         )
8517d4 262
CM 263     def test_implicit_ordering_2(self):
264         from pyramid.tweens import MAIN
0c29cf 265
8517d4 266         tweens = self._makeOne()
CM 267         tweens.add_implicit('name1', 'factory1')
4cce79 268         tweens.add_implicit('name2', 'factory2', over=MAIN)
0c29cf 269         self.assertEqual(
MM 270             tweens.implicit(), [('name1', 'factory1'), ('name2', 'factory2')]
271         )
8517d4 272
CM 273     def test_implicit_ordering_3(self):
274         from pyramid.tweens import MAIN
0c29cf 275
8517d4 276         tweens = self._makeOne()
CM 277         add = tweens.add_implicit
4cce79 278         add('auth', 'auth_factory', under='browserid')
0c29cf 279         add('dbt', 'dbt_factory')
4cce79 280         add('retry', 'retry_factory', over='txnmgr', under='exceptionview')
8517d4 281         add('browserid', 'browserid_factory')
4cce79 282         add('txnmgr', 'txnmgr_factory', under='exceptionview')
CM 283         add('exceptionview', 'excview_factory', over=MAIN)
0c29cf 284         self.assertEqual(
MM 285             tweens.implicit(),
286             [
287                 ('browserid', 'browserid_factory'),
288                 ('auth', 'auth_factory'),
289                 ('dbt', 'dbt_factory'),
290                 ('exceptionview', 'excview_factory'),
291                 ('retry', 'retry_factory'),
292                 ('txnmgr', 'txnmgr_factory'),
293             ],
294         )
8517d4 295
CM 296     def test_implicit_ordering_4(self):
297         from pyramid.tweens import MAIN
0c29cf 298
8517d4 299         tweens = self._makeOne()
CM 300         add = tweens.add_implicit
4cce79 301         add('exceptionview', 'excview_factory', over=MAIN)
CM 302         add('auth', 'auth_factory', under='browserid')
303         add('retry', 'retry_factory', over='txnmgr', under='exceptionview')
8517d4 304         add('browserid', 'browserid_factory')
4cce79 305         add('txnmgr', 'txnmgr_factory', under='exceptionview')
0c29cf 306         add('dbt', 'dbt_factory')
MM 307         self.assertEqual(
308             tweens.implicit(),
309             [
310                 ('dbt', 'dbt_factory'),
311                 ('browserid', 'browserid_factory'),
312                 ('auth', 'auth_factory'),
313                 ('exceptionview', 'excview_factory'),
314                 ('retry', 'retry_factory'),
315                 ('txnmgr', 'txnmgr_factory'),
316             ],
317         )
8517d4 318
04ca37 319     def test_implicit_ordering_5(self):
CM 320         from pyramid.tweens import MAIN, INGRESS
0c29cf 321
04ca37 322         tweens = self._makeOne()
CM 323         add = tweens.add_implicit
324         add('exceptionview', 'excview_factory', over=MAIN)
325         add('auth', 'auth_factory', under=INGRESS)
326         add('retry', 'retry_factory', over='txnmgr', under='exceptionview')
327         add('browserid', 'browserid_factory', under=INGRESS)
328         add('txnmgr', 'txnmgr_factory', under='exceptionview', over=MAIN)
0c29cf 329         add('dbt', 'dbt_factory')
MM 330         self.assertEqual(
331             tweens.implicit(),
332             [
333                 ('dbt', 'dbt_factory'),
334                 ('browserid', 'browserid_factory'),
335                 ('auth', 'auth_factory'),
336                 ('exceptionview', 'excview_factory'),
337                 ('retry', 'retry_factory'),
338                 ('txnmgr', 'txnmgr_factory'),
339             ],
340         )
04ca37 341
c45737 342     def test_implicit_ordering_missing_over_partial(self):
MM 343         from pyramid.exceptions import ConfigurationError
0c29cf 344
c45737 345         tweens = self._makeOne()
MM 346         add = tweens.add_implicit
347         add('dbt', 'dbt_factory')
348         add('auth', 'auth_factory', under='browserid')
349         add('retry', 'retry_factory', over='txnmgr', under='exceptionview')
350         add('browserid', 'browserid_factory')
351         self.assertRaises(ConfigurationError, tweens.implicit)
352
353     def test_implicit_ordering_missing_under_partial(self):
354         from pyramid.exceptions import ConfigurationError
0c29cf 355
c45737 356         tweens = self._makeOne()
MM 357         add = tweens.add_implicit
358         add('dbt', 'dbt_factory')
359         add('auth', 'auth_factory', under='txnmgr')
360         add('retry', 'retry_factory', over='dbt', under='exceptionview')
361         add('browserid', 'browserid_factory')
362         self.assertRaises(ConfigurationError, tweens.implicit)
363
364     def test_implicit_ordering_missing_over_and_under_partials(self):
365         from pyramid.exceptions import ConfigurationError
0c29cf 366
c45737 367         tweens = self._makeOne()
MM 368         add = tweens.add_implicit
369         add('dbt', 'dbt_factory')
370         add('auth', 'auth_factory', under='browserid')
371         add('retry', 'retry_factory', over='foo', under='txnmgr')
372         add('browserid', 'browserid_factory')
373         self.assertRaises(ConfigurationError, tweens.implicit)
374
375     def test_implicit_ordering_missing_over_partial_with_fallback(self):
8517d4 376         from pyramid.tweens import MAIN
0c29cf 377
8517d4 378         tweens = self._makeOne()
CM 379         add = tweens.add_implicit
4cce79 380         add('exceptionview', 'excview_factory', over=MAIN)
CM 381         add('auth', 'auth_factory', under='browserid')
0c29cf 382         add(
MM 383             'retry',
384             'retry_factory',
385             over=('txnmgr', MAIN),
386             under='exceptionview',
387         )
8517d4 388         add('browserid', 'browserid_factory')
0c29cf 389         add('dbt', 'dbt_factory')
MM 390         self.assertEqual(
391             tweens.implicit(),
392             [
393                 ('dbt', 'dbt_factory'),
394                 ('browserid', 'browserid_factory'),
395                 ('auth', 'auth_factory'),
396                 ('exceptionview', 'excview_factory'),
397                 ('retry', 'retry_factory'),
398             ],
399         )
8517d4 400
c45737 401     def test_implicit_ordering_missing_under_partial_with_fallback(self):
8517d4 402         from pyramid.tweens import MAIN
0c29cf 403
8517d4 404         tweens = self._makeOne()
CM 405         add = tweens.add_implicit
4cce79 406         add('exceptionview', 'excview_factory', over=MAIN)
0c29cf 407         add('auth', 'auth_factory', under=('txnmgr', 'browserid'))
c45737 408         add('retry', 'retry_factory', under='exceptionview')
8517d4 409         add('browserid', 'browserid_factory')
c45737 410         add('dbt', 'dbt_factory')
0c29cf 411         self.assertEqual(
MM 412             tweens.implicit(),
413             [
414                 ('dbt', 'dbt_factory'),
415                 ('browserid', 'browserid_factory'),
416                 ('auth', 'auth_factory'),
417                 ('exceptionview', 'excview_factory'),
418                 ('retry', 'retry_factory'),
419             ],
420         )
8517d4 421
c45737 422     def test_implicit_ordering_with_partial_fallbacks(self):
MM 423         from pyramid.tweens import MAIN
0c29cf 424
c45737 425         tweens = self._makeOne()
MM 426         add = tweens.add_implicit
4f070b 427         add('exceptionview', 'excview_factory', over=('wontbethere', MAIN))
CM 428         add('retry', 'retry_factory', under='exceptionview')
429         add('browserid', 'browserid_factory', over=('wont2', 'exceptionview'))
0c29cf 430         self.assertEqual(
MM 431             tweens.implicit(),
432             [
433                 ('browserid', 'browserid_factory'),
434                 ('exceptionview', 'excview_factory'),
435                 ('retry', 'retry_factory'),
436             ],
437         )
c45737 438
MM 439     def test_implicit_ordering_with_multiple_matching_fallbacks(self):
440         from pyramid.tweens import MAIN
0c29cf 441
c45737 442         tweens = self._makeOne()
MM 443         add = tweens.add_implicit
4f070b 444         add('exceptionview', 'excview_factory', over=MAIN)
CM 445         add('retry', 'retry_factory', under='exceptionview')
446         add('browserid', 'browserid_factory', over=('retry', 'exceptionview'))
0c29cf 447         self.assertEqual(
MM 448             tweens.implicit(),
449             [
450                 ('browserid', 'browserid_factory'),
451                 ('exceptionview', 'excview_factory'),
452                 ('retry', 'retry_factory'),
453             ],
454         )
c45737 455
MM 456     def test_implicit_ordering_with_missing_fallbacks(self):
457         from pyramid.exceptions import ConfigurationError
458         from pyramid.tweens import MAIN
0c29cf 459
c45737 460         tweens = self._makeOne()
MM 461         add = tweens.add_implicit
4f070b 462         add('exceptionview', 'excview_factory', over=MAIN)
CM 463         add('retry', 'retry_factory', under='exceptionview')
c45737 464         add('browserid', 'browserid_factory', over=('txnmgr', 'auth'))
MM 465         self.assertRaises(ConfigurationError, tweens.implicit)
466
8517d4 467     def test_implicit_ordering_conflict_direct(self):
66fe1d 468         from pyramid.exceptions import CyclicDependencyError
0c29cf 469
8517d4 470         tweens = self._makeOne()
CM 471         add = tweens.add_implicit
472         add('browserid', 'browserid_factory')
2ab791 473         add('auth', 'auth_factory', over='browserid', under='browserid')
8517d4 474         self.assertRaises(CyclicDependencyError, tweens.implicit)
CM 475
476     def test_implicit_ordering_conflict_indirect(self):
66fe1d 477         from pyramid.exceptions import CyclicDependencyError
0c29cf 478
8517d4 479         tweens = self._makeOne()
CM 480         add = tweens.add_implicit
481         add('browserid', 'browserid_factory')
2ab791 482         add('auth', 'auth_factory', over='browserid')
CM 483         add('dbt', 'dbt_factory', under='browserid', over='auth')
8517d4 484         self.assertRaises(CyclicDependencyError, tweens.implicit)