-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexercise.py
More file actions
executable file
·51 lines (45 loc) · 1.67 KB
/
exercise.py
File metadata and controls
executable file
·51 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import pytest
import subprocess
import sys
import rates
@pytest.mark.parametrize(
"base, currency, value, date, converted",
[
("EUR", "USD", 10, "2019-04-20", 11.25),
("USD", "EUR", 11.25, "2019-04-20", 10),
("eur", "usd", 10, "2019-01-20", 11.40),
("usd", "eur", 11.40, "2019-01-20", 10),
]
)
def test_convert_valid(base, currency, value, date, converted):
res = rates.convert(base, currency, value, date)
assert converted == round(res, 2)
@pytest.mark.parametrize(
"base, currency, value, date, converted",
[
("FOO", "USD", 10, "2019-04-20", 11.25),
("EUR", "USD", 10, "2019-20-04", 11.25),
("EUR", "USD", "abc", "2019-04-20", 11.25),
]
)
def test_convert_invalid(base, currency, value, date, converted):
with pytest.raises(RuntimeError):
res = rates.convert(base, currency, value, date)
@pytest.mark.parametrize(
"base, currencies, values, date, output",
[
("EUR", ["USD"], ["10"], "2019-04-20", "10.00 EUR is 11.25 USD\n"),
("eur", ["usd"], ["10", "20", "100"], "2019-04-20", "10.00 EUR is 11.25 USD\n20.00 EUR is 22.50 USD\n100.00 EUR is 112.50 USD\n"),
("EUR", ["USD", "CZK", "CNY"], ["10"], "2019-04-20", "10.00 EUR is 11.25 USD\n10.00 EUR is 256.82 CZK\n10.00 EUR is 75.45 CNY\n"),
]
)
def test_command(base, currencies, values, date, output):
cmd = ["./rates.py", "-b", base, "-d", date]
for currency in currencies:
cmd.extend(["-c", currency])
cmd.extend(values)
res = subprocess.run(cmd, capture_output=True)
assert res.stdout.decode() == output
assert res.stderr.decode() == ""
pytest.main(sys.argv)