def test_load_from_path_should_raise_error_if_path_does_not_exist(self): registry = Registry('context') with self.assertRaises(RegistryException) as context: registry.load_from_path("/non/existent/path") self.assertEqual( "[Errno 2] No such file or directory: '/non/existent/path'", str(context.exception))
def test_registry_should_be_iterable_on_service_id(self): registry = Registry('context') registry._indicators['service1'] = "s1" registry._indicators['service2'] = "s2" self.assertEqual(2, len(list(registry.__iter__()))) self.assertIn('service1', list(registry.__iter__())) self.assertIn('service2', list(registry.__iter__()))
def register(context): global registry definitions_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "definitions") registry = Registry(context) registry.load_from_path(definitions_path) logger.info("Indicators successfully loaded from {0}".format(definitions_path))
def test_load_from_file_path_should_raise_error_if_invalid_file(self): registry = Registry('context') with self.assertRaises(ScannerError) as context: registry._load_from_file_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', 'invalid.yaml'))
def test_load_from_empty_path_should_not_add_indicators(self): registry = Registry("context") registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', 'empty')) self.assertEqual(0, len(registry._indicators))
def test_load_from_path_should_raise_error_if_path_is_file(self): registry = Registry('context') with self.assertRaises(RegistryException) as context: path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', 'empty.txt') registry.load_from_path(path) self.assertEqual("[Errno 20] Not a directory: '{0}'".format(path), str(context.exception))
def test_load_from_path_with_invalid_files_should_not_add_indicators( self): registry = Registry("context") with self.assertRaises(RegistryException): registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.4-indicators.2-invalid')) self.assertEqual(0, len(registry._indicators))
def test_load_from_path_should_raise_error_if_path_is_file(self): registry = Registry('context') with self.assertRaises(RegistryException) as context: path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', 'empty.txt') registry.load_from_path(path) self.assertEqual( "[Errno 20] Not a directory: '{0}'".format(path), str(context.exception))
def register(context): global registry definitions_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'definitions') registry = Registry(context) registry.load_from_path(definitions_path) logger.info( "Indicators successfully loaded from {0}".format(definitions_path))
def test_load_from_path_should_load_from_file_path_if_files_in_path(self): registry = Registry('context') mock_from_file_path = Mock() with patch( 'genweb.core.indicators.registry.Registry._load_from_file_path', side_effect=mock_from_file_path): registry.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', '3-files')) self.assertEqual(3, mock_from_file_path.call_count)
def test_load_from_file_path_should_add_from_dict_if_valid_file(self): registry = Registry('context') mock_add_indicator_from_dict = Mock() with patch( 'genweb.core.indicators.registry.Registry._add_indicator_from_dict', side_effect=mock_add_indicator_from_dict): registry._load_from_file_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', '3-indicators.yaml')) self.assertEqual(3, mock_add_indicator_from_dict.call_count)
def setUp(self): self.reporter = WebServiceReporter("url", "api_key") self.registry = Registry("context") self.registry.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.3-indicators')) self.registry_with_exception = Registry("context") self.registry_with_exception.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', '1-service.1-indicator.1-category_exception'))
def setUp(self): self.reporter = WebServiceReporter("url", "api_key") self.registry = Registry("context") self.registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.3-indicators')) self.registry_with_exception = Registry("context") self.registry_with_exception.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '1-service.1-indicator.1-category_exception'))
def test_load_from_file_path_should_add_from_dict_if_valid_file(self): registry = Registry('context') mock_add_indicator_from_dict = Mock() with patch( 'genweb.core.indicators.registry.Registry._add_indicator_from_dict', side_effect=mock_add_indicator_from_dict ): registry._load_from_file_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', '3-indicators.yaml')) self.assertEqual(3, mock_add_indicator_from_dict.call_count)
def test_load_from_path_should_load_from_file_path_if_files_in_path(self): registry = Registry('context') mock_from_file_path = Mock() with patch( 'genweb.core.indicators.registry.Registry._load_from_file_path', side_effect=mock_from_file_path ): registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '3-files')) self.assertEqual(3, mock_from_file_path.call_count)
def test_load_from_path_should_not_load_from_file_path_if_no_files_in_path( self): registry = Registry('context') mock_from_file_path = Mock() with patch( 'genweb.core.indicators.registry.Registry._load_from_file_path', side_effect=mock_from_file_path): registry.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', 'empty')) # The folder is not actually empty, it contains a .gitkeep file so that # it can be tracked by Git. Ideally it should be empty but there is no # way to upload an empty folder to Git. self.assertEqual(1, mock_from_file_path.call_count)
def test_load_from_path_should_not_load_from_file_path_if_no_files_in_path( self): registry = Registry('context') mock_from_file_path = Mock() with patch( 'genweb.core.indicators.registry.Registry._load_from_file_path', side_effect=mock_from_file_path ): registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', 'empty')) # The folder is not actually empty, it contains a .gitkeep file so that # it can be tracked by Git. Ideally it should be empty but there is no # way to upload an empty folder to Git. self.assertEqual(1, mock_from_file_path.call_count)
def test_add_indicator_should_override_existent_indicators(self): registry = Registry('context') mock_indicator_old = Mock(service='s1', id='i1', description='old one') mock_indicator_new = Mock(service='s1', id='i1', description='new one') with patch('genweb.core.indicators.model.Indicator.instance_from_dict', side_effect=(mock_indicator_old, mock_indicator_new, mock_indicator_old)): registry._add_indicator_from_dict({}) self.assertEqual(mock_indicator_old, registry._indicators['s1']['i1']) registry._add_indicator_from_dict({}) self.assertEqual(mock_indicator_new, registry._indicators['s1']['i1']) registry._add_indicator_from_dict({}) self.assertEqual(mock_indicator_old, registry._indicators['s1']['i1'])
def test_add_indicator_should_override_existent_indicators(self): registry = Registry('context') mock_indicator_old = Mock(service='s1', id='i1', description='old one') mock_indicator_new = Mock(service='s1', id='i1', description='new one') with patch('genweb.core.indicators.model.Indicator.instance_from_dict', side_effect=(mock_indicator_old, mock_indicator_new, mock_indicator_old)): registry._add_indicator_from_dict({}) self.assertEqual( mock_indicator_old, registry._indicators['s1']['i1']) registry._add_indicator_from_dict({}) self.assertEqual( mock_indicator_new, registry._indicators['s1']['i1']) registry._add_indicator_from_dict({}) self.assertEqual( mock_indicator_old, registry._indicators['s1']['i1'])
def test_report_should_not_raise_error_if_valid_type(self): reporter = WebServiceReporter("url", "api_key") with patch( 'genweb.core.indicators.reporter.WebServiceReporter._report_registry', side_effect=(None,)): reporter.report(Registry('context')) with patch( 'genweb.core.indicators.reporter.WebServiceReporter._report_indicator_dict', side_effect=(None,)): reporter.report({}) with patch( 'genweb.core.indicators.reporter.WebServiceReporter._report_indicator', side_effect=(None,)): reporter.report(Indicator('service', 'id', 'description')) with patch( 'genweb.core.indicators.reporter.WebServiceReporter._report_category', side_effect=(None,)): reporter.report(Category( 'id', 'description', 'type', 'frequency', 'calculator'))
class TestRegistry(unittest.TestCase): def setUp(self): self.reporter = WebServiceReporter("url", "api_key") self.registry = Registry("context") self.registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.3-indicators')) self.registry_with_exception = Registry("context") self.registry_with_exception.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '1-service.1-indicator.1-category_exception')) def test_report_registry_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry) self.assertEqual(3, mock_update_indicator.call_count) mock_update_indicator.assert_any_call( 'service-1', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call( 'service-2', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call( 'service-2', 'indicator-2', 'Indicator 2') self.assertEqual(6, mock_update_category.call_count) mock_update_category.assert_any_call( 'service-1', 'indicator-1', 'category-1.1', 'Category 1.1', 'type 1.1', 'frequency 1.1', 111) mock_update_category.assert_any_call( 'service-1', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 112) mock_update_category.assert_any_call( 'service-2', 'indicator-1', 'category-1.1', 'Category 1.1', None, None, 211) mock_update_category.assert_any_call( 'service-2', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 212) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_registry_should_raise_reporter_exception_if_client_exception(self): with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=ClientException('Something wrong with WS')): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=ClientException('Something wrong with WS')): with self.assertRaises(ReporterException) as context: self.reporter.report(self.registry) self.assertEqual( 'WS client exception (Something wrong with WS)', context.exception.message) def test_report_registry_should_raise_reporter_exception_if_calculator_exception(self): with patch('genweb.core.indicators.client.Client.update_indicator'): with patch('genweb.core.indicators.client.Client.update_category'): with self.assertRaises(ReporterException) as context: self.reporter.report(self.registry_with_exception) self.assertEqual( 'Error when calculating category (Oh!)', context.exception.message) def test_report_dict_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry['service-2']) self.assertEqual(2, mock_update_indicator.call_count) mock_update_indicator.assert_any_call( 'service-2', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call( 'service-2', 'indicator-2', 'Indicator 2') self.assertEqual(4, mock_update_category.call_count) mock_update_category.assert_any_call( 'service-2', 'indicator-1', 'category-1.1', 'Category 1.1', None, None, 211) mock_update_category.assert_any_call( 'service-2', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 212) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_indicator_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry['service-2']['indicator-2']) self.assertEqual(1, mock_update_indicator.call_count) mock_update_indicator.assert_any_call( 'service-2', 'indicator-2', 'Indicator 2') self.assertEqual(2, mock_update_category.call_count) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_category_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report( self.registry['service-2']['indicator-2']['category-2.2']) self.assertEqual(0, mock_update_indicator.call_count) self.assertEqual(1, mock_update_category.call_count) mock_update_category.assert_any_call( 'service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_category_should_raise_reporter_exception_if_client_exception(self): with patch( 'genweb.core.indicators.client.Client.update_category', side_effect=ClientException('Something wrong with WS')): with self.assertRaises(ReporterException) as context: self.reporter.report( self.registry['service-2']['indicator-2']['category-2.2']) self.assertEqual( 'WS client exception (Something wrong with WS)', context.exception.message)
class TestRegistry(unittest.TestCase): def setUp(self): self.reporter = WebServiceReporter("url", "api_key") self.registry = Registry("context") self.registry.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.3-indicators')) self.registry_with_exception = Registry("context") self.registry_with_exception.load_from_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', '1-service.1-indicator.1-category_exception')) def test_report_registry_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch('genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry) self.assertEqual(3, mock_update_indicator.call_count) mock_update_indicator.assert_any_call('service-1', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call('service-2', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call('service-2', 'indicator-2', 'Indicator 2') self.assertEqual(6, mock_update_category.call_count) mock_update_category.assert_any_call('service-1', 'indicator-1', 'category-1.1', 'Category 1.1', 'type 1.1', 'frequency 1.1', 111) mock_update_category.assert_any_call('service-1', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 112) mock_update_category.assert_any_call('service-2', 'indicator-1', 'category-1.1', 'Category 1.1', None, None, 211) mock_update_category.assert_any_call('service-2', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 212) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_registry_should_raise_reporter_exception_if_client_exception( self): with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=ClientException('Something wrong with WS')): with patch('genweb.core.indicators.client.Client.update_category', side_effect=ClientException('Something wrong with WS')): with self.assertRaises(ReporterException) as context: self.reporter.report(self.registry) self.assertEqual('WS client exception (Something wrong with WS)', context.exception.message) def test_report_registry_should_raise_reporter_exception_if_calculator_exception( self): with patch('genweb.core.indicators.client.Client.update_indicator'): with patch('genweb.core.indicators.client.Client.update_category'): with self.assertRaises(ReporterException) as context: self.reporter.report(self.registry_with_exception) self.assertEqual('Error when calculating category (Oh!)', context.exception.message) def test_report_dict_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch('genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry['service-2']) self.assertEqual(2, mock_update_indicator.call_count) mock_update_indicator.assert_any_call('service-2', 'indicator-1', 'Indicator 1') mock_update_indicator.assert_any_call('service-2', 'indicator-2', 'Indicator 2') self.assertEqual(4, mock_update_category.call_count) mock_update_category.assert_any_call('service-2', 'indicator-1', 'category-1.1', 'Category 1.1', None, None, 211) mock_update_category.assert_any_call('service-2', 'indicator-1', 'category-1.2', 'Category 1.2', None, None, 212) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_indicator_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch('genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report(self.registry['service-2']['indicator-2']) self.assertEqual(1, mock_update_indicator.call_count) mock_update_indicator.assert_any_call('service-2', 'indicator-2', 'Indicator 2') self.assertEqual(2, mock_update_category.call_count) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.1', 'Category 2.1', None, None, 221) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_category_should_update_indicators_and_categories(self): mock_update_indicator = Mock() mock_update_category = Mock() with patch('genweb.core.indicators.client.Client.update_indicator', side_effect=mock_update_indicator): with patch('genweb.core.indicators.client.Client.update_category', side_effect=mock_update_category): self.reporter.report( self.registry['service-2']['indicator-2']['category-2.2']) self.assertEqual(0, mock_update_indicator.call_count) self.assertEqual(1, mock_update_category.call_count) mock_update_category.assert_any_call('service-2', 'indicator-2', 'category-2.2', 'Category 2.2', None, None, 222) def test_report_category_should_raise_reporter_exception_if_client_exception( self): with patch('genweb.core.indicators.client.Client.update_category', side_effect=ClientException('Something wrong with WS')): with self.assertRaises(ReporterException) as context: self.reporter.report( self.registry['service-2']['indicator-2']['category-2.2']) self.assertEqual('WS client exception (Something wrong with WS)', context.exception.message)
def test_load_from_non_empty_path_should_add_indicators(self): registry = Registry("context") registry.load_from_path( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'indicators', '2-services.3-indicators')) self.assertEqual(2, len(registry._indicators)) self.assertEqual(1, len(registry._indicators['service-1'])) self.assertEqual(2, len(registry._indicators['service-2'])) self.assertEqual('indicator-1', registry['service-1']['indicator-1'].id) self.assertEqual( 'Indicator 1', registry['service-1']['indicator-1'].description) self.assertEqual( 'category-1.1', registry['service-1']['indicator-1']['category-1.1'].id) self.assertEqual( 'Category 1.1', registry['service-1']['indicator-1']['category-1.1'].description) self.assertEqual( 'type 1.1', registry['service-1']['indicator-1']['category-1.1'].type) self.assertEqual( 'frequency 1.1', registry['service-1']['indicator-1']['category-1.1'].frequency) self.assertEqual( 111, registry['service-1']['indicator-1']['category-1.1'].value) self.assertEqual( 'category-1.2', registry['service-1']['indicator-1']['category-1.2'].id) self.assertEqual( 'Category 1.2', registry['service-1']['indicator-1']['category-1.2'].description) self.assertEqual( None, registry['service-1']['indicator-1']['category-1.2'].type) self.assertEqual( None, registry['service-1']['indicator-1']['category-1.2'].frequency) self.assertEqual( 112, registry['service-1']['indicator-1']['category-1.2'].value) self.assertEqual('indicator-1', registry['service-2']['indicator-1'].id) self.assertEqual( 'Indicator 1', registry['service-2']['indicator-1'].description) self.assertEqual( 'category-1.1', registry['service-2']['indicator-1']['category-1.1'].id) self.assertEqual( 'Category 1.1', registry['service-2']['indicator-1']['category-1.1'].description) self.assertEqual( 211, registry['service-2']['indicator-1']['category-1.1'].value) self.assertEqual( 'category-1.2', registry['service-2']['indicator-1']['category-1.2'].id) self.assertEqual( 'Category 1.2', registry['service-2']['indicator-1']['category-1.2'].description) self.assertEqual( 212, registry['service-2']['indicator-1']['category-1.2'].value) self.assertEqual('indicator-2', registry['service-2']['indicator-2'].id) self.assertEqual( 'Indicator 2', registry['service-2']['indicator-2'].description) self.assertEqual( 'category-2.1', registry['service-2']['indicator-2']['category-2.1'].id) self.assertEqual( 'Category 2.1', registry['service-2']['indicator-2']['category-2.1'].description) self.assertEqual( 221, registry['service-2']['indicator-2']['category-2.1'].value) self.assertEqual( 'category-2.2', registry['service-2']['indicator-2']['category-2.2'].id) self.assertEqual( 'Category 2.2', registry['service-2']['indicator-2']['category-2.2'].description) self.assertEqual( 222, registry['service-2']['indicator-2']['category-2.2'].value)
def test_getitem_should_return_indicators_dict_getitem(self): registry = Registry('context') registry._indicators['service1'] = "s1" self.assertEqual( registry._indicators['service1'], registry['service1'])
def test_values_should_return_indicators_dict_values(self): registry = Registry('context') registry._indicators['service1'] = "s1" registry._indicators['service2'] = "s2" self.assertEqual( registry.values(), registry._indicators.values())
def test_getitem_should_return_indicators_dict_getitem(self): registry = Registry('context') registry._indicators['service1'] = "s1" self.assertEqual(registry._indicators['service1'], registry['service1'])
def test_load_from_file_path_should_raise_error_if_invalid_file(self): registry = Registry('context') with self.assertRaises(ScannerError) as context: registry._load_from_file_path( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'indicators', 'definitions', 'invalid.yaml'))
def test_values_should_return_indicators_dict_values(self): registry = Registry('context') registry._indicators['service1'] = "s1" registry._indicators['service2'] = "s2" self.assertEqual(registry.values(), registry._indicators.values())