def testLoading(self, config_flag): """Tests loading config from file.""" values = _parse_flags('./program {}'.format(config_flag), default='nonexisting.py') self.assertIn('test_config', values) self.assert_equal_configs(values.test_config, mock_config.get_config())
def testTypes(self): """Tests whether various types of objects are valid.""" parser = config_flags._ConfigFileParser('test_config') self.assertEqual(parser.flag_type(), 'config object') test_config = mock_config.get_config() paths = ('float', 'integer', 'string', 'bool', 'dict', 'dict.float', 'dict.list', 'list', 'list[0]', 'object.float', 'object.integer', 'object.string', 'object.bool', 'object.dict', 'object.dict.float', 'object.dict.list', 'object.list', 'object.list[0]', 'object.tuple', 'object_reference.float', 'object_reference.integer', 'object_reference.string', 'object_reference.bool', 'object_reference.dict', 'object_reference.dict.float', 'object_copy.float', 'object_copy.integer', 'object_copy.string', 'object_copy.bool', 'object_copy.dict', 'object_copy.dict.float') paths_types = [ float, int, str, bool, dict, float, list, list, int, float, int, str, bool, dict, float, list, list, int, tuple, float, int, str, bool, dict, float, float, int, str, bool, dict, float, ] config_types = config_flags.GetTypes(paths, test_config) self.assertEqual(paths_types, config_types)
def testDefaultLoading(self): """Tests loading config from file using default path.""" for required in [True, False]: values = _parse_flags('./program', default=_TEST_CONFIG_FILE, required=required) self.assertIn('test_config', values) self.assert_equal_configs(values.test_config, mock_config.get_config())
def testOverride(self, config_flag, override_format): """Tests overriding config values from command line.""" overrides = { 'integer': 1, 'float': -3, 'dict.float': 3, 'object.integer': 12, 'object.float': 123, 'object.string': 'tom', 'object.dict.integer': -2, 'object.dict.float': 3.15, 'object.dict.list[0]': 101, 'object.dict.list[2][0]': 103, 'object.list[0]': 101, 'object.list[2][0]': 103, 'object.tuple': '(1,2,(1,2))', 'object.tuple_with_spaces': '(1, 2, (1, 2))', 'object_reference.dict.string': 'marry', 'object.dict.dict.float': 123, 'object_copy.float': 111.111 } override_flags = _get_override_flags(overrides, override_format) values = _parse_flags('./program {} {}'.format(config_flag, override_flags)) test_config = mock_config.get_config() test_config.integer = overrides['integer'] test_config.float = overrides['float'] test_config.dict['float'] = overrides['dict.float'] test_config.object.integer = overrides['object.integer'] test_config.object.float = overrides['object.float'] test_config.object.string = overrides['object.string'] test_config.object.dict['integer'] = overrides['object.dict.integer'] test_config.object.dict['float'] = overrides['object.dict.float'] test_config.object.dict['list'][0] = overrides['object.dict.list[0]'] test_config.object.dict['list'][2][0] = overrides[ 'object.dict.list[2][0]'] test_config.object.dict['list'][0] = overrides['object.list[0]'] test_config.object.dict['list'][2][0] = overrides['object.list[2][0]'] test_config.object.tuple = (1, 2, (1, 2)) test_config.object.tuple_with_spaces = (1, 2, (1, 2)) test_config.object_reference.dict['string'] = overrides[ 'object_reference.dict.string'] test_config.object.dict['dict']['float'] = overrides[ 'object.dict.dict.float'] test_config.object_copy.float = overrides['object_copy.float'] self.assert_equal_configs(values.test_config, test_config)
def testParserWrapping(self): """Tests callback based Parser wrapping.""" parser = flags.IntegerParser() test_config = mock_config.get_config() overrides = {} wrapped_parser = config_flags._ConfigFieldParser( parser, 'integer', test_config, overrides) wrapped_parser.parse('12321') self.assertEqual(test_config.integer, 12321) self.assertEqual(overrides, {'integer': 12321}) self.assertEqual(wrapped_parser.flag_type(), parser.flag_type()) self.assertEqual(wrapped_parser.syntactic_help, parser.syntactic_help) self.assertEqual(wrapped_parser.convert('3'), parser.convert('3'))
def test_types(self, path, path_type): """Tests whether various types of objects are valid.""" test_config = mock_config.get_config() self.assertEqual(path_type, config_path.get_type(path, test_config))
def testListOutOfRangeGet(self): """Tries to access out-of-range value in list.""" test_config = mock_config.get_config() with self.assertRaises(IndexError): config_flags.GetValue('dict.list[2][1]', test_config)
def testListOutOfRangeSet(self): """Tries to override out-of-range value in list.""" test_config = mock_config.get_config() with self.assertRaises(config_flags.UnsupportedOperationError): config_flags.SetValue('dict.list[2][1]', test_config, -1)
def testEmptyKey(self): """Tests calling an empty key update.""" test_config = mock_config.get_config() with self.assertRaises(ValueError): config_flags.SetValue('', test_config, None)
def testListExtraIndex(self): """Tries to index a non-indexable list element.""" test_config = mock_config.get_config() with self.assertRaises(IndexError): config_flags.GetValue('dict.list[0][0]', test_config)
def testReadingNonExistingKey(self): """Tests reading non existing key from config.""" test_config = mock_config.get_config() with self.assertRaises(config_flags.UnsupportedOperationError): config_flags.SetValue('dict.not_existing_key', test_config, 1)
def testReadingSettingExistingKeyInDict(self): """Tests setting non existing key from dict inside config.""" test_config = mock_config.get_config() with self.assertRaises(KeyError): config_flags.SetValue('dict.not_existing_key.key', test_config, 1)
def test_list_extra_index(self): """Tries to index a non-indexable list element.""" test_config = mock_config.get_config() with self.assertRaises(IndexError): config_path.get_value('dict.list[0][0]', test_config)
def test_list_out_of_range_set(self): """Tries to override out-of-range value in list.""" test_config = mock_config.get_config() with self.assertRaises(IndexError): config_path.set_value('dict.list[2][1]', test_config, -1)
def test_reading_non_existing_key(self): """Tests reading non existing key from config.""" test_config = mock_config.get_config() with self.assertRaises(KeyError): config_path.set_value('dict.not_existing_key', test_config, 1)
def test_empty_key(self): """Tests calling an empty key update.""" test_config = mock_config.get_config() with self.assertRaises(ValueError): config_path.set_value('', test_config, None)
def test_reading_setting_existing_key_in_dict(self): """Tests setting non existing key from dict inside config.""" test_config = mock_config.get_config() with self.assertRaises(KeyError): config_path.set_value('dict.not_existing_key.key', test_config, 1)