Exemplo n.º 1
0
def update(context, service, indicator):
    ws_url = serveistic_config().ws_indicadors_endpoint
    ws_key = serveistic_config().ws_indicadors_key
    registry = get_registry(context)

    reporter = WebServiceReporter(ws_url, ws_key)
    reporter.report(get_data_to_report(registry, service, indicator))
Exemplo n.º 2
0
def update(context, service, indicator):
    ws_url = serveistic_config().ws_indicadors_endpoint
    ws_key = serveistic_config().ws_indicadors_key
    registry = get_registry(context)

    reporter = WebServiceReporter(ws_url, ws_key)
    reporter.report(get_data_to_report(registry, service, indicator))
Exemplo n.º 3
0
 def test_report_category_should_update_category(self):
     reporter = WebServiceReporter("url", "api_key")
     mock_update_category = Mock()
     with patch('genweb.core.indicators.client.Client.update_category',
                side_effect=mock_update_category):
         reporter._report_category(Mock())
     self.assertEqual(1, mock_update_category.call_count)
Exemplo n.º 4
0
 def test_report_indicator_should_raise_reporter_exception_if_reporter_exception(self):
     reporter = WebServiceReporter("url", "api_key")
     with patch('genweb.core.indicators.client.Client.update_indicator',
                side_effect=(None,)):
         with patch('genweb.core.indicators.reporter.WebServiceReporter._report_indicator_categories',
                    side_effect=ReporterException):
             with self.assertRaises(ReporterException):
                 reporter._report_indicator(Mock(), report_categories=True)
Exemplo n.º 5
0
 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'))
Exemplo n.º 6
0
 def test_report_indicator_should_not_update_categories_if_specified(self):
     reporter = WebServiceReporter("url", "api_key")
     mock_update_indicator = Mock()
     mock_report_categories = Mock()
     with patch('genweb.core.indicators.client.Client.update_indicator',
                side_effect=mock_update_indicator):
         with patch('genweb.core.indicators.reporter.WebServiceReporter._report_indicator_categories',
                    side_effect=mock_report_categories):
             reporter._report_indicator(Mock(), report_categories=False)
     self.assertEqual(1, mock_update_indicator.call_count)
     self.assertEqual(0, mock_report_categories.call_count)
Exemplo n.º 7
0
 def test_report_indicator_categories_should_report_categories(self):
     reporter = WebServiceReporter("url", "api_key")
     mock_report_category = Mock()
     mock_categories = {'one': 1, 'two': 2, 'three': 3}
     with patch('genweb.core.indicators.reporter.WebServiceReporter._report_category',
                side_effect=mock_report_category):
         reporter._report_indicator_categories(
             Mock(categories=mock_categories))
     self.assertEqual(3, mock_report_category.call_count)
     mock_report_category.assert_any_call(1)
     mock_report_category.assert_any_call(2)
     mock_report_category.assert_any_call(3)
Exemplo n.º 8
0
 def test_report_indicator_should_raise_reporter_exception_if_calculator_exception(self):
     reporter = WebServiceReporter("url", "api_key")
     with patch('genweb.core.indicators.client.Client.update_indicator'):
         with patch('genweb.core.indicators.client.Client.update_category',
                    side_effect=CalculatorException('Oh!')):
             with self.assertRaises(ReporterException) as context:
                 reporter._report_indicator(
                     Mock(categories={'c1': Mock(), '2': Mock()}),
                     report_categories=True)
             self.assertEqual(
                 "Error when calculating category (Oh!)",
                 context.exception.message)
Exemplo n.º 9
0
 def test_report_should_raise_error_if_invalid_type(self):
     reporter = WebServiceReporter("url", "api_key")
     with self.assertRaises(TypeError):
         reporter.report(None)
     with self.assertRaises(TypeError):
         reporter.report([])
     with self.assertRaises(TypeError):
         reporter.report(1)
Exemplo n.º 10
0
def update_after_commit_hook(is_commit_successful, context):
    if not is_commit_successful:
        return
    try:
        ws_url = get_settings_property('ws_endpoint')
        ws_key = get_settings_property('ws_key')
        registry = get_registry(context)

        reporter = WebServiceReporter(ws_url, ws_key)
        reporter.report(registry)
        logger.info("Indicators were successfully reported")
    except RegistryException as e:
        logger.warning(
            "Error while loading indicator registry ({0})".format(e))
    except ReporterException as e:
        logger.warning("Error while reporting indicators ({0})".format(e))
Exemplo n.º 11
0
 def test_report_indicator_dict_should_report_indicators(self):
     reporter = WebServiceReporter("url", "api_key")
     mock_report_indicator = Mock()
     with patch('genweb.core.indicators.reporter.WebServiceReporter._report_indicator',
                side_effect=mock_report_indicator):
         reporter._report_indicator_dict(
             {'indicator-1': 1,
              'indicator-2': 2,
              'indicator-3': 3,
              'indicator-4': 4,
              })
     self.assertEqual(4, mock_report_indicator.call_count)
     mock_report_indicator.assert_any_call(1)
     mock_report_indicator.assert_any_call(2)
     mock_report_indicator.assert_any_call(3)
     mock_report_indicator.assert_any_call(4)
Exemplo n.º 12
0
def update_after_commit_hook(is_commit_successful, context, service,
                             indicator):
    if not is_commit_successful:
        return
    try:
        ws_url = get_settings_property('ws_endpoint')
        ws_key = get_settings_property('ws_key')
        registry = get_registry(context)

        reporter = WebServiceReporter(ws_url, ws_key)
        reporter.report(get_data_to_report(registry, service, indicator))
        logger.info("Indicators were successfully reported")
    except RegistryException as e:
        logger.warning(
            "Error while loading indicator registry ({0})".format(e))
    except ReporterException as e:
        logger.warning("Error while reporting indicators ({0})".format(e))
Exemplo n.º 13
0
 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'))
Exemplo n.º 14
0
 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'))
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
 def test_report_category_should_raise_reporter_exception_if_client_exception(self):
     reporter = WebServiceReporter("url", "api_key")
     with patch('genweb.core.indicators.client.Client.update_category',
                side_effect=ClientException):
         with self.assertRaises(ReporterException):
             reporter._report_category(Mock())