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