diff --git a/currency.py b/currency.py new file mode 100644 index 0000000..ecf3255 --- /dev/null +++ b/currency.py @@ -0,0 +1,30 @@ +rates = [('USD', 'EUR', 0.86), + ('USD', 'JPY', 118.68), + ('GBP', 'USD', 1.51), + ('USD', 'CHF', 0.87), + ('USD', 'CAD', 1.21), + ('EUR', 'JPY', 137.08), + ('AUD', 'USD', 0.82)] + + +def get_rate(start, to): + """Returns the exchange rate based on the starting currency to the ending + currency.""" + for tup in rates: + if start == tup[0] and to == tup[1]: + rate = tup[2] + return rate + elif start == tup[1] and to == tup[0]: + rate = round(1/tup[2], 2) + return rate + + +def convert(rates, value, start, to): + """Takes a value and applies the correct exchange rate and returns the + result.""" + rate = get_rate(start, to) + if start == to: + return value + else: + rate = get_rate(start, to) + return value * rate diff --git a/test_currency.py b/test_currency.py new file mode 100644 index 0000000..759a6b7 --- /dev/null +++ b/test_currency.py @@ -0,0 +1,45 @@ +from currency import * + +rates = [('USD', 'EUR', 0.86), + ('USD', 'JPY', 118.68), + ('GBP', 'USD', 1.51), + ('USD', 'CHF', 0.87), + ('USD', 'CAD', 1.21), + ('EUR', 'JPY', 137.08), + ('AUD', 'USD', 0.82)] + + +def test_convert_same(): + assert convert(rates[0][2], 1, 'USD', 'USD') == 1 + + +def test_convert_USDtoEUR(): + assert convert(rates[0][2], 1, 'USD', 'EUR') == 0.86 + + +def test_value_other_than_1(): + assert convert(rates[0][2], 2, 'USD', 'EUR') == 1.72 + + +def test_convert_EURtoUSD(): + assert convert(rates[1][2], 1, 'EUR', 'USD') == 1.16 + + +def test_get_rate_normal(): + assert get_rate('USD', 'EUR') == 0.86 + + +def test_get_rate_inverse(): + assert get_rate('EUR', 'USD') == 1.16 + + +def test_convert_GBPtoUSD(): + assert convert(rates, 2, 'GBP', 'USD') == 3.02 + + +def test_convert_USDtoGBP(): + assert convert(rates, 2, 'USD', 'GBP') == 1.32 + + +def test_convert_EURtoJPY(): + assert convert(rates, 3, 'EUR', 'JPY') == 411.24