diff options
Diffstat (limited to 'test.py')
-rw-r--r-- | test.py | 174 |
1 files changed, 174 insertions, 0 deletions
@@ -0,0 +1,174 @@ | |||
1 | import portfolio | ||
2 | import unittest | ||
3 | from unittest import mock | ||
4 | |||
5 | class AmountTest(unittest.TestCase): | ||
6 | def setUp(self): | ||
7 | super(AmountTest, self).setUp() | ||
8 | |||
9 | def test_values(self): | ||
10 | amount = portfolio.Amount("BTC", 0.65) | ||
11 | self.assertEqual(0.65, amount.value) | ||
12 | self.assertEqual("BTC", amount.currency) | ||
13 | |||
14 | amount = portfolio.Amount("BTC", 10, int_val=2000000000000000) | ||
15 | self.assertEqual(0.002, amount.value) | ||
16 | |||
17 | def test_in_currency(self): | ||
18 | amount = portfolio.Amount("ETC", 10) | ||
19 | |||
20 | self.assertEqual(amount, amount.in_currency("ETC", None)) | ||
21 | |||
22 | ticker_mock = unittest.mock.Mock() | ||
23 | with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock): | ||
24 | ticker_mock.return_value = None | ||
25 | portfolio.Amount.get_ticker = ticker_mock | ||
26 | |||
27 | self.assertRaises(Exception, amount.in_currency, "ETH", None) | ||
28 | |||
29 | with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock): | ||
30 | ticker_mock.return_value = { | ||
31 | "average": 0.3, | ||
32 | "foo": "bar", | ||
33 | } | ||
34 | converted_amount = amount.in_currency("ETH", None) | ||
35 | |||
36 | self.assertEqual(3.0, converted_amount.value) | ||
37 | self.assertEqual("ETH", converted_amount.currency) | ||
38 | self.assertEqual(amount, converted_amount.linked_to) | ||
39 | self.assertEqual("bar", converted_amount.ticker["foo"]) | ||
40 | |||
41 | @unittest.skip("TODO") | ||
42 | def test_get_ticker(self): | ||
43 | pass | ||
44 | |||
45 | def test__abs(self): | ||
46 | amount = portfolio.Amount("SC", -120) | ||
47 | self.assertEqual(120, abs(amount).value) | ||
48 | self.assertEqual("SC", abs(amount).currency) | ||
49 | |||
50 | amount = portfolio.Amount("SC", 10) | ||
51 | self.assertEqual(10, abs(amount).value) | ||
52 | self.assertEqual("SC", abs(amount).currency) | ||
53 | |||
54 | def test__add(self): | ||
55 | amount1 = portfolio.Amount("XVG", 12.9) | ||
56 | amount2 = portfolio.Amount("XVG", 13.1) | ||
57 | |||
58 | self.assertEqual(26, (amount1 + amount2).value) | ||
59 | self.assertEqual("XVG", (amount1 + amount2).currency) | ||
60 | |||
61 | amount3 = portfolio.Amount("ETH", 1.6) | ||
62 | with self.assertRaises(Exception): | ||
63 | amount1 + amount3 | ||
64 | |||
65 | amount4 = portfolio.Amount("ETH", 0.0) | ||
66 | self.assertEqual(amount1, amount1 + amount4) | ||
67 | |||
68 | def test__radd(self): | ||
69 | amount = portfolio.Amount("XVG", 12.9) | ||
70 | |||
71 | self.assertEqual(amount, 0 + amount) | ||
72 | with self.assertRaises(Exception): | ||
73 | 4 + amount | ||
74 | |||
75 | def test__sub(self): | ||
76 | amount1 = portfolio.Amount("XVG", 13.3) | ||
77 | amount2 = portfolio.Amount("XVG", 13.1) | ||
78 | |||
79 | self.assertEqual(0.2, (amount1 - amount2).value) | ||
80 | self.assertEqual("XVG", (amount1 - amount2).currency) | ||
81 | |||
82 | amount3 = portfolio.Amount("ETH", 1.6) | ||
83 | with self.assertRaises(Exception): | ||
84 | amount1 - amount3 | ||
85 | |||
86 | amount4 = portfolio.Amount("ETH", 0.0) | ||
87 | self.assertEqual(amount1, amount1 - amount4) | ||
88 | |||
89 | def test__int(self): | ||
90 | amount = portfolio.Amount("XMR", 0.1) | ||
91 | self.assertEqual(100000000000000000, int(amount)) | ||
92 | |||
93 | def test__mul(self): | ||
94 | amount = portfolio.Amount("XEM", 11) | ||
95 | |||
96 | self.assertEqual(38.5, (amount * 3.5).value) | ||
97 | self.assertEqual(33, (amount * 3).value) | ||
98 | |||
99 | with self.assertRaises(Exception): | ||
100 | amount * amount | ||
101 | |||
102 | def test__rmul(self): | ||
103 | amount = portfolio.Amount("XEM", 11) | ||
104 | |||
105 | self.assertEqual(38.5, (3.5 * amount).value) | ||
106 | self.assertEqual(33, (3 * amount).value) | ||
107 | |||
108 | def test__floordiv(self): | ||
109 | amount = portfolio.Amount("XEM", 11) | ||
110 | |||
111 | self.assertEqual(5.5, (amount // 2).value) | ||
112 | with self.assertRaises(TypeError): | ||
113 | amount // 2.5 | ||
114 | self.assertEqual(1571428571428571428, (amount // 7)._value) | ||
115 | |||
116 | def test__div(self): | ||
117 | amount = portfolio.Amount("XEM", 11) | ||
118 | |||
119 | with self.assertRaises(TypeError): | ||
120 | amount / 2.5 | ||
121 | self.assertEqual(5.5, (amount / 2).value) | ||
122 | self.assertEqual(1571428571428571428, (amount / 7)._value) | ||
123 | |||
124 | def test__lt(self): | ||
125 | amount1 = portfolio.Amount("BTD", 11.3) | ||
126 | amount2 = portfolio.Amount("BTD", 13.1) | ||
127 | |||
128 | self.assertTrue(amount1 < amount2) | ||
129 | self.assertFalse(amount2 < amount1) | ||
130 | self.assertFalse(amount1 < amount1) | ||
131 | |||
132 | amount3 = portfolio.Amount("BTC", 1.6) | ||
133 | with self.assertRaises(Exception): | ||
134 | amount1 < amount3 | ||
135 | |||
136 | def test__eq(self): | ||
137 | amount1 = portfolio.Amount("BTD", 11.3) | ||
138 | amount2 = portfolio.Amount("BTD", 13.1) | ||
139 | amount3 = portfolio.Amount("BTD", 11.3) | ||
140 | |||
141 | self.assertFalse(amount1 == amount2) | ||
142 | self.assertFalse(amount2 == amount1) | ||
143 | self.assertTrue(amount1 == amount3) | ||
144 | self.assertFalse(amount2 == 0) | ||
145 | |||
146 | amount4 = portfolio.Amount("BTC", 1.6) | ||
147 | with self.assertRaises(Exception): | ||
148 | amount1 == amount4 | ||
149 | |||
150 | amount5 = portfolio.Amount("BTD", 0) | ||
151 | self.assertTrue(amount5 == 0) | ||
152 | |||
153 | def test__str(self): | ||
154 | amount1 = portfolio.Amount("BTX", 32) | ||
155 | self.assertEqual("32.00000000 BTX", str(amount1)) | ||
156 | |||
157 | amount2 = portfolio.Amount("USDT", 12000) | ||
158 | amount1.linked_to = amount2 | ||
159 | self.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1)) | ||
160 | |||
161 | def test__repr(self): | ||
162 | amount1 = portfolio.Amount("BTX", 32) | ||
163 | self.assertEqual("Amount(32.00000000 BTX)", repr(amount1)) | ||
164 | |||
165 | amount2 = portfolio.Amount("USDT", 12000) | ||
166 | amount1.linked_to = amount2 | ||
167 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1)) | ||
168 | |||
169 | amount3 = portfolio.Amount("BTC", 0.1) | ||
170 | amount2.linked_to = amount3 | ||
171 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1)) | ||
172 | |||
173 | if __name__ == '__main__': | ||
174 | unittest.main() | ||