Create a set of functions to convert currency from one denomination to another.
After completing this assignment, you should understand:
- Test-driven development
After completing this assignment, you should be able to:
- Raise exceptions
- Write detailed tests
- Break down programming problems into smaller ones
- A Git repo called currency-converter containing at least:
README.mdfile explaining how to run your project- a
currencymodule - a
test_currencyset of tests
- Passing unit tests
- No PEP8 or Pyflakes warnings or errors
- No functions longer than 7 lines of code
- 100% test coverage
Use test-driven development to design a function called convert. Below is a list of all the things it should do. In order to complete this exercise, follow these directions:
- For each requirement, write a test first, and then make the test pass, do not pass Go, do not collect $200.
- Every time you finish a requirement, commit your code with a message describing the requirement.
- Go through the requirements one-by-one. Do not skip around.
-
Create a function called
convertthat takes a list calledrates, a number calledvalue, a string calledfrom, and a string calledto. Make sure than when you callconvertwithfromandtobeing equal, the return value is the same asvalue. -
rateslist should be a list of tuples, with each tuple containing a currency code you can convert from, a currency code you can convert to, and a rate.[("USD", "EUR", 0.74)]
This means that each dollar is worth 0.74 euros.
valueis the amount of currency,fromis the current currency code, andtois the currency code you wish to convert to.Given the above rates, make sure that converting 1 dollar into euros returns the following value:
0.74. -
Next, test that you can convert currency with a
valuethat is not 1. -
Next, test that converting 1 euro into dollars returns
1.35(or an approximation). -
Create a new list of rates with two or more tuples. Make sure you can convert both ways with each rate. For example, with these rates:
[("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]
Make sure you can convert from USD to EUR, EUR to USD, EUR to JPY, and JPY to EUR.
In addition to the requirements from Normal Mode:
-
Make sure that if you try to make a conversion you do not know about, a
ValueErroris raised with an appropriate message. -
Test that you can convert between any two rates that you have the ability to, even if you do not have a direct conversion rate for them. For example, with the rates:
[("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]
Make sure you can convert from USD to JPY.
This will probably require Dijkstra's algorithm.
- Currency charts if you want accurate values.
- The pytest testing framework.
- An Introduction to Test Driven Development.
- Test-Driven Development on Wikibooks.
- Test-Driven Development by Example, the canonical book on this stuff.