def test_loading_conf_without_harvesters(self): """ An exception must be raised if the 'harvesters' section is empty """ with self.assertRaises(AssertionError, msg='No harvesters are configured'): with self.assertLogs(harvest.LOGGER): _ = harvest.Configuration(CONFIGURATION_FILES['no_harvesters'])
def test_default_config_path(self): """ If the file path cannot be obtained from the CLI or constructor arguments, the default value must be used """ with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration() self.assertEqual(configuration._path, CONFIGURATION_FILES['ok'])
def test_loading_conf_without_harvester_section(self): """ An exception must be raised if the configuration does not contain a 'harvesters' section """ with self.assertRaises(AssertionError, msg='Invalid top-level keys'): with self.assertLogs(harvest.LOGGER): _ = harvest.Configuration( CONFIGURATION_FILES['no_harvesters_section'])
def test_get_config_path_from_cli(self): """Must be able to retrieve the configuration file path from the CLI""" conf_file_path = CONFIGURATION_FILES['ok'] # Test short argument with mock.patch.object(sys, 'argv', [harvest.__file__, '-c', conf_file_path]): with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration() self.assertEqual(configuration._path, conf_file_path) # Test long argument with mock.patch.object(sys, 'argv', [harvest.__file__, '--config', conf_file_path]): with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration() self.assertEqual(configuration._path, conf_file_path)
def test_loading_conf_without_class(self): """ An exception must be raised if a harvester configuration does not contain 'class' """ message_regex = "^Harvester configuration must contain the following keys: .*$" with self.assertRaisesRegex(AssertionError, message_regex): with self.assertLogs(harvest.LOGGER): _ = harvest.Configuration(CONFIGURATION_FILES['no_class'])
def test_iterable(self): """Configuration objects must be iterable""" with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration(CONFIGURATION_FILES['ok']) self.assertTrue(callable(getattr(configuration, '__iter__'))) config_iterator = iter(configuration) self.assertEqual(next(config_iterator), 'update_vocabularies') self.assertEqual(next(config_iterator), 'update_pythesint') self.assertEqual(next(config_iterator), 'harvesters') with self.assertRaises(StopIteration): next(config_iterator)
def test_inexistent_config_file(self): """ An exception must be raised if an attempt is made to load the configuration from an empty file, and an error message must be logged """ conf_file_path = '/this_file_should_not_exist' with self.assertRaises(AssertionError, msg='Configuration data is empty'): with self.assertLogs(harvest.LOGGER, level=logging.ERROR) as logs_cm: _ = harvest.Configuration(conf_file_path) self.assertEqual(len(logs_cm.records), 1) self.assertEqual(logs_cm.records[0].exc_info[0], FileNotFoundError)
def test_loading_valid_conf(self): """Correct configuration file parsing""" with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration(CONFIGURATION_FILES['ok']) self.assertDictEqual( configuration._data, { 'harvesters': { 'test': { 'class': 'TestHarvester', 'urls': ['https://random1.url', 'https://random2.url'] } }, 'update_vocabularies': True, 'update_pythesint': True })
def test_loading_valid_conf_with_env_var_inside_it(self): """Correct configuration file parsing with changing the 'password name' into 'password value' """ os.environ["test_password"] = "******" os.environ["test_sth_like_password"] = "******" with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration( CONFIGURATION_FILES['ok_pass']) self.assertDictEqual( configuration._data, { 'harvesters': { 'test': { 'class': 'TestHarvester', 'urls': ['https://random1.url', 'https://random2.url'], 'password': '******', 'sth_like_password': '******' }, }, 'update_vocabularies': True, 'update_pythesint': True })
def test_loading_empty_conf(self): """An exception must be raised if the configuration file is empty""" with self.assertRaises(AssertionError, msg='Configuration data is empty'): with self.assertLogs(harvest.LOGGER): _ = harvest.Configuration(CONFIGURATION_FILES['empty'])
def test_length(self): """the __len__ method must be correctly implemented""" with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration(CONFIGURATION_FILES['ok']) self.assertTrue(callable(getattr(configuration, '__len__'))) self.assertEqual(len(configuration), 3)
def test_subscriptable(self): """Configuration objects must be subscriptable""" with self.assertLogs(harvest.LOGGER): configuration = harvest.Configuration(CONFIGURATION_FILES['ok']) self.assertTrue(callable(getattr(configuration, '__getitem__'))) self.assertIsNotNone(configuration['harvesters'])