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 sure data is available for finding exchange rates #58

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 12 additions & 4 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,19 @@ def get_base_band(level=None):
return get_base_band()
else:
try:
return models.Band.objects.filter(
base=True
).latest()
default_level = utils.get_standard_support_level()
if default_level:
return models.Band.objects.filter(
base=True,
level=default_level,
).latest()
else:
return models.Band.objects.filter(
base=True,
).latest()

except models.Band.DoesNotExist:
logger.warning('No base band found')
logger.warning('No default base band found.')


def latest_multiplier_for_indicator(
Expand Down
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def exchange_rate(self, base_band=None) -> Tuple[decimal.Decimal, str]:
base_band = logic.get_base_band()

if base_band:
base_key = base_band.country.alpha3
base_key = base_band.currency.region
else:
# This is needed during initial configuration
base_key = '---'
Expand Down
35 changes: 32 additions & 3 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,53 @@ def test_get_indicator_by_country(self):
data = logic.get_indicator_by_country(self.fake_indicator, 2050)
self.assertEqual(data['NLD'], 12345)

def test_get_base_band(self):

def test_get_base_band_base_level(self):
base_band = logic.get_base_band(self.level_base)
self.assertEqual(
self.band_base,
base_band,
)

def test_get_base_band_other_level(self):
other_base_band = logic.get_base_band(self.level_other)
self.assertEqual(
self.band_base_level_other,
other_base_band,
)

def test_get_base_band_no_args(self):
base_band = logic.get_base_band()
self.assertEqual(
self.band_base,
base_band,
)

def test_get_base_band_no_args_no_default_level(self):

# Make it so there is no default level
self.level_base.default = False
self.level_base.save()

base_band = logic.get_base_band()

# Restore data
self.level_base.default = True
self.level_base.save()

self.assertEqual(
self.band_base,
base_band,
)

def test_get_base_bands(self):

base_bands = logic.get_base_bands()
self.assertListEqual(
[self.band_base_level_other, self.band_base],
[
self.band_base_country_other,
self.band_base_level_other,
self.band_base
],
base_bands,
)

Expand Down
61 changes: 44 additions & 17 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.http import HttpRequest

from plugins.consortial_billing import models, plugin_settings
from plugins.consortial_billing import utils
from utils.testing import helpers
from press import models as press_models
from cms.models import Page
Expand Down Expand Up @@ -66,6 +67,10 @@ def setUpClass(cls):
name='Higher',
order=1,
)
cls.level_third = models.SupportLevel.objects.create(
name='Even Higher',
order=0,
)
cls.currency_base = models.Currency.objects.create(
code='GBP',
region='GBR',
Expand Down Expand Up @@ -94,6 +99,15 @@ def setUpClass(cls):
billing_agent=cls.agent_default,
base=True,
)
cls.band_base_country_other = models.Band.objects.create(
size=cls.size_other,
country='DE',
currency=cls.currency_other,
level=cls.level_third,
fee=5000,
billing_agent=cls.agent_default,
base=True,
)
cls.band_other_one = models.Band.objects.create(
size=cls.size_base,
country='GB',
Expand Down Expand Up @@ -174,9 +188,12 @@ def tearDownClass(cls):
cls.supporter_one.delete()
cls.band_other_two.delete()
cls.band_other_one.delete()
cls.band_base_country_other.delete()
cls.band_base_level_other.delete()
cls.band_base.delete()
cls.currency_other.delete()
cls.currency_base.delete()
cls.level_third.delete()
cls.level_other.delete()
cls.level_base.delete()
cls.size_other.delete()
Expand Down Expand Up @@ -224,23 +241,33 @@ def test_billing_agent_save(self):
self.assertFalse(other_has_country)
self.assertFalse(default_still_default)

def test_currency_exchange_rate(self):
with patch(
'plugins.consortial_billing.logic.latest_multiplier_for_indicator'
) as latest_multiplier:
self.currency_other.exchange_rate()
self.assertIn(
plugin_settings.RATE_INDICATOR,
latest_multiplier.call_args.args,
)
self.assertIn(
self.currency_other.region,
latest_multiplier.call_args.args,
)
self.assertIn(
self.currency_other.region,
latest_multiplier.call_args.args,
)
@patch('plugins.consortial_billing.logic.latest_multiplier_for_indicator')
def test_currency_exchange_rate_with_typical_args(self, latest_multiplier):
self.currency_base.exchange_rate(base_band=self.band_base_country_other)
expected_args = (
plugin_settings.RATE_INDICATOR,
self.currency_base.region, # Target currency
self.currency_other.region, # Specified base currency
utils.setting('missing_data_exchange_rate')
)
self.assertTupleEqual(
expected_args,
latest_multiplier.call_args.args,
)

@patch('plugins.consortial_billing.logic.latest_multiplier_for_indicator')
def test_currency_exchange_rate_with_no_args(self, latest_multiplier):
self.currency_other.exchange_rate()
expected_args = (
plugin_settings.RATE_INDICATOR,
self.currency_other.region, # Target currency
self.currency_base.region, # Default base currency
utils.setting('missing_data_exchange_rate')
)
self.assertTupleEqual(
expected_args,
latest_multiplier.call_args.args,
)

def test_band_economic_disparity(self):
with patch(
Expand Down
6 changes: 5 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def test_manager_context_essential(self, latest_multiplier):
response.context['plugin'],
)
self.assertListEqual(
[self.band_base_level_other, self.band_base],
[
self.band_base_country_other,
self.band_base_level_other,
self.band_base
],
response.context['base_bands'],
)
self.assertEqual(
Expand Down
6 changes: 6 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ def get_standard_support_level():
try:
return models.SupportLevel.objects.get(default=True)
except models.SupportLevel.DoesNotExist:
logger.error(
'No default support level found. '
'Using the support level ordered last, '
'but that may produce unintended results. '
'For best results, set a level as the default.'
)
return models.SupportLevel.objects.all().last()


Expand Down