Ejemplo n.º 1
0
 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'])
Ejemplo n.º 2
0
 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'])
Ejemplo n.º 3
0
 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'])
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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'])
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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
         })
Ejemplo n.º 9
0
 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
         })
Ejemplo n.º 10
0
 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'])
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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'])