Skip to content

Commit

Permalink
Migrated utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Feb 12, 2017
1 parent 4a73df2 commit ca904a7
Showing 1 changed file with 73 additions and 70 deletions.
143 changes: 73 additions & 70 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import unicode_literals

import unittest

from decimal import Decimal

import pytest

from djmoney_rates.backends import BaseRateBackend
from djmoney_rates.exceptions import CurrencyConversionException
from djmoney_rates.models import RateSource, Rate
Expand All @@ -12,96 +12,99 @@

import moneyed

@pytest.fixture
def set_up():
class RateBackend(BaseRateBackend):
source_name = "fake-backend"
base_currency = "USD"

class TestMoneyConverter(unittest.TestCase):
def setUp(self):
class RateBackend(BaseRateBackend):
source_name = "fake-backend"
base_currency = "USD"

money_rates_settings.DEFAULT_BACKEND = RateBackend

def tearDown(self):
RateSource.objects.all().delete()
Rate.objects.all().delete()

def test_base_conversion_fail_when_source_does_not_exist(self):
with self.assertRaises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")
money_rates_settings.DEFAULT_BACKEND = RateBackend

self.assertIn("Rate for fake-backend source do not exists", str(cm.exception))
@pytest.mark.django_db(transaction=True)
def test_base_conversion_fail_when_source_does_not_exist(set_up):
with pytest.raises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")

def test_base_conversion_fail_when_currency_from_does_not_exist(self):
RateSource.objects.create(name="fake-backend")
assert "Rate for fake-backend source do not exists" in str(cm.value)

with self.assertRaises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")
@pytest.mark.django_db(transaction=True)
def test_base_conversion_fail_when_currency_from_does_not_exist(set_up):
RateSource.objects.create(name="fake-backend")

self.assertIn("Rate for PLN in fake-backend do not exists", str(cm.exception))
with pytest.raises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")

def test_base_conversion_fail_when_currency_to_does_not_exist(self):
source = RateSource.objects.create(name="fake-backend")
Rate.objects.create(source=source, currency="PLN", value=0.99999)
assert "Rate for PLN in fake-backend do not exists" in str(cm.value)

with self.assertRaises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")
@pytest.mark.django_db(transaction=True)
def test_base_conversion_fail_when_currency_to_does_not_exist(set_up):
source = RateSource.objects.create(name="fake-backend")
Rate.objects.create(source=source, currency="PLN", value=0.99999)

self.assertIn("Rate for EUR in fake-backend do not exists", str(cm.exception))
with pytest.raises(CurrencyConversionException) as cm:
base_convert_money(10.0, "PLN", "EUR")

def test_base_conversion_works_from_base_currency(self):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="USD", value=1)
Rate.objects.create(source=source, currency="EUR", value=0.74)
assert "Rate for EUR in fake-backend do not exists" in str(cm.value)

amount = base_convert_money(1, "USD", "EUR")
self.assertEqual(amount, Decimal("0.74"))
@pytest.mark.django_db(transaction=True)
def test_base_conversion_works_from_base_currency(set_up):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="USD", value=1)
Rate.objects.create(source=source, currency="EUR", value=0.74)

def test_base_conversion_is_working_from_other_currency(self):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="PLN", value=3.07)
Rate.objects.create(source=source, currency="EUR", value=0.74)
amount = base_convert_money(1, "USD", "EUR")
assert amount == Decimal("0.74")

amount = base_convert_money(10.0, "PLN", "EUR")
self.assertEqual(amount, Decimal("2.41"))
@pytest.mark.django_db(transaction=True)
def test_base_conversion_is_working_from_other_currency(set_up):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="PLN", value=3.07)
Rate.objects.create(source=source, currency="EUR", value=0.74)

def test_conversion_fail_when_source_does_not_exist(self):
with self.assertRaises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")
amount = base_convert_money(10.0, "PLN", "EUR")
assert amount == Decimal("2.41")

self.assertIn("Rate for fake-backend source do not exists", str(cm.exception))
@pytest.mark.django_db(transaction=True)
def test_conversion_fail_when_source_does_not_exist(set_up):
with pytest.raises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")

def test_conversion_fail_when_currency_from_does_not_exist(self):
RateSource.objects.create(name="fake-backend")
assert "Rate for fake-backend source do not exists" in str(cm.value)

with self.assertRaises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")
@pytest.mark.django_db(transaction=True)
def test_conversion_fail_when_currency_from_does_not_exist(set_up):
RateSource.objects.create(name="fake-backend")

self.assertIn("Rate for PLN in fake-backend do not exists", str(cm.exception))
with pytest.raises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")

def test_conversion_fail_when_currency_to_does_not_exist(self):
source = RateSource.objects.create(name="fake-backend")
Rate.objects.create(source=source, currency="PLN", value=0.99999)
assert "Rate for PLN in fake-backend do not exists" in str(cm.value)

with self.assertRaises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")
@pytest.mark.django_db(transaction=True)
def test_conversion_fail_when_currency_to_does_not_exist(set_up):
source = RateSource.objects.create(name="fake-backend")
Rate.objects.create(source=source, currency="PLN", value=0.99999)

self.assertIn("Rate for EUR in fake-backend do not exists", str(cm.exception))
with pytest.raises(CurrencyConversionException) as cm:
convert_money(10.0, "PLN", "EUR")

def test_conversion_works_from_base_currency(self):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="USD", value=1)
Rate.objects.create(source=source, currency="EUR", value=0.74)
assert "Rate for EUR in fake-backend do not exists" in str(cm.value)

amount = convert_money(1, "USD", "EUR")
self.assertEqual(type(amount), moneyed.Money)
self.assertEqual(amount, moneyed.Money(Decimal("0.74"), "EUR"))
@pytest.mark.django_db(transaction=True)
def test_conversion_works_from_base_currency(set_up):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="USD", value=1)
Rate.objects.create(source=source, currency="EUR", value=0.74)

def test_conversion_is_working_from_other_currency(self):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="PLN", value=3.07)
Rate.objects.create(source=source, currency="EUR", value=0.74)
amount = convert_money(1, "USD", "EUR")
assert type(amount) == moneyed.Money
assert amount == moneyed.Money(Decimal("0.74"), "EUR")

amount = convert_money(10.0, "PLN", "EUR")
self.assertEqual(amount, moneyed.Money(Decimal("2.41"), "EUR"))
@pytest.mark.django_db(transaction=True)
def test_conversion_is_working_from_other_currency(set_up):
source = RateSource.objects.create(name="fake-backend", base_currency="USD")
Rate.objects.create(source=source, currency="PLN", value=3.07)
Rate.objects.create(source=source, currency="EUR", value=0.74)


amount = convert_money(10.0, "PLN", "EUR")
assert amount == moneyed.Money(Decimal("2.41"), "EUR")

0 comments on commit ca904a7

Please sign in to comment.