Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make data retrieval functions a bit more resilient #59

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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'
)

Expand Down
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
57 changes: 31 additions & 26 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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(
Expand Down