File: test_conf.py

package info (click to toggle)
python-pecan 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,276 kB
  • sloc: python: 10,400; makefile: 133; sh: 46
file content (356 lines) | stat: -rw-r--r-- 11,842 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import sys
import tempfile
import unittest

from six import b as b_
from webtest import TestApp

import pecan
from pecan.tests import PecanTestCase


__here__ = os.path.dirname(__file__)


class TestConf(PecanTestCase):

    def test_update_config_fail_identifier(self):
        """Fail when naming does not pass correctness"""
        from pecan import configuration
        bad_dict = {'bad name': 'value'}
        self.assertRaises(ValueError, configuration.Config, bad_dict)

    def test_update_set_config(self):
        """Update an empty configuration with the default values"""
        from pecan import configuration

        conf = configuration.initconf()
        conf.update(configuration.conf_from_file(os.path.join(
            __here__,
            'config_fixtures/config.py'
        )))

        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, 'myproject/templates')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '1.1.1.1')
        self.assertEqual(conf.server.port, '8081')

    def test_update_set_default_config(self):
        """Update an empty configuration with the default values"""
        from pecan import configuration

        conf = configuration.initconf()
        conf.update(configuration.conf_from_file(os.path.join(
            __here__,
            'config_fixtures/empty.py'
        )))

        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

    def test_update_force_dict(self):
        """Update an empty configuration with the default values"""
        from pecan import configuration
        conf = configuration.initconf()
        conf.update(configuration.conf_from_file(os.path.join(
            __here__,
            'config_fixtures/forcedict.py'
        )))

        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

        self.assertTrue(isinstance(conf.beaker, dict))
        self.assertEqual(conf.beaker['session.key'], 'key')
        self.assertEqual(conf.beaker['session.type'], 'cookie')
        self.assertEqual(
            conf.beaker['session.validate_key'],
            '1a971a7df182df3e1dec0af7c6913ec7'
        )
        self.assertEqual(conf.beaker.get('__force_dict__'), None)

    def test_update_config_with_dict(self):
        from pecan import configuration
        conf = configuration.initconf()
        d = {'attr': True}
        conf['attr'] = d
        self.assertTrue(conf.attr.attr)

    def test_config_repr(self):
        from pecan import configuration
        conf = configuration.Config({'a': 1})
        self.assertEqual(repr(conf), "Config({'a': 1})")

    def test_config_from_dict(self):
        from pecan import configuration
        conf = configuration.conf_from_dict({})
        conf['path'] = '%(confdir)s'
        self.assertTrue(os.path.samefile(conf['path'], os.getcwd()))

    def test_config_from_file(self):
        from pecan import configuration
        path = os.path.join(
            os.path.dirname(__file__), 'config_fixtures', 'config.py'
        )
        configuration.conf_from_file(path)

    def test_config_illegal_ids(self):
        from pecan import configuration
        conf = configuration.Config({})
        conf.update(configuration.conf_from_file(os.path.join(
            __here__,
            'config_fixtures/bad/module_and_underscore.py'
        )))
        self.assertEqual([], list(conf))

    def test_config_missing_file(self):
        from pecan import configuration
        path = ('doesnotexist.py',)
        configuration.Config({})
        self.assertRaises(
            RuntimeError,
            configuration.conf_from_file,
            os.path.join(__here__, 'config_fixtures', *path)
        )

    def test_config_missing_file_on_path(self):
        from pecan import configuration
        path = ('bad', 'bad', 'doesnotexist.py',)
        configuration.Config({})

        self.assertRaises(
            RuntimeError,
            configuration.conf_from_file,
            os.path.join(__here__, 'config_fixtures', *path)
        )

    def test_config_with_syntax_error(self):
        from pecan import configuration
        with tempfile.NamedTemporaryFile('wb') as f:
            f.write(b_('\n'.join(['if false', 'var = 3'])))
            f.flush()
            configuration.Config({})

            self.assertRaises(
                SyntaxError,
                configuration.conf_from_file,
                f.name
            )

    def test_config_with_bad_import(self):
        from pecan import configuration
        path = ('bad', 'importerror.py')
        configuration.Config({})

        self.assertRaises(
            ImportError,
            configuration.conf_from_file,
            os.path.join(
                __here__,
                'config_fixtures',
                *path
            )
        )

    def test_config_dir(self):
        from pecan import configuration
        conf = configuration.Config({})
        self.assertEqual([], dir(conf))
        conf = configuration.Config({'a': 1})
        self.assertEqual(['a'], dir(conf))

    def test_config_bad_key(self):
        from pecan import configuration
        conf = configuration.Config({'a': 1})
        assert conf.a == 1
        self.assertRaises(AttributeError, getattr, conf, 'b')

    def test_config_get_valid_key(self):
        from pecan import configuration
        conf = configuration.Config({'a': 1})
        assert conf.get('a') == 1

    def test_config_get_invalid_key(self):
        from pecan import configuration
        conf = configuration.Config({'a': 1})
        assert conf.get('b') is None

    def test_config_get_invalid_key_return_default(self):
        from pecan import configuration
        conf = configuration.Config({'a': 1})
        assert conf.get('b', True) is True

    def test_config_to_dict(self):
        from pecan import configuration
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        to_dict = conf.to_dict()

        assert isinstance(to_dict, dict)
        assert to_dict['server']['host'] == '0.0.0.0'
        assert to_dict['server']['port'] == '8080'
        assert to_dict['app']['modules'] == []
        assert to_dict['app']['root'] is None
        assert to_dict['app']['static_root'] == 'public'
        assert to_dict['app']['template_path'] == ''

    def test_config_to_dict_nested(self):
        from pecan import configuration
        """have more than one level nesting and convert to dict"""
        conf = configuration.initconf()
        nested = {'one': {'two': 2}}
        conf['nested'] = nested

        to_dict = conf.to_dict()

        assert isinstance(to_dict, dict)
        assert to_dict['server']['host'] == '0.0.0.0'
        assert to_dict['server']['port'] == '8080'
        assert to_dict['app']['modules'] == []
        assert to_dict['app']['root'] is None
        assert to_dict['app']['static_root'] == 'public'
        assert to_dict['app']['template_path'] == ''
        assert to_dict['nested']['one']['two'] == 2

    def test_config_to_dict_prefixed(self):
        from pecan import configuration
        """Add a prefix for keys"""
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        to_dict = conf.to_dict('prefix_')

        assert isinstance(to_dict, dict)
        assert to_dict['prefix_server']['prefix_host'] == '0.0.0.0'
        assert to_dict['prefix_server']['prefix_port'] == '8080'
        assert to_dict['prefix_app']['prefix_modules'] == []
        assert to_dict['prefix_app']['prefix_root'] is None
        assert to_dict['prefix_app']['prefix_static_root'] == 'public'
        assert to_dict['prefix_app']['prefix_template_path'] == ''


class TestGlobalConfig(PecanTestCase):

    def tearDown(self):
        from pecan import configuration
        configuration.set_config(
            dict(configuration.initconf()),
            overwrite=True
        )

    def test_paint_from_dict(self):
        from pecan import configuration
        configuration.set_config({'foo': 'bar'})
        assert dict(configuration._runtime_conf) != {'foo': 'bar'}
        self.assertEqual(configuration._runtime_conf.foo, 'bar')

    def test_overwrite_from_dict(self):
        from pecan import configuration
        configuration.set_config({'foo': 'bar'}, overwrite=True)
        assert dict(configuration._runtime_conf) == {'foo': 'bar'}

    def test_paint_from_file(self):
        from pecan import configuration
        configuration.set_config(os.path.join(
            __here__,
            'config_fixtures/foobar.py'
        ))
        assert dict(configuration._runtime_conf) != {'foo': 'bar'}
        assert configuration._runtime_conf.foo == 'bar'

    def test_overwrite_from_file(self):
        from pecan import configuration
        configuration.set_config(
            os.path.join(
                __here__,
                'config_fixtures/foobar.py',
            ),
            overwrite=True
        )
        assert dict(configuration._runtime_conf) == {'foo': 'bar'}

    def test_set_config_none_type(self):
        from pecan import configuration
        self.assertRaises(RuntimeError, configuration.set_config, None)

    def test_set_config_to_dir(self):
        from pecan import configuration
        self.assertRaises(RuntimeError, configuration.set_config, '/')


class TestConfFromEnv(PecanTestCase):
    #
    # Note that there is a good chance of pollution if ``tearDown`` does not
    # reset the configuration like this class does. If implementing new classes
    # for configuration this tearDown **needs to be implemented**
    #

    def setUp(self):
        super(TestConfFromEnv, self).setUp()
        self.addCleanup(self._remove_config_key)

        from pecan import configuration
        self.get_conf_path_from_env = configuration.get_conf_path_from_env

    def _remove_config_key(self):
        os.environ.pop('PECAN_CONFIG', None)

    def test_invalid_path(self):
        os.environ['PECAN_CONFIG'] = '/'
        msg = "PECAN_CONFIG was set to an invalid path: /"
        self.assertRaisesRegexp(
            RuntimeError,
            msg,
            self.get_conf_path_from_env
        )

    def test_is_not_set(self):
        msg = "PECAN_CONFIG is not set and " \
              "no config file was passed as an argument."
        self.assertRaisesRegexp(
            RuntimeError,
            msg,
            self.get_conf_path_from_env
        )

    def test_return_valid_path(self):
        __here__ = os.path.abspath(__file__)
        os.environ['PECAN_CONFIG'] = __here__
        assert self.get_conf_path_from_env() == __here__


class TestConfigCleanup(unittest.TestCase):

    def setUp(self):
        class RootController(object):
            @pecan.expose()
            def index(self):
                return 'Hello, World!'
        self.app = TestApp(pecan.Pecan(RootController()))

    def tearDown(self):
        pecan.configuration.set_config(pecan.configuration.DEFAULT,
                                       overwrite=True)

    def test_conf_default(self):
        assert pecan.conf.server.to_dict() == {
            'port': '8080', 'host': '0.0.0.0'
        }

    def test_conf_changed(self):
        pecan.conf.server = pecan.configuration.Config({'port': '80'})
        assert pecan.conf.server.to_dict() == {'port': '80'}