test_threadlocal.py 2.68 KB
Newer Older
1 2 3
from pyramid import testing
import unittest

4

5 6 7 8 9 10 11 12 13
class TestThreadLocalManager(unittest.TestCase):
    def setUp(self):
        testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _getTargetClass(self):
        from pyramid.threadlocal import ThreadLocalManager
14

15 16 17 18 19 20 21 22 23 24 25 26 27
        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'
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
        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
55

56 57 58 59 60 61 62 63
        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
64

65 66
        request = object()
        try:
67
            manager.push({'request': request})
68 69 70 71 72
            self.assertEqual(self._callFUT(), request)
        finally:
            manager.pop()
        self.assertEqual(self._callFUT(), None)

73

74 75 76 77 78 79
class GetCurrentRegistryTests(unittest.TestCase):
    def setUp(self):
        testing.setUp()

    def tearDown(self):
        testing.tearDown()
80

81 82
    def _callFUT(self):
        from pyramid.threadlocal import get_current_registry
83

84 85 86 87
        return get_current_registry()

    def test_it(self):
        from pyramid.threadlocal import manager
88

89
        try:
90
            manager.push({'registry': 123})
91 92 93 94
            self.assertEqual(self._callFUT(), 123)
        finally:
            manager.pop()

95

96 97 98
class GetCurrentRegistryWithoutTestingRegistry(unittest.TestCase):
    def _callFUT(self):
        from pyramid.threadlocal import get_current_registry
99

100 101 102 103
        return get_current_registry()

    def test_it(self):
        from pyramid.registry import global_registry
104

105
        self.assertEqual(self._callFUT(), global_registry)