-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtesting.py
More file actions
94 lines (64 loc) · 2.41 KB
/
testing.py
File metadata and controls
94 lines (64 loc) · 2.41 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import unittest
class Currency:
def __init__(self, code, exchange_to_usd):
self.amount = 0.00
self.code = code
self.exchange_to_usd = exchange_to_usd
def set_amount(self, amount):
self.amount = amount
def in_currency(self, amount):
return amount / self.exchange_to_usd
def to_usd(self, amount=None):
to_convert = amount or self.amount
return to_convert * self.exchange_to_usd
def __eq__(self, other):
return self.to_usd() == other.to_usd()
def __gt__(self, other):
return self.to_usd() > other.to_usd()
def __lt__(self, other):
return self.to_usd() < other.to_usd()
def __le__(self, other):
return self.to_usd() <= other.to_usd()
def __ge__(self, other):
return self.to_usd() >= other.to_usd()
class CurrencyTest(unittest.TestCase):
def test_create_currency(self):
pounds = Currency('GBP', 1.21)
self.assertEqual(pounds.code, 'GBP')
self.assertEqual(pounds.exchange_to_usd, 1.21)
def test_set_amount(self):
pounds = Currency('GBP', 1.21)
euros = Currency('EUR', 1.07)
pounds.set_amount(5000)
euros.set_amount(10)
self.assertEqual(pounds.amount, 5000)
self.assertEqual(euros.amount, 10)
def test_compare_currency(self):
pounds = Currency('GBP', 1.21)
euros = Currency('EUR', 1.07)
pounds.set_amount(5000)
euros.set_amount(10)
self.assertTrue(pounds > euros)
self.assertFalse(pounds < euros)
self.assertFalse(pounds == euros)
def test_compare_currency_equal_value(self):
pounds = Currency('GBP', 1.21)
pounds2 = Currency('GBP', 1.21)
pounds.set_amount(500)
pounds2.set_amount(500)
self.assertTrue(pounds >= pounds2)
self.assertTrue(pounds <= pounds2)
self.assertTrue(pounds == pounds2)
self.assertFalse(pounds > pounds2)
self.assertFalse(pounds < pounds2)
def test_in_currency(self):
pounds = Currency('GBP', 1.21)
self.assertEqual(pounds.in_currency(1210), 1000)
def test_to_usd(self):
pounds = Currency('GBP', 1.21)
self.assertEqual(pounds.to_usd(1000), 1210)
def test_comparison_with_exceptions(self):
pounds = Currency('GBP', 1.21)
pounds.set_amount(1000)
with self.assertRaises(AttributeError):
pounds == 1000