Skip to content

Commit

Permalink
Merge pull request #689 from nschlemm/patch-1
Browse files Browse the repository at this point in the history
rate is always 1 if source and target are equal
  • Loading branch information
benjaoming committed Oct 3, 2022
2 parents 94f2795 + 1da5225 commit fe9b71a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions djmoney/contrib/exchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def get_rate(source, target, backend=None):
Converts exchange rate on the DB side if there is no backends with given base currency.
Uses data from the default backend if the backend is not specified.
"""
if str(source) == str(target):
return 1
if backend is None:
backend = get_default_backend_name()
key = f"djmoney:get_rate:{source}:{target}:{backend}"
Expand All @@ -53,8 +55,6 @@ def get_rate(source, target, backend=None):

def _get_rate(source, target, backend):
source, target = str(source), str(target)
if str(source) == target:
return 1
rates = Rate.objects.filter(currency__in=(source, target), backend=backend).select_related("backend")
if not rates:
raise MissingRate(f"Rate {source} -> {target} does not exist")
Expand Down
4 changes: 3 additions & 1 deletion tests/contrib/exchange/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ def test_string_representation(backend):
def test_cache():
with patch("djmoney.contrib.exchange.models._get_rate", wraps=_get_rate) as original:
assert get_rate("USD", "USD") == 1
assert original.call_count == 0
assert get_rate("USD", "EUR") == 2
assert original.call_count == 1
assert get_rate("USD", "USD") == 1
assert get_rate("USD", "EUR") == 2
assert original.call_count == 1


Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_django_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_post_put_values(self, body, field_kwargs, expected):

def test_serializer_with_fields(self):
serializer = self.get_serializer(ModelWithVanillaMoneyField, data={"money": "10.00"}, fields_=("money",))
serializer.is_valid(True)
serializer.is_valid(raise_exception=True)
assert serializer.data == {"money": "10.00"}

@pytest.mark.parametrize(
Expand Down

0 comments on commit fe9b71a

Please sign in to comment.