From 132d51777a66dcd35aee39397f49b09ca29885cc Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Mon, 26 May 2025 14:50:21 +0200 Subject: [PATCH] add currency pair class --- tests/tests_unit/test_currency_pair.py | 35 ++++++++++++++++++++++++++ xexe/currency_pair.py | 19 ++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/tests_unit/test_currency_pair.py create mode 100644 xexe/currency_pair.py diff --git a/tests/tests_unit/test_currency_pair.py b/tests/tests_unit/test_currency_pair.py new file mode 100644 index 0000000..93aeccc --- /dev/null +++ b/tests/tests_unit/test_currency_pair.py @@ -0,0 +1,35 @@ +from money.currency import Currency + +from xexe.currency_pair import CurrencyPair + + +def test_create_currency_pair_normal_works_fine(): + + a_pair: CurrencyPair = CurrencyPair( + from_currency=Currency["USD"], to_currency=Currency["EUR"] + ) + + assert (a_pair.from_currency == "USD", a_pair.to_currency == "EUR") + + +def test_create_currency_pair_with_same_currency_works(): + same_curr_pair: CurrencyPair = CurrencyPair( + from_currency=Currency["USD"], to_currency=Currency["USD"] + ) + + assert (same_curr_pair.from_currency == "USD", same_curr_pair.to_currency == "USD") + + +def test_reverse_currency_works_fine(): + + a_pair: CurrencyPair = CurrencyPair( + from_currency=Currency["USD"], to_currency=Currency["EUR"] + ) + + reverse_pair: CurrencyPair = a_pair.get_reverse_pair() + + assert ( + isinstance(reverse_pair, CurrencyPair), + reverse_pair.from_currency == "EUR", + reverse_pair.to_currency == "USD", + ) diff --git a/xexe/currency_pair.py b/xexe/currency_pair.py new file mode 100644 index 0000000..c2012d5 --- /dev/null +++ b/xexe/currency_pair.py @@ -0,0 +1,19 @@ +from money.currency import Currency + + +class CurrencyPair: + """Two currencies with directionality (from->to).""" + + def __init__( + self, from_currency: Currency, to_currency: Currency + ) -> "CurrencyPair": + self.from_currency = from_currency + self.to_currency = to_currency + + def __str__(self): + return str(self.from_currency) + str(self.to_currency) + + def get_reverse_pair(self) -> "CurrencyPair": + return CurrencyPair( + from_currency=self.to_currency, to_currency=self.from_currency + )