73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
import pytest
|
|
|
|
from cashflow_model import CurrencyConverter, CurrencyError, ExchangeRate
|
|
|
|
|
|
class TestExchangeRate:
|
|
def test_to_dict_roundtrip(self):
|
|
rate = ExchangeRate(from_currency="USD", to_currency="RUB", rate=80.0)
|
|
d = rate.to_dict()
|
|
r2 = ExchangeRate.from_dict(d)
|
|
assert r2.from_currency == "USD"
|
|
assert r2.to_currency == "RUB"
|
|
assert r2.rate == 80.0
|
|
|
|
def test_defaults(self):
|
|
r = ExchangeRate()
|
|
assert r.from_currency == "USD"
|
|
assert r.to_currency == "RUB"
|
|
assert r.rate == 80.0
|
|
|
|
|
|
class TestCurrencyConverter:
|
|
def test_convert_usd_to_rub(self, sample_converter):
|
|
result = sample_converter.convert(100, "USD", "RUB")
|
|
assert result == 8000.0
|
|
|
|
def test_convert_rub_to_usd(self, sample_converter):
|
|
result = sample_converter.convert(8000, "RUB", "USD")
|
|
assert result == 100.0
|
|
|
|
def test_same_currency(self, sample_converter):
|
|
result = sample_converter.convert(500, "USD", "USD")
|
|
assert result == 500.0
|
|
|
|
def test_unknown_pair(self):
|
|
converter = CurrencyConverter()
|
|
with pytest.raises(CurrencyError):
|
|
converter.convert(100, "USD", "RUB")
|
|
|
|
def test_negative_rate(self):
|
|
converter = CurrencyConverter()
|
|
with pytest.raises(CurrencyError):
|
|
converter.set_rate("USD", "RUB", -1)
|
|
|
|
def test_zero_rate(self):
|
|
converter = CurrencyConverter()
|
|
with pytest.raises(CurrencyError):
|
|
converter.set_rate("USD", "RUB", 0)
|
|
|
|
def test_inverse_auto(self):
|
|
rate = ExchangeRate(from_currency="EUR", to_currency="RUB", rate=90.0)
|
|
converter = CurrencyConverter([rate])
|
|
assert converter.convert(900, "RUB", "EUR") == 10.0
|
|
|
|
def test_get_symbol(self, sample_converter):
|
|
assert sample_converter.get_symbol("USD") == "$"
|
|
assert sample_converter.get_symbol("RUB") == "₽"
|
|
assert sample_converter.get_symbol("XYZ") == "XYZ"
|
|
|
|
def test_with_defaults(self):
|
|
converter = CurrencyConverter.with_defaults()
|
|
assert converter.convert(10, "USD", "RUB") == 800.0
|
|
|
|
def test_multiple_rates(self):
|
|
rates = [
|
|
ExchangeRate(from_currency="USD", to_currency="RUB", rate=80.0),
|
|
ExchangeRate(from_currency="EUR", to_currency="RUB", rate=90.0),
|
|
]
|
|
converter = CurrencyConverter(rates)
|
|
assert converter.convert(10, "USD", "RUB") == 800.0
|
|
assert converter.convert(10, "EUR", "RUB") == 900.0
|
|
assert converter.convert(900, "RUB", "EUR") == 10.0
|