diff --git a/logic.py b/logic.py index 0366eb9..85821d2 100644 --- a/logic.py +++ b/logic.py @@ -109,10 +109,15 @@ def get_indicator_by_country( indicator: str, year: int ) -> dict[str, decimal.Decimal]: - _metadata, data = utils.open_saved_world_bank_data(indicator, year) + data = utils.open_saved_world_bank_data(indicator, year) + # The next few lines are written defensively because of the variation + # in the JSON received from the World Bank API. if not data: return {} - return {each['countryiso3code']: each['value'] for each in data} + _metadata, country_records = data + if not country_records: + return {} + return {each['countryiso3code']: each['value'] for each in country_records} def get_base_bands(): @@ -186,7 +191,11 @@ def latest_multiplier_for_indicator( return multiplier, warning if base_improperly_configured == 5: - raise ImproperlyConfigured(f'{base_key} not found in {indicator} data') + logger.error( + f'{base_key} not found in the data for indicator {indicator}. ' + f'World Bank data may be missing or the base band ' + f'may not be properly configured.' + ) return multiplier, warning @@ -236,7 +245,7 @@ def determine_billing_agent(country): agent = models.BillingAgent.objects.get(default=True) return agent except models.BillingAgent.DoesNotExist: - raise ImproperlyConfigured( + logger.error( 'No billing agent has been set as default' ) diff --git a/models.py b/models.py index d877199..3cd3e84 100755 --- a/models.py +++ b/models.py @@ -352,7 +352,7 @@ def calculate_fee(self) -> Tuple[int, str]: fee = logic.get_base_band(self.level).fee if fee is None: - raise ImproperlyConfigured( + logger.error( 'No fee has been set on base band' ) diff --git a/tests/test_logic.py b/tests/test_logic.py index fddbdea..33f15c6 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -23,26 +23,30 @@ def test_get_display_bands(self): '1000–1001', ) - def test_get_indicator_by_country(self): - with patch( - 'plugins.consortial_billing.utils.open_saved_world_bank_data', - ) as open_saved: - open_saved.return_value = [{}, []] - data = logic.get_indicator_by_country(self.fake_indicator, 2050) - open_saved.assert_called() - self.assertEqual(data, {}) - - open_saved.return_value = [ - {}, - [ - { - 'countryiso3code': 'NLD', - 'value': 12345, - } - ] + @patch('plugins.consortial_billing.utils.open_saved_world_bank_data') + def test_get_indicator_by_country(self, open_saved): + + open_saved.return_value = [] + data = logic.get_indicator_by_country(self.fake_indicator, 2050) + open_saved.assert_called() + self.assertEqual(data, {}) + + open_saved.return_value = ['something', None] + data = logic.get_indicator_by_country(self.fake_indicator, 2050) + open_saved.assert_called() + self.assertEqual(data, {}) + + open_saved.return_value = [ + {}, + [ + { + 'countryiso3code': 'NLD', + 'value': 12345, + } ] - data = logic.get_indicator_by_country(self.fake_indicator, 2050) - self.assertEqual(data['NLD'], 12345) + ] + data = logic.get_indicator_by_country(self.fake_indicator, 2050) + self.assertEqual(data['NLD'], 12345) def test_get_base_band(self): @@ -125,13 +129,14 @@ def test_latest_multiplier_for_indicator(self): self.assertEqual(warning, '') get_indicator.return_value = {'dog': 5} - with self.assertRaises(ImproperlyConfigured): - multiplier, warning = logic.latest_multiplier_for_indicator( - self.fake_indicator, - measure_key, - base_key, - warning_text, - ) + multiplier, warning = logic.latest_multiplier_for_indicator( + self.fake_indicator, + measure_key, + base_key, + warning_text, + ) + self.assertEqual(multiplier, 1) + self.assertEqual(warning, warning_text) get_indicator.return_value = {'cat': 10} multiplier, warning = logic.latest_multiplier_for_indicator(