def test_float_validator_fails(self): environ['KEY'] = '1.4' with self.assertRaises(ConfigParseError) as context: def validator(input): self.assertEqual('1.4', input) raise RuntimeError('some message') self.config.declare('key', parse_float(validator=validator)) self.assertEqual(context.exception.key, 'KEY')
def test_recursive_dict_nested_element_missing(self): environ['KEY_STRING'] = 'string' with self.assertRaises(ConfigValueError): self.config.declare( 'key', { 'string': parse_str(), 'dict2': { 'dict3': { 'float': parse_float(), }, } }, )
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)
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)
def test_float_value_is_invalid(self): environ['KEY'] = 'some_invalid_float' with self.assertRaises(ConfigParseError): self.config.declare('key', parse_float())
def test_float_return_default_when_is_falsy(self): self.config.declare('key', parse_float(0)) result = self.config.get('key') self.assertEqual(0, result)
def test_float_return_default(self): self.config.declare('key', parse_float(1.4)) result = self.config.get('key') self.assertEqual(1.4, result)
def test_float_variable_missing(self): with self.assertRaises(ConfigValueError): self.config.declare('key', parse_float())
def test_float(self): environ['KEY'] = '1.4' self.config.declare('key', parse_float()) result = self.config.get('key') self.assertEqual(result, 1.4)