Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions currency.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions test_currency.py
Original file line number Diff line number Diff line change
@@ -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