Ejemplo n.º 1
0
    def test__import_settings(self):
        settings_loader = SettingsLoader()
        application = 'app'
        group = 'local'
        storage = Storage()
        self.assertEqual(storage.keys(), [])  # Initialized storage is empty
        settings_loader.import_settings(group, storage)
        # No config file, storage unchanged
        self.assertEqual(storage.keys(), [])

        f_text = '/tmp/settings_loader_config.txt'

        # Test defaults and section overrides
        text = \
            """
[web2py]
auth.username = admin
auth.version = '3.33'
mail.username = mailer
mail.version = '4.44'
username = jimk
version = '1.11'

[app]
auth.version = '5.55'
mail.version = '6.66'
version = '2.22'
email = [email protected]
"""
        _config_file_from_text(f_text, text)
        settings_loader.config_file = f_text
        settings_loader.application = application

        settings_loader.get_settings()
        settings_loader.import_settings('zzz', storage)
        # Group has no settings, storage unchanged
        self.assertEqual(storage.keys(), [])
        settings_loader.import_settings(group, storage)
        self.assertEqual(sorted(storage.keys()), ['email', 'username',
                         'version'])

        # Group has settings, storage changed

        self.assertEqual(storage['email'], '*****@*****.**')
        self.assertEqual(storage['username'], 'jimk')
        self.assertEqual(storage['version'], '2.22')

        os.unlink(f_text)
        return
Ejemplo n.º 2
0
    def test__get_settings(self):
        settings_loader = SettingsLoader()
        settings_loader.get_settings()
        # No config file, no settings
        self.assertEqual(settings_loader.settings, {})

        tests = [
            {
                'label': 'empty file',
                'expect': {},
                'raise': NoSectionError,
                'text': '',
                },
            {
                'label': 'no web2py section',
                'expect': {},
                'raise': NoSectionError,
                'text': """
[fake_section]
setting = value
""",
                },
            {
                'label': 'web2py section empty',
                'expect': {},
                'raise': None,
                'text': """
[web2py]
""",
                },
            {
                'label': 'web2py one local setting',
                'expect': {'local': {'version': '1.11'}},
                'raise': None,
                'text': """
[web2py]
version = '1.11'
""",
                },
            {
                'label': 'web2py two local setting',
                'expect': {'local':
                    {'username': '******', 'version': '1.11'}},
                'raise': None,
                'text': """
[web2py]
username = jimk
version = '1.11'
""",
                },
            {
                'label': 'app section',
                'expect': {'local': {'email': '*****@*****.**',
                           'username': '******', 'version': '2.22'}},
                'raise': None,
                'text': """
[web2py]
username = jimk
version = '1.11'

[app]
version = '2.22'
email = [email protected]
""",
                },
            {
                'label': 'app section auth/mail',
                'expect': {
                    'auth': {'username': '******', 'version': '5.55'},
                    'mail': {'username': '******', 'version': '6.66'},
                    'local': {'email': '*****@*****.**', 'username': '******',
                           'version': '2.22'}},
                'raise': None,
                'text': """
[web2py]
auth.username = admin
auth.version = '3.33'
mail.username = mailer
mail.version = '4.44'
username = jimk
version = '1.11'

[app]
auth.version = '5.55'
mail.version = '6.66'
version = '2.22'
email = [email protected]
""",
                },
            ]

        f_text = '/tmp/settings_loader_config.txt'
        for t in tests:
            settings_loader = SettingsLoader()
            _config_file_from_text(f_text, t['text'])
            settings_loader.config_file = f_text
            settings_loader.application = 'app'
            if t['raise']:
                self.assertRaises(t['raise'],
                                  settings_loader.get_settings)
            else:
                settings_loader.get_settings()
            self.assertEqual(settings_loader.settings, t['expect'])

        # Test datatype handling.
        text = \
            """
[web2py]
s01_true = True
s02_false = False
s03_int = 123
s04_float = 123.45
s05_str1 = my setting
s06_str2 = 'my setting'
s07_str_true = 'True'
s08_str_int = '123'
s09_str_float = '123.45'

[app]
"""
        settings_loader = SettingsLoader()
        _config_file_from_text(f_text, text)
        settings_loader.config_file = f_text
        settings_loader.application = 'app'
        settings_loader.get_settings()

        self.assertEqual(sorted(settings_loader.settings['local'].keys()),
            [
                's01_true',
                's02_false',
                's03_int',
                's04_float',
                's05_str1',
                's06_str2',
                's07_str_true',
                's08_str_int',
                's09_str_float',
            ])

        slocal = settings_loader.settings['local']
        self.assertEqual(slocal['s01_true'], True)
        self.assertEqual(slocal['s02_false'], False)
        self.assertEqual(slocal['s03_int'], 123)
        self.assertEqual(slocal['s04_float'], 123.45)
        self.assertEqual(slocal['s05_str1'], 'my setting')
        self.assertEqual(slocal['s06_str2'], "'my setting'")
        self.assertEqual(slocal['s07_str_true'], 'True')
        self.assertEqual(slocal['s08_str_int'], '123')
        self.assertEqual(slocal['s09_str_float'], '123.45')

        os.unlink(f_text)