Michael Merickel
2018-10-15 0c29cf2df41600d3906d521c72991c7686018b71
commit | author | age
b60bdb 1 from pyramid import testing
a1a9fb 2 import unittest
CM 3
0c29cf 4
a1a9fb 5 class TestThreadLocalManager(unittest.TestCase):
CM 6     def setUp(self):
cbfafb 7         testing.setUp()
a1a9fb 8
CM 9     def tearDown(self):
cbfafb 10         testing.tearDown()
a1a9fb 11
CM 12     def _getTargetClass(self):
b60bdb 13         from pyramid.threadlocal import ThreadLocalManager
0c29cf 14
a1a9fb 15         return ThreadLocalManager
CM 16
17     def _makeOne(self, default=lambda *x: 1):
18         return self._getTargetClass()(default)
19
20     def test_init(self):
21         local = self._makeOne()
22         self.assertEqual(local.stack, [])
23         self.assertEqual(local.get(), 1)
24
25     def test_default(self):
17b8bc 26         def thedefault():
CM 27             return '123'
0c29cf 28
17b8bc 29         local = self._makeOne(thedefault)
a1a9fb 30         self.assertEqual(local.stack, [])
17b8bc 31         self.assertEqual(local.get(), '123')
a1a9fb 32
CM 33     def test_push_and_pop(self):
34         local = self._makeOne()
35         local.push(True)
36         self.assertEqual(local.get(), True)
37         self.assertEqual(local.pop(), True)
38         self.assertEqual(local.pop(), None)
39         self.assertEqual(local.get(), 1)
40
41     def test_set_get_and_clear(self):
42         local = self._makeOne()
43         local.set(None)
44         self.assertEqual(local.stack, [None])
45         self.assertEqual(local.get(), None)
46         local.clear()
47         self.assertEqual(local.get(), 1)
48         local.clear()
49         self.assertEqual(local.get(), 1)
50
947b8b 51
CM 52 class TestGetCurrentRequest(unittest.TestCase):
53     def _callFUT(self):
b60bdb 54         from pyramid.threadlocal import get_current_request
0c29cf 55
947b8b 56         return get_current_request()
CM 57
58     def test_it_None(self):
59         request = self._callFUT()
60         self.assertEqual(request, None)
61
62     def test_it(self):
b60bdb 63         from pyramid.threadlocal import manager
0c29cf 64
947b8b 65         request = object()
CM 66         try:
0c29cf 67             manager.push({'request': request})
947b8b 68             self.assertEqual(self._callFUT(), request)
CM 69         finally:
70             manager.pop()
71         self.assertEqual(self._callFUT(), None)
0c29cf 72
947b8b 73
CM 74 class GetCurrentRegistryTests(unittest.TestCase):
75     def setUp(self):
cbfafb 76         testing.setUp()
947b8b 77
CM 78     def tearDown(self):
cbfafb 79         testing.tearDown()
0c29cf 80
947b8b 81     def _callFUT(self):
b60bdb 82         from pyramid.threadlocal import get_current_registry
0c29cf 83
947b8b 84         return get_current_registry()
CM 85
cbfafb 86     def test_it(self):
b60bdb 87         from pyramid.threadlocal import manager
0c29cf 88
947b8b 89         try:
0c29cf 90             manager.push({'registry': 123})
947b8b 91             self.assertEqual(self._callFUT(), 123)
CM 92         finally:
93             manager.pop()
cbfafb 94
0c29cf 95
cbfafb 96 class GetCurrentRegistryWithoutTestingRegistry(unittest.TestCase):
CM 97     def _callFUT(self):
b60bdb 98         from pyramid.threadlocal import get_current_registry
0c29cf 99
cbfafb 100         return get_current_registry()
CM 101
102     def test_it(self):
b60bdb 103         from pyramid.registry import global_registry
0c29cf 104
59c5df 105         self.assertEqual(self._callFUT(), global_registry)