Пример #1
0
    def test_float_list_validator_applied_to_each_element(self):
        environ['KEY'] = '1.2,4.6'
        values_validated = []

        def validator(input):
            nonlocal values_validated
            values_validated.append(input)

        self.config.declare('key', parse_float_list(validator=validator))
        self.assertListEqual([1.2, 4.6], values_validated)
Пример #2
0
    def test_float_list_validator_fails(self):
        environ['KEY'] = '1.2,4.6'
        with self.assertRaises(ConfigParseError) as context:

            def validator(input):
                raise RuntimeError('some message')

            self.config.declare('key', parse_float_list(validator=validator))

        self.assertEqual(context.exception.key, 'KEY')
Пример #3
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)
Пример #4
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)
Пример #5
0
 def test_float_list_one_value_invalid(self):
     environ['KEY'] = '1.9,em,45.0'
     with self.assertRaises(ConfigParseError):
         self.config.declare('key', parse_float_list())
Пример #6
0
 def test_float_list_different_separator(self):
     environ['KEY'] = '1.4-4.3-9.56'
     self.config.declare('key', parse_float_list(separator='-'))
     result = self.config.get('key')
     self.assertEqual([1.4, 4.3, 9.56], result)
Пример #7
0
 def test_float_list_return_default_when_falsy(self):
     default_value = []
     self.config.declare('key', parse_float_list(default_value))
     result = self.config.get('key')
     self.assertEqual(default_value, result)
Пример #8
0
 def test_float_list_return_default(self):
     default_value = [1.3, 5.8, 5.89008]
     self.config.declare('key', parse_float_list(default_value))
     result = self.config.get('key')
     self.assertEqual(default_value, result)
Пример #9
0
 def test_float_list_variable_missing(self):
     with self.assertRaises(ConfigValueError):
         self.config.declare('key', parse_float_list())
Пример #10
0
 def test_float_list_trim_whitespace(self):
     environ['KEY'] = '123\t, 456\n,  789    \t'
     self.config.declare('key', parse_float_list())
     result = self.config.get('key')
     self.assertEqual(result, [123, 456, 789])
Пример #11
0
 def test_float_list(self):
     environ['KEY'] = '123,456,789'
     self.config.declare('key', parse_float_list())
     result = self.config.get('key')
     self.assertEqual(result, [123, 456, 789])