File: test_paster.py

package info (click to toggle)
python-pyramid 1.10.2+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,052 kB
  • sloc: python: 42,982; makefile: 41
file content (193 lines) | stat: -rw-r--r-- 6,256 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import unittest
from .test_scripts.dummy import DummyLoader

here = os.path.dirname(__file__)


class Test_get_app(unittest.TestCase):
    def _callFUT(self, config_file, section_name, options=None, _loader=None):
        import pyramid.paster

        old_loader = pyramid.paster.get_config_loader
        try:
            if _loader is not None:
                pyramid.paster.get_config_loader = _loader
            return pyramid.paster.get_app(
                config_file, section_name, options=options
            )
        finally:
            pyramid.paster.get_config_loader = old_loader

    def test_it(self):
        app = DummyApp()
        loader = DummyLoader(app=app)
        result = self._callFUT(
            '/foo/bar/myapp.ini', 'myapp', options={'a': 'b'}, _loader=loader
        )
        self.assertEqual(loader.uri.path, '/foo/bar/myapp.ini')
        self.assertEqual(len(loader.calls), 1)
        self.assertEqual(loader.calls[0]['op'], 'app')
        self.assertEqual(loader.calls[0]['name'], 'myapp')
        self.assertEqual(loader.calls[0]['defaults'], {'a': 'b'})
        self.assertEqual(result, app)

    def test_it_with_dummyapp_requiring_options(self):
        options = {'bar': 'baz'}
        app = self._callFUT(
            os.path.join(here, 'fixtures', 'dummy.ini'),
            'myapp',
            options=options,
        )
        self.assertEqual(app.settings['foo'], 'baz')


class Test_get_appsettings(unittest.TestCase):
    def _callFUT(self, config_file, section_name, options=None, _loader=None):
        import pyramid.paster

        old_loader = pyramid.paster.get_config_loader
        try:
            if _loader is not None:
                pyramid.paster.get_config_loader = _loader
            return pyramid.paster.get_appsettings(
                config_file, section_name, options=options
            )
        finally:
            pyramid.paster.get_config_loader = old_loader

    def test_it(self):
        values = {'a': 1}
        loader = DummyLoader(app_settings=values)
        result = self._callFUT(
            '/foo/bar/myapp.ini', 'myapp', options={'a': 'b'}, _loader=loader
        )
        self.assertEqual(loader.uri.path, '/foo/bar/myapp.ini')
        self.assertEqual(len(loader.calls), 1)
        self.assertEqual(loader.calls[0]['op'], 'app_settings')
        self.assertEqual(loader.calls[0]['name'], 'myapp')
        self.assertEqual(loader.calls[0]['defaults'], {'a': 'b'})
        self.assertEqual(result, values)

    def test_it_with_dummyapp_requiring_options(self):
        options = {'bar': 'baz'}
        result = self._callFUT(
            os.path.join(here, 'fixtures', 'dummy.ini'),
            'myapp',
            options=options,
        )
        self.assertEqual(result['foo'], 'baz')


class Test_setup_logging(unittest.TestCase):
    def _callFUT(self, config_file, global_conf=None, _loader=None):
        import pyramid.paster

        old_loader = pyramid.paster.get_config_loader
        try:
            if _loader is not None:
                pyramid.paster.get_config_loader = _loader
            return pyramid.paster.setup_logging(config_file, global_conf)
        finally:
            pyramid.paster.get_config_loader = old_loader

    def test_it_no_global_conf(self):
        loader = DummyLoader()
        self._callFUT('/abc.ini', _loader=loader)
        self.assertEqual(loader.uri.path, '/abc.ini')
        self.assertEqual(len(loader.calls), 1)
        self.assertEqual(loader.calls[0]['op'], 'logging')
        self.assertEqual(loader.calls[0]['defaults'], None)

    def test_it_global_conf_empty(self):
        loader = DummyLoader()
        self._callFUT('/abc.ini', global_conf={}, _loader=loader)
        self.assertEqual(loader.uri.path, '/abc.ini')
        self.assertEqual(len(loader.calls), 1)
        self.assertEqual(loader.calls[0]['op'], 'logging')
        self.assertEqual(loader.calls[0]['defaults'], {})

    def test_it_global_conf_not_empty(self):
        loader = DummyLoader()
        self._callFUT('/abc.ini', global_conf={'key': 'val'}, _loader=loader)
        self.assertEqual(loader.uri.path, '/abc.ini')
        self.assertEqual(len(loader.calls), 1)
        self.assertEqual(loader.calls[0]['op'], 'logging')
        self.assertEqual(loader.calls[0]['defaults'], {'key': 'val'})


class Test_bootstrap(unittest.TestCase):
    def _callFUT(self, config_uri, request=None):
        from pyramid.paster import bootstrap

        return bootstrap(config_uri, request)

    def setUp(self):
        import pyramid.paster

        self.original_get_app = pyramid.paster.get_app
        self.original_prepare = pyramid.paster.prepare
        self.app = app = DummyApp()
        self.root = root = Dummy()

        class DummyGetApp(object):
            def __call__(self, *a, **kw):
                self.a = a
                self.kw = kw
                return app

        self.get_app = pyramid.paster.get_app = DummyGetApp()

        class DummyPrepare(object):
            def __call__(self, *a, **kw):
                self.a = a
                self.kw = kw
                return {'root': root, 'closer': lambda: None}

        self.getroot = pyramid.paster.prepare = DummyPrepare()

    def tearDown(self):
        import pyramid.paster

        pyramid.paster.get_app = self.original_get_app
        pyramid.paster.prepare = self.original_prepare

    def test_it_request_with_registry(self):
        request = DummyRequest({})
        request.registry = dummy_registry
        result = self._callFUT('/foo/bar/myapp.ini', request)
        self.assertEqual(result['app'], self.app)
        self.assertEqual(result['root'], self.root)
        self.assertTrue('closer' in result)


class Dummy:
    pass


class DummyRegistry(object):
    settings = {}


dummy_registry = DummyRegistry()


class DummyApp:
    def __init__(self):
        self.registry = dummy_registry


def make_dummyapp(global_conf, **settings):
    app = DummyApp()
    app.settings = settings
    app.global_conf = global_conf
    return app


class DummyRequest:
    application_url = 'http://example.com:5432'
    script_name = ''

    def __init__(self, environ):
        self.environ = environ
        self.matchdict = {}