예제 #1
0
    def test_do_not_report_the_same_error_multiple_times(self):
        self.config.declare('err_key_1', parse_int())
        self.config.declare('err_key_1', parse_int())
        self.config.declare('err_key_1', parse_int())

        with self.assertRaises(AggregateConfigError) as context:
            self.config.get('undeclared')

        self.assertMatchSnapshot(str(context.exception))
예제 #2
0
 def test_load_prefixed_nested_variable(self):
     environ['NAMESPACE_KEY1_KEY2'] = '14'
     environ['NAMESPACE_KEY1_KEY3'] = '25'
     config = Config(namespace='namespace')
     config.declare('key1', {
         'key2': parse_int(),
         'key3': parse_int(),
     })
     self.assertEqual(14, config.get('key1').get('key2'))
     self.assertEqual(25, config.get('key1').get('key3'))
예제 #3
0
    def test_raise_config_value_error_when_prefixed_variable_does_not_exist(
            self):
        environ['KEY'] = '14'
        config = Config(namespace='namespace', defer_raise=False)
        with self.assertRaises(ConfigValueError) as context:
            config.declare('key', parse_int())

        self.assertMatchSnapshot(str(context.exception))
예제 #4
0
    def test_dont_raise_if_config_file_does_not_exist(self):
        environ['CONFIG_FILE'] = 'test/missing'
        self.config = Config(filename_variable='CONFIG_FILE')
        with self.assertLogs(logger=self.config.logger,
                             level='WARNING') as context:
            self.config.declare('missing_value', parse_int(), ('test', ),
                                'test')

        self.assertIn('Config file not found', context[1][0])
예제 #5
0
    def test_int_validator_fails(self):
        environ['KEY'] = '13'
        with self.assertRaises(ConfigParseError) as context:

            def validator(input):
                self.assertEqual('13', input)
                raise RuntimeError('some message')

            self.config.declare('key', parse_int(validator=validator))
        self.assertEqual(context.exception.key, 'KEY')
예제 #6
0
 def test_dict_element_missing(self):
     environ['KEY_STRING'] = 'string'
     with self.assertRaises(ConfigValueError):
         self.config.declare(
             'key',
             {
                 'string': parse_str(),
                 'int': parse_int(),
             },
         )
예제 #7
0
    def test_report_message_for_missing_environment_variables(self):
        environ['INT_VALUE'] = 'some int value'
        environ['UNDECLARED'] = 'some value'
        environ['ERR_KEY_1_VALUE1'] = 'some int value'

        self.config.declare('err_key_1', parse_int())
        self.config.declare(
            'err_key_1', {
                'value1': parse_int(),
                'dict2': {
                    'key3': parse_str(),
                    'dict3': {
                        'key4': parse_str()
                    }
                }
            })
        self.config.declare('int_value', parse_int())
        with self.assertRaises(AggregateConfigError) as context:
            self.config.get('undeclared')

        self.assertMatchSnapshot(str(context.exception))
예제 #8
0
    def test_load_bash_file(self):
        environ['CONFIG_FILE'] = 'test/env'
        self.config = Config(filename_variable='CONFIG_FILE')
        self.config.declare('first_variable', parse_int(), ('test', ), 'test')
        self.config.declare('second_variable', parse_int(), ('test', ), 'test')
        self.config.declare('third_variable', parse_int(), ('test', ), 'test')
        self.config.declare('fourth_variable', parse_int(), ('test', ), 'test')
        self.config.declare('fifth_variable', parse_int(), ('test', ), 'test')
        self.config.declare('sixth_variable', parse_int(), ('test', ), 'test')
        self.config.declare('dict1', {
            'value1': parse_int(),
            'value2': parse_int()
        }, ('test', ), 'test')
        with self.assertRaises(AggregateConfigError) as context:
            self.config.get('first_variable')

        first_line, *rest = str(context.exception).split('\n')
        # the absolute path to the config file varies depending on environment. It's not suitable for snapshot testing.
        self.assertTrue(
            re.match(r'^Errors in config file [/\-_A-Za-z0-9]+?/test/env:$',
                     first_line))
        self.assertMatchSnapshot('\n'.join(rest))
예제 #9
0
    def test_recursive_dict(self):
        environ['KEY_STRING'] = 'string'
        environ['KEY_DICT2_INT'] = '1'
        environ['KEY_DICT2_DICT3_FLOAT'] = '1.4'
        environ['KEY_STRING_LIST'] = 'one,two'
        environ['KEY_FLOAT_LIST'] = '1.4, 3.89'
        environ['KEY_INT_LIST'] = '1,2,3'
        self.config.declare(
            'key',
            {
                'string': parse_str(),
                'dict2': {
                    'int': parse_int(),
                    'dict3': {
                        'float': parse_float(),
                    },
                },
                'string_list': parse_str_list(),
                'int_list': parse_int_list(),
                'float_list': parse_float_list()
            },
        )

        result = self.config.get('key')

        self.assertDictEqual(
            {
                'string': 'string',
                'dict2': {
                    'int': 1,
                    'dict3': {
                        'float': 1.4,
                    },
                },
                'string_list': ['one', 'two'],
                'int_list': [1, 2, 3],
                'float_list': [1.4, 3.89]
            }, result)
예제 #10
0
    def test_dict(self):
        environ['KEY_STRING'] = 'string'
        environ['KEY_INT'] = '1'
        environ['KEY_FLOAT'] = '1.4'
        environ['KEY_BOOL'] = 'false'
        environ['KEY_STRING_LIST'] = 'one,two'
        environ['KEY_FLOAT_LIST'] = '1.4, 3.89'
        environ['KEY_INT_LIST'] = '1,2,3'
        environ['KEY_BOOL_LIST'] = '1,FALSE,no'
        self.config.declare(
            'key',
            {
                'string': parse_str(),
                'int': parse_int(),
                'float': parse_float(),
                'bool': parse_bool(),
                'string_list': parse_str_list(),
                'int_list': parse_int_list(),
                'float_list': parse_float_list(),
                'bool_list': parse_bool_list(),
            },
        )

        result = self.config.get('key')

        self.assertDictEqual(
            {
                'string': 'string',
                'int': 1,
                'float': 1.4,
                'bool': False,
                'string_list': ['one', 'two'],
                'int_list': [1, 2, 3],
                'float_list': [1.4, 3.89],
                'bool_list': [True, False, False],
            }, result)
예제 #11
0
 def test_int(self):
     environ['KEY'] = '1'
     self.config.declare('key', parse_int())
     result = self.config.get('key')
     self.assertEqual(result, 1)
예제 #12
0
 def test_load_prefixed_environment_variable(self):
     environ['NAMESPACE_KEY'] = '14'
     config = Config(namespace='namespace')
     config.declare('key', parse_int())
     self.assertEqual(14, config.get('key'))
예제 #13
0
 def test_dont_raise_error_if_filename_variable_does_not_exist(self):
     self.config = Config(filename_variable='MISSING_VARIABLE')
     self.config.declare('variable1', parse_int(), ('test', ), 'test')
예제 #14
0
 def test_raise_error_if_config_file_is_empty(self):
     environ['CONFIG_FILE'] = 'test/empty'
     self.config = Config(filename_variable='CONFIG_FILE')
     with self.assertRaises(ConfigFileEmptyError):
         self.config.declare('variable1', parse_int(), ('test', ), 'test')
예제 #15
0
 def test_int_value_is_invalid(self):
     environ['KEY'] = 'some_invalid_int'
     with self.assertRaises(ConfigParseError):
         self.config.declare('key', parse_int())
예제 #16
0
 def test_int_variable_missing(self):
     with self.assertRaises(ConfigValueError):
         self.config.declare('key', parse_int())
예제 #17
0
 def test_int_return_default(self):
     self.config.declare('key', parse_int(89))
     result = self.config.get('key')
     self.assertEqual(89, result)
예제 #18
0
 def test_int_return_default_when_is_falsy(self):
     self.config.declare('key', parse_int(0))
     result = self.config.get('key')
     self.assertEqual(0, result)