forked from bitshares/python-bitshares
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_amount.py
More file actions
213 lines (184 loc) · 6.68 KB
/
test_amount.py
File metadata and controls
213 lines (184 loc) · 6.68 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
import unittest
from bitshares import BitShares
from bitshares.amount import Amount
from bitshares.asset import Asset
from bitshares.instance import set_shared_bitshares_instance, SharedInstance
from .fixtures import fixture_data, bitshares
class Testcases(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.asset = Asset("BTS")
self.symbol = Asset("BTS")["symbol"]
self.precision = Asset("BTS")["precision"]
self.asset2 = Asset("EUR")
def setUp(self):
fixture_data()
def dotest(self, ret, amount, symbol):
self.assertEqual(float(ret), float(amount))
self.assertEqual(ret["symbol"], symbol)
self.assertIsInstance(ret["asset"], dict)
self.assertIsInstance(ret["amount"], float)
def test_init(self):
# String init
amount = Amount("1 {}".format(self.symbol))
self.dotest(amount, 1, self.symbol)
# Amount init
amount = Amount(amount)
self.dotest(amount, 1, self.symbol)
# blockchain dict init
amount = Amount(
{"amount": 1 * 10 ** self.precision, "asset_id": self.asset["id"]}
)
self.dotest(amount, 1, self.symbol)
# API dict init
amount = Amount(
{"amount": 1.3 * 10 ** self.precision, "asset": self.asset["id"]}
)
self.dotest(amount, 1.3, self.symbol)
# Asset as symbol
amount = Amount(1.3, Asset("1.3.0"))
self.dotest(amount, 1.3, self.symbol)
# Asset as symbol
amount = Amount(1.3, self.symbol)
self.dotest(amount, 1.3, self.symbol)
# keyword inits
amount = Amount(amount=1.3, asset=Asset("1.3.0"))
self.dotest(amount, 1.3, self.symbol)
# keyword inits
amount = Amount(amount=1.3, asset=dict(Asset("1.3.0")))
self.dotest(amount, 1.3, self.symbol)
# keyword inits
amount = Amount(amount=1.3, asset=self.symbol)
self.dotest(amount, 1.3, self.symbol)
def test_copy(self):
amount = Amount("1", self.symbol)
self.dotest(amount.copy(), 1, self.symbol)
def test_properties(self):
amount = Amount("1", self.symbol)
self.assertEqual(amount.amount, 1.0)
self.assertEqual(amount.symbol, self.symbol)
self.assertIsInstance(amount.asset, Asset)
self.assertEqual(amount.asset["symbol"], self.symbol)
def test_tuple(self):
amount = Amount("1", self.symbol)
self.assertEqual(amount.tuple(), (1.0, self.symbol))
def test_json(self):
amount = Amount("1", self.symbol)
self.assertEqual(
amount.json(),
{"asset_id": self.asset["id"], "amount": 1 * 10 ** self.precision},
)
def test_string(self):
self.assertEqual(
str(Amount("1", self.symbol)), "1.00000 {}".format(self.symbol)
)
def test_int(self):
self.assertEqual(int(Amount("1", self.symbol)), 100000)
def test_float(self):
self.assertEqual(float(Amount("1", self.symbol)), 1.00000)
def test_plus(self):
a1 = Amount(1, self.symbol)
a2 = Amount(2, self.symbol)
self.dotest(a1 + a2, 3, self.symbol)
with self.assertRaises(Exception):
a1 + Amount(1, asset=self.asset2)
# inline
a2 = Amount(2, self.symbol)
a2 += a1
self.dotest(a2, 3, self.symbol)
a2 += 5
self.dotest(a2, 8, self.symbol)
with self.assertRaises(Exception):
a1 += Amount(1, asset=self.asset2)
def test_minus(self):
a1 = Amount(1, self.symbol)
a2 = Amount(2, self.symbol)
self.dotest(a1 - a2, -1, self.symbol)
self.dotest(a1 - 5, -4, self.symbol)
with self.assertRaises(Exception):
a1 - Amount(1, asset=self.asset2)
# inline
a2 = Amount(2, self.symbol)
a2 -= a1
self.dotest(a2, 1, self.symbol)
a2 -= 1
self.dotest(a2, 0, self.symbol)
self.dotest(a2 - 2, -2, self.symbol)
with self.assertRaises(Exception):
a1 -= Amount(1, asset=self.asset2)
def test_mul(self):
a1 = Amount(5, self.symbol)
a2 = Amount(2, self.symbol)
self.dotest(a1 * a2, 10, self.symbol)
self.dotest(a1 * 3, 15, self.symbol)
with self.assertRaises(Exception):
a1 * Amount(1, asset=self.asset2)
# inline
a2 = Amount(2, self.symbol)
a2 *= 5
self.dotest(a2, 10, self.symbol)
with self.assertRaises(Exception):
a1 *= Amount(2, asset=self.asset2)
def test_div(self):
a1 = Amount(15, self.symbol)
self.dotest(a1 / 3, 5, self.symbol)
self.dotest(a1 // 2, 7, self.symbol)
with self.assertRaises(Exception):
a1 / Amount(1, asset=self.asset2)
# inline
a2 = a1.copy()
a2 /= 3
self.dotest(a2, 5, self.symbol)
a2 = a1.copy()
a2 //= 2
self.dotest(a2, 7, self.symbol)
with self.assertRaises(Exception):
a1 *= Amount(2, asset=self.asset2)
def test_mod(self):
a1 = Amount(15, self.symbol)
self.dotest(a1 % 3, 0, self.symbol)
self.dotest(a1 % 2, 1, self.symbol)
with self.assertRaises(Exception):
a1 % Amount(1, asset=self.asset2)
# inline
a2 = a1.copy()
a2 %= 3
self.dotest(a2, 0, self.symbol)
with self.assertRaises(Exception):
a1 %= Amount(2, asset=self.asset2)
def test_pow(self):
a1 = Amount(15, self.symbol)
self.dotest(a1 ** 3, 15 ** 3, self.symbol)
self.dotest(a1 ** 2, 15 ** 2, self.symbol)
with self.assertRaises(Exception):
a1 ** Amount(1, asset=self.asset2)
# inline
a2 = a1.copy()
a2 **= 3
self.dotest(a2, 15 ** 3, self.symbol)
with self.assertRaises(Exception):
a1 **= Amount(2, asset=self.asset2)
def test_ltge(self):
a1 = Amount(1, self.symbol)
a2 = Amount(2, self.symbol)
self.assertTrue(a1 < a2)
self.assertTrue(a2 > a1)
self.assertTrue(a2 > 1)
self.assertTrue(a1 < 5)
def test_leeq(self):
a1 = Amount(1, self.symbol)
a2 = Amount(1, self.symbol)
self.assertTrue(a1 <= a2)
self.assertTrue(a1 >= a2)
self.assertTrue(a1 <= 1)
self.assertTrue(a1 >= 1)
def test_ne(self):
a1 = Amount(1, self.symbol)
a2 = Amount(2, self.symbol)
self.assertTrue(a1 != a2)
self.assertTrue(a1 != 5)
a1 = Amount(1, self.symbol)
a2 = Amount(1, self.symbol)
self.assertTrue(a1 == a2)
self.assertTrue(a1 == 1)