Skip to content

Commit 98ede91

Browse files
authored
Merge pull request #58 from BirkbeckCTP/56-currency-region
Make sure data is available for finding exchange rates
2 parents 0228497 + e515479 commit 98ede91

File tree

6 files changed

+103
-33
lines changed

6 files changed

+103
-33
lines changed

logic.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ def get_base_band(level=None):
149149
return get_base_band()
150150
else:
151151
try:
152-
return models.Band.objects.filter(
153-
base=True
154-
).latest()
152+
default_level = utils.get_standard_support_level()
153+
if default_level:
154+
return models.Band.objects.filter(
155+
base=True,
156+
level=default_level,
157+
).latest()
158+
else:
159+
return models.Band.objects.filter(
160+
base=True,
161+
).latest()
162+
155163
except models.Band.DoesNotExist:
156-
logger.warning('No base band found')
164+
logger.warning('No default base band found.')
157165

158166

159167
def latest_multiplier_for_indicator(
@@ -257,10 +265,6 @@ def keep_default_unique(obj):
257265
:obj: an unsaved model instance with a property named 'default'
258266
"""
259267
if obj.default:
260-
try:
261-
other = type(obj).objects.get(default=True)
262-
if obj != other:
263-
other.default = False
264-
other.save()
265-
except type(obj).DoesNotExist:
266-
pass
268+
type(obj).objects.filter(default=True).exclude(pk=obj.pk).update(
269+
default=False,
270+
)

models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def exchange_rate(self, base_band=None) -> Tuple[decimal.Decimal, str]:
198198
base_band = logic.get_base_band()
199199

200200
if base_band:
201-
base_key = base_band.country.alpha3
201+
base_key = base_band.currency.region
202202
else:
203203
# This is needed during initial configuration
204204
base_key = '---'

tests/test_logic.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,53 @@ def test_get_indicator_by_country(self, open_saved):
4848
data = logic.get_indicator_by_country(self.fake_indicator, 2050)
4949
self.assertEqual(data['NLD'], 12345)
5050

51-
def test_get_base_band(self):
52-
51+
def test_get_base_band_base_level(self):
5352
base_band = logic.get_base_band(self.level_base)
5453
self.assertEqual(
5554
self.band_base,
5655
base_band,
5756
)
57+
58+
def test_get_base_band_other_level(self):
5859
other_base_band = logic.get_base_band(self.level_other)
5960
self.assertEqual(
6061
self.band_base_level_other,
6162
other_base_band,
6263
)
6364

65+
def test_get_base_band_no_args(self):
66+
base_band = logic.get_base_band()
67+
self.assertEqual(
68+
self.band_base,
69+
base_band,
70+
)
71+
72+
def test_get_base_band_no_args_no_default_level(self):
73+
74+
# Make it so there is no default level
75+
self.level_base.default = False
76+
self.level_base.save()
77+
78+
base_band = logic.get_base_band()
79+
80+
# Restore data
81+
self.level_base.default = True
82+
self.level_base.save()
83+
84+
self.assertEqual(
85+
self.band_base,
86+
base_band,
87+
)
88+
6489
def test_get_base_bands(self):
6590

6691
base_bands = logic.get_base_bands()
6792
self.assertListEqual(
68-
[self.band_base_level_other, self.band_base],
93+
[
94+
self.band_base_country_other,
95+
self.band_base_level_other,
96+
self.band_base
97+
],
6998
base_bands,
7099
)
71100

tests/test_models.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.http import HttpRequest
1212

1313
from plugins.consortial_billing import models, plugin_settings
14+
from plugins.consortial_billing import utils
1415
from utils.testing import helpers
1516
from press import models as press_models
1617
from cms.models import Page
@@ -72,6 +73,10 @@ def setUpClass(cls):
7273
name='Higher',
7374
order=1,
7475
)
76+
cls.level_third = models.SupportLevel.objects.create(
77+
name='Even Higher',
78+
order=0,
79+
)
7580
cls.currency_base = models.Currency.objects.create(
7681
code='GBP',
7782
region='GBR',
@@ -100,6 +105,15 @@ def setUpClass(cls):
100105
billing_agent=cls.agent_default,
101106
base=True,
102107
)
108+
cls.band_base_country_other = models.Band.objects.create(
109+
size=cls.size_other,
110+
country='DE',
111+
currency=cls.currency_other,
112+
level=cls.level_third,
113+
fee=5000,
114+
billing_agent=cls.agent_default,
115+
base=True,
116+
)
103117
cls.band_other_one = models.Band.objects.create(
104118
size=cls.size_base,
105119
country='GB',
@@ -192,9 +206,12 @@ def tearDownClass(cls):
192206
cls.supporter_one.delete()
193207
cls.band_other_two.delete()
194208
cls.band_other_one.delete()
209+
cls.band_base_country_other.delete()
210+
cls.band_base_level_other.delete()
195211
cls.band_base.delete()
196212
cls.currency_other.delete()
197213
cls.currency_base.delete()
214+
cls.level_third.delete()
198215
cls.level_other.delete()
199216
cls.level_base.delete()
200217
cls.size_other.delete()
@@ -242,23 +259,33 @@ def test_billing_agent_save(self):
242259
self.assertFalse(other_has_country)
243260
self.assertFalse(default_still_default)
244261

245-
def test_currency_exchange_rate(self):
246-
with patch(
247-
'plugins.consortial_billing.logic.latest_multiplier_for_indicator'
248-
) as latest_multiplier:
249-
self.currency_other.exchange_rate()
250-
self.assertIn(
251-
plugin_settings.RATE_INDICATOR,
252-
latest_multiplier.call_args.args,
253-
)
254-
self.assertIn(
255-
self.currency_other.region,
256-
latest_multiplier.call_args.args,
257-
)
258-
self.assertIn(
259-
self.currency_other.region,
260-
latest_multiplier.call_args.args,
261-
)
262+
@patch('plugins.consortial_billing.logic.latest_multiplier_for_indicator')
263+
def test_currency_exchange_rate_with_typical_args(self, latest_multiplier):
264+
self.currency_base.exchange_rate(base_band=self.band_base_country_other)
265+
expected_args = (
266+
plugin_settings.RATE_INDICATOR,
267+
self.currency_base.region, # Target currency
268+
self.currency_other.region, # Specified base currency
269+
utils.setting('missing_data_exchange_rate')
270+
)
271+
self.assertTupleEqual(
272+
expected_args,
273+
latest_multiplier.call_args.args,
274+
)
275+
276+
@patch('plugins.consortial_billing.logic.latest_multiplier_for_indicator')
277+
def test_currency_exchange_rate_with_no_args(self, latest_multiplier):
278+
self.currency_other.exchange_rate()
279+
expected_args = (
280+
plugin_settings.RATE_INDICATOR,
281+
self.currency_other.region, # Target currency
282+
self.currency_base.region, # Default base currency
283+
utils.setting('missing_data_exchange_rate')
284+
)
285+
self.assertTupleEqual(
286+
expected_args,
287+
latest_multiplier.call_args.args,
288+
)
262289

263290
def test_band_economic_disparity(self):
264291
with patch(

tests/test_views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ def test_manager_context_essential(self, latest_multiplier):
5252
response.context['plugin'],
5353
)
5454
self.assertListEqual(
55-
[self.band_base_level_other, self.band_base],
55+
[
56+
self.band_base_country_other,
57+
self.band_base_level_other,
58+
self.band_base
59+
],
5660
response.context['base_bands'],
5761
)
5862
self.assertEqual(

utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def get_standard_support_level():
170170
try:
171171
return models.SupportLevel.objects.get(default=True)
172172
except models.SupportLevel.DoesNotExist:
173+
logger.error(
174+
'No default support level found. '
175+
'Using the support level ordered last, '
176+
'but that may produce unintended results. '
177+
'For best results, set a level as the default.'
178+
)
173179
return models.SupportLevel.objects.all().last()
174180

175181

0 commit comments

Comments
 (0)