From 7cfbe2284d796e8b0e5130e72e3ed5f59181912b Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Mon, 26 May 2025 15:03:08 +0200 Subject: [PATCH] implement equality --- tests/tests_unit/test_currency_pair.py | 13 ++++++++++++- xexe/currency_pair.py | 11 ++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/tests_unit/test_currency_pair.py b/tests/tests_unit/test_currency_pair.py index 93aeccc..722f9e7 100644 --- a/tests/tests_unit/test_currency_pair.py +++ b/tests/tests_unit/test_currency_pair.py @@ -20,7 +20,7 @@ def test_create_currency_pair_with_same_currency_works(): assert (same_curr_pair.from_currency == "USD", same_curr_pair.to_currency == "USD") -def test_reverse_currency_works_fine(): +def test_reverse_pair_works_fine(): a_pair: CurrencyPair = CurrencyPair( from_currency=Currency["USD"], to_currency=Currency["EUR"] @@ -33,3 +33,14 @@ def test_reverse_currency_works_fine(): reverse_pair.from_currency == "EUR", reverse_pair.to_currency == "USD", ) + + +def test_pair_equality_works(): + + a_pair: CurrencyPair = CurrencyPair( + from_currency=Currency["USD"], to_currency=Currency["EUR"] + ) + + reverse_pair: CurrencyPair = a_pair.get_reverse_pair() + + assert a_pair == a_pair and a_pair != reverse_pair diff --git a/xexe/currency_pair.py b/xexe/currency_pair.py index c2012d5..d9a603b 100644 --- a/xexe/currency_pair.py +++ b/xexe/currency_pair.py @@ -10,10 +10,15 @@ class 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 ) + + def __str__(self): + return str(self.from_currency) + str(self.to_currency) + + def __eq__(self, other: "CurrencyPair") -> bool: + return (self.from_currency == other.from_currency) and ( + self.to_currency == other.to_currency + )