~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/tests/test_app.py -rw-r--r-- 5.3 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 years ago
                                                                                
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import unittest
from pyramid import testing


class TestRouteName(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp()
        self.request = testing.DummyRequest()
        self.request.registry = self.config.registry

    def tearDown(self):
        testing.tearDown()

    def _get_function(self):
        from .. import route_name
        return route_name

    def test_route_name(self):
        class _MockMatchedRule(object):
            def __init__(self, name):
                self.name = name

        self.request.matched_route = _MockMatchedRule('foobar')
        self.assertEqual(self._get_function()(self.request), 'foobar')

    def test_route_name_not_exists(self):
        self.request.matched_route = None
        self.assertEqual(self._get_function()(self.request), None)


class DummyStaticURLInfo:

    def __init__(self, result):
        self.result = result

    def generate(self, path, request, **kw):
        self.args = path, request, kw
        return self.result


class TestTaggedStaticUrl(unittest.TestCase):

    def setUp(self):
        from pyramid.url import URLMethodsMixin

        class _MockRequest(URLMethodsMixin):
            application_url = 'http://example.com:5432'
            script_name = ''

            def __init__(self, environ):
                self.environ = environ
                self.registry = None

        self.config = testing.setUp()
        self.request = _MockRequest({})
        self.request.registry = self.config.registry

    def tearDown(self):
        testing.tearDown()

    def _get_function(self):
        from .. import tagged_static_path
        return tagged_static_path

    def _get_hash(self, package, path):
        import hashlib
        from pyramid.path import AssetResolver
        abspath = AssetResolver(package).resolve(path).abspath()
        with open(abspath, 'rb') as f:
            return hashlib.md5(f.read()).hexdigest()[:8]

    def test_tagged_static_path(self):
        from pyramid.interfaces import IStaticURLInfo
        info = DummyStaticURLInfo('foobar')
        self.request.registry.registerUtility(info, IStaticURLInfo)
        result = self._get_function()(
            self.request,
            'fanboi2:tests/test_app.py')
        self.assertEqual(result, 'foobar')
        self.assertEqual(
            info.args, ('fanboi2:tests/test_app.py', self.request, {
                '_app_url': '',
                '_query': {
                    'h': self._get_hash('fanboi2', 'tests/test_app.py')}}))

    def test_tagged_static_path_non_exists(self):
        from pyramid.interfaces import IStaticURLInfo
        info = DummyStaticURLInfo('foobar')
        self.request.registry.registerUtility(info, IStaticURLInfo)
        with self.assertRaises(IOError):
            self._get_function()(self.request, 'fanboi2:static/notexists')

    def test_tagged_static_path_non_package(self):
        from pyramid.interfaces import IStaticURLInfo
        info = DummyStaticURLInfo('foobar')
        self.request.registry.registerUtility(info, IStaticURLInfo)
        result = self._get_function()(self.request, 'tests/test_app.py')
        self.assertEqual(result, 'foobar')
        self.assertEqual(
            info.args, ('fanboi2:tests/test_app.py', self.request, {
                '_app_url': '',
                '_query': {
                    'h': self._get_hash('fanboi2', 'tests/test_app.py')}}))

    def test_tagged_static_path_non_package_non_exists(self):
        from pyramid.interfaces import IStaticURLInfo
        info = DummyStaticURLInfo('foobar')
        self.request.registry.registerUtility(info, IStaticURLInfo)
        with self.assertRaises(IOError):
            self._get_function()(self.request, 'static/notexists')


class TestSettingsFromEnv(unittest.TestCase):

    def _getFunction(self):
        from .. import settings_from_env
        return settings_from_env

    def _make_one(self, environ, settings_map=None):
        from .. import NO_VALUE
        if settings_map is None:
            settings_map = (
                ('ENV_FOO', 'test.foo', None, None),
                ('ENV_BAR', 'test.bar', NO_VALUE, None),
                ('ENV_BAZ', 'test.baz', 'demo', None),
                ('ENV_BAX', 'test.bax', '42', int))
        func = self._getFunction()
        return func(settings_map, environ)

    def test_environ(self):
        result = self._make_one({
            'ENV_FOO': 'foo',
            'ENV_BAR': 'bar'})
        self.assertEqual(result['test.foo'], 'foo')
        self.assertEqual(result['test.bar'], 'bar')
        self.assertEqual(result['test.baz'], 'demo')
        self.assertEqual(result['test.bax'], 42)

    def test_environ_missing(self):
        with self.assertRaises(RuntimeError):
            self._make_one({})


class TestTmMaybeActivate(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp()
        self.request = testing.DummyRequest()
        self.request.registry = self.config.registry

    def tearDown(self):
        testing.tearDown()

    def _get_function(self):
        from .. import tm_maybe_activate
        return tm_maybe_activate

    def test_paths(self):
        self.request.path_info = "/"
        self.assertTrue(self._get_function()(self.request))

    def test_static(self):
        self.request.path_info = "/static/"
        self.assertFalse(self._get_function()(self.request))