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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
import portfolio
import unittest
from unittest import mock
class AmountTest(unittest.TestCase):
def setUp(self):
super(AmountTest, self).setUp()
def test_values(self):
amount = portfolio.Amount("BTC", 0.65)
self.assertEqual(0.65, amount.value)
self.assertEqual("BTC", amount.currency)
amount = portfolio.Amount("BTC", 10, int_val=2000000000000000)
self.assertEqual(0.002, amount.value)
def test_in_currency(self):
amount = portfolio.Amount("ETC", 10)
self.assertEqual(amount, amount.in_currency("ETC", None))
ticker_mock = unittest.mock.Mock()
with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock):
ticker_mock.return_value = None
portfolio.Amount.get_ticker = ticker_mock
self.assertRaises(Exception, amount.in_currency, "ETH", None)
with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock):
ticker_mock.return_value = {
"average": 0.3,
"foo": "bar",
}
converted_amount = amount.in_currency("ETH", None)
self.assertEqual(3.0, converted_amount.value)
self.assertEqual("ETH", converted_amount.currency)
self.assertEqual(amount, converted_amount.linked_to)
self.assertEqual("bar", converted_amount.ticker["foo"])
@unittest.skip("TODO")
def test_get_ticker(self):
pass
def test__abs(self):
amount = portfolio.Amount("SC", -120)
self.assertEqual(120, abs(amount).value)
self.assertEqual("SC", abs(amount).currency)
amount = portfolio.Amount("SC", 10)
self.assertEqual(10, abs(amount).value)
self.assertEqual("SC", abs(amount).currency)
def test__add(self):
amount1 = portfolio.Amount("XVG", 12.9)
amount2 = portfolio.Amount("XVG", 13.1)
self.assertEqual(26, (amount1 + amount2).value)
self.assertEqual("XVG", (amount1 + amount2).currency)
amount3 = portfolio.Amount("ETH", 1.6)
with self.assertRaises(Exception):
amount1 + amount3
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 + amount4)
def test__radd(self):
amount = portfolio.Amount("XVG", 12.9)
self.assertEqual(amount, 0 + amount)
with self.assertRaises(Exception):
4 + amount
def test__sub(self):
amount1 = portfolio.Amount("XVG", 13.3)
amount2 = portfolio.Amount("XVG", 13.1)
self.assertEqual(0.2, (amount1 - amount2).value)
self.assertEqual("XVG", (amount1 - amount2).currency)
amount3 = portfolio.Amount("ETH", 1.6)
with self.assertRaises(Exception):
amount1 - amount3
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 - amount4)
def test__int(self):
amount = portfolio.Amount("XMR", 0.1)
self.assertEqual(100000000000000000, int(amount))
def test__mul(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(38.5, (amount * 3.5).value)
self.assertEqual(33, (amount * 3).value)
with self.assertRaises(Exception):
amount * amount
def test__rmul(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(38.5, (3.5 * amount).value)
self.assertEqual(33, (3 * amount).value)
def test__floordiv(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(5.5, (amount // 2).value)
with self.assertRaises(TypeError):
amount // 2.5
self.assertEqual(1571428571428571428, (amount // 7)._value)
def test__div(self):
amount = portfolio.Amount("XEM", 11)
with self.assertRaises(TypeError):
amount / 2.5
self.assertEqual(5.5, (amount / 2).value)
self.assertEqual(1571428571428571428, (amount / 7)._value)
def test__lt(self):
amount1 = portfolio.Amount("BTD", 11.3)
amount2 = portfolio.Amount("BTD", 13.1)
self.assertTrue(amount1 < amount2)
self.assertFalse(amount2 < amount1)
self.assertFalse(amount1 < amount1)
amount3 = portfolio.Amount("BTC", 1.6)
with self.assertRaises(Exception):
amount1 < amount3
def test__eq(self):
amount1 = portfolio.Amount("BTD", 11.3)
amount2 = portfolio.Amount("BTD", 13.1)
amount3 = portfolio.Amount("BTD", 11.3)
self.assertFalse(amount1 == amount2)
self.assertFalse(amount2 == amount1)
self.assertTrue(amount1 == amount3)
self.assertFalse(amount2 == 0)
amount4 = portfolio.Amount("BTC", 1.6)
with self.assertRaises(Exception):
amount1 == amount4
amount5 = portfolio.Amount("BTD", 0)
self.assertTrue(amount5 == 0)
def test__str(self):
amount1 = portfolio.Amount("BTX", 32)
self.assertEqual("32.00000000 BTX", str(amount1))
amount2 = portfolio.Amount("USDT", 12000)
amount1.linked_to = amount2
self.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1))
def test__repr(self):
amount1 = portfolio.Amount("BTX", 32)
self.assertEqual("Amount(32.00000000 BTX)", repr(amount1))
amount2 = portfolio.Amount("USDT", 12000)
amount1.linked_to = amount2
self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1))
amount3 = portfolio.Amount("BTC", 0.1)
amount2.linked_to = amount3
self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1))
if __name__ == '__main__':
unittest.main()
|