forked from Pylons/pyramid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_threadlocal.py
106 lines (76 loc) · 2.68 KB
/
test_threadlocal.py
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
import unittest
from pyramid import testing
class TestThreadLocalManager(unittest.TestCase):
def setUp(self):
testing.setUp()
def tearDown(self):
testing.tearDown()
def _getTargetClass(self):
from pyramid.threadlocal import ThreadLocalManager
return ThreadLocalManager
def _makeOne(self, default=lambda *x: 1):
return self._getTargetClass()(default)
def test_init(self):
local = self._makeOne()
self.assertEqual(local.stack, [])
self.assertEqual(local.get(), 1)
def test_default(self):
def thedefault():
return '123'
local = self._makeOne(thedefault)
self.assertEqual(local.stack, [])
self.assertEqual(local.get(), '123')
def test_push_and_pop(self):
local = self._makeOne()
local.push(True)
self.assertEqual(local.get(), True)
self.assertEqual(local.pop(), True)
self.assertEqual(local.pop(), None)
self.assertEqual(local.get(), 1)
def test_set_get_and_clear(self):
local = self._makeOne()
local.set(None)
self.assertEqual(local.stack, [None])
self.assertEqual(local.get(), None)
local.clear()
self.assertEqual(local.get(), 1)
local.clear()
self.assertEqual(local.get(), 1)
class TestGetCurrentRequest(unittest.TestCase):
def _callFUT(self):
from pyramid.threadlocal import get_current_request
return get_current_request()
def test_it_None(self):
request = self._callFUT()
self.assertEqual(request, None)
def test_it(self):
from pyramid.threadlocal import manager
request = object()
try:
manager.push({'request': request})
self.assertEqual(self._callFUT(), request)
finally:
manager.pop()
self.assertEqual(self._callFUT(), None)
class GetCurrentRegistryTests(unittest.TestCase):
def setUp(self):
testing.setUp()
def tearDown(self):
testing.tearDown()
def _callFUT(self):
from pyramid.threadlocal import get_current_registry
return get_current_registry()
def test_it(self):
from pyramid.threadlocal import manager
try:
manager.push({'registry': 123})
self.assertEqual(self._callFUT(), 123)
finally:
manager.pop()
class GetCurrentRegistryWithoutTestingRegistry(unittest.TestCase):
def _callFUT(self):
from pyramid.threadlocal import get_current_registry
return get_current_registry()
def test_it(self):
from pyramid.registry import global_registry
self.assertEqual(self._callFUT(), global_registry)