This is a follow up question on this post
This is what I changed on my previous code:
I use
Exchanger
andPairCurrency
class instead ofBank
I renamed
addRate
andaddCommission
tosetRate
andsetCommission
respectivelyExchanger
object contains multiplePairCurrency
objects which can be added toExchanger
byaddPairCurrency()
method.
This is my code and tests:
import pytest
class DuplicatePairError(Exception):
pass
class Exchanger:
def __init__(self):
self.pair_currencies = []
self.commission = 0.015
def addPairCurrency(self, pair_object):
if pair_object in self.pair_currencies:
raise DuplicatePairError('Cannot add duplicate pair of currency')
self.pair_currencies.append(pair_object)
@property
def last_pair(self):
return self.pair_currencies[-1]
def getRate(self, pair):
for pair_obj in self.pair_currencies:
if pair_obj.name == pair:
return pair_obj.rate
def convert(self, src_dest, amount):
return amount / self.getRate(src_dest) * (1-self.commission)
def editRate(self,new_object):
for pair_obj in self.pair_currencies:
if pair_obj.name == new_object.name:
pair_obj.rate = new_object.rate
class PairCurrency:
def __init__(self, name):
self.name = name
def setRate(self, rate):
self.rate = rate
def editRate(self, new_rate):
self.rate = new_rate
def __eq__(self, object):
return self.name == object
def test_canInitializePair_CurrenciesListInExhangeClass():
exchanger = Exchanger()
assert exchanger.pair_currencies == []
def test_canSetRateinPairCurrencyClass():
USD_GBP = PairCurrency('USDGBP')
USD_GBP.setRate(2)
assert USD_GBP.name == 'USDGBP'
assert USD_GBP.rate == 2
def test_canSetCommisionInExchanger():
exchanger = Exchanger()
assert exchanger.commission == 0.015
@pytest.fixture()
def USD_GBP():
USD_GBP = PairCurrency('USDGBP')
USD_GBP.setRate(2)
return USD_GBP
@pytest.fixture()
def GBP_USD():
GBP_USD = PairCurrency('GBPUSD')
GBP_USD.setRate(0.5)
return GBP_USD
@pytest.fixture()
def exchanger(USD_GBP,GBP_USD):
exchanger = Exchanger()
exchanger.addPairCurrency(USD_GBP)
exchanger.addPairCurrency(GBP_USD)
return exchanger
def test_canAddPairCurrencyinExchangerClass(exchanger, USD_GBP, GBP_USD):
assert USD_GBP == exchanger.pair_currencies[-2]
assert GBP_USD == exchanger.last_pair
def test_cannotAddDuplicatePair(exchanger, USD_GBP):
USD_GBP2 = PairCurrency('USDGBP')
USD_GBP2.setRate(5)
exchanger = Exchanger()
exchanger.addPairCurrency(USD_GBP)
with pytest.raises(DuplicatePairError) as excinfo:
exchanger.addPairCurrency(USD_GBP2)
assert str(excinfo.value) == 'Cannot add duplicate pair of currency'
def test_canEditRateInPairCurrencyClass(USD_GBP):
USD_GBP.editRate(3)
assert USD_GBP.rate == 3
USD_GBP.editRate(4)
assert USD_GBP.rate == 4
def test_canEditRateinExhangerClass(exchanger):
USD_GBP = PairCurrency('USDGBP')
USD_GBP.setRate(5)
exchanger.editRate(USD_GBP)
assert exchanger.getRate('USDGBP') == 5
def test_canConvertInExchangerClass(exchanger, USD_GBP, GBP_USD):
assert exchanger.convert(src_dest = 'USDGBP', amount = 100) == 49.25
assert exchanger.convert(src_dest='GBPUSD', amount=100) == 197
I have few questions:
How good is my OOP design?
Is my test list fine?
Should I remove
editRate()
inPairCurrency
and just keepeditRate()
inExchanger
?Do my classes violate the SOLID Principle ?
What I have to fix to improve my code and OOP design ?