forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolio_test.go
379 lines (322 loc) · 11.6 KB
/
portfolio_test.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package portfolio
import (
"reflect"
"testing"
)
func TestGetEthereumBalance(t *testing.T) {
address := "0xb794f5ea0ba39494ce839613fffba74279579268"
nonsenseAddress := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
response, err := GetEthereumBalance(address)
if err != nil {
t.Errorf("Test Failed - Portfolio GetEthereumBalance() Error: %s", err)
}
if response.Address != "0xb794f5ea0ba39494ce839613fffba74279579268" {
t.Error("Test Failed - Portfolio GetEthereumBalance() address invalid")
}
response, err = GetEthereumBalance(nonsenseAddress)
if response.Error.Message != "" {
t.Errorf("Test Failed - Portfolio GetEthereumBalance() Error: %s",
response.Error.Message)
}
}
func TestGetCryptoIDBalance(t *testing.T) {
ltcAddress := "LX2LMYXtuv5tiYEMztSSoEZcafFPYJFRK1"
_, err := GetCryptoIDAddress(ltcAddress, "ltc")
if err != nil {
t.Fatalf("Test failed. TestGetCryptoIDBalance error: %s", err)
}
}
func TestGetAddressBalance(t *testing.T) {
ltcAddress := "LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL"
ltc := "ltc"
description := "Description of Wallet"
balance := float64(1000)
portfolio := Base{}
portfolio.AddAddress(ltcAddress, ltc, description, balance)
addBalance, _ := portfolio.GetAddressBalance("LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL", ltc, description)
if addBalance != balance {
t.Error("Test Failed - Portfolio GetAddressBalance() Error: Incorrect value")
}
addBalance, found := portfolio.GetAddressBalance("WigWham", ltc, description)
if addBalance != 0 {
t.Error("Test Failed - Portfolio GetAddressBalance() Error: Incorrect value")
}
if found != false {
t.Error("Test Failed - Portfolio GetAddressBalance() Error: Incorrect value")
}
}
func TestExchangeExists(t *testing.T) {
newBase := Base{}
newBase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
if !newBase.ExchangeExists("someaddress") {
t.Error("Test Failed - portfolio_test.go - AddressExists error")
}
if newBase.ExchangeExists("bla") {
t.Error("Test Failed - portfolio_test.go - AddressExists error")
}
}
func TestAddressExists(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
if !newbase.AddressExists("someaddress") {
t.Error("Test Failed - portfolio_test.go - AddressExists error")
}
if newbase.AddressExists("bla") {
t.Error("Test Failed - portfolio_test.go - AddressExists error")
}
}
func TestExchangeAddressExists(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
if !newbase.ExchangeAddressExists("someaddress", "LTC") {
t.Error("Test Failed - portfolio_test.go - ExchangeAddressExists error")
}
if newbase.ExchangeAddressExists("TEST", "LTC") {
t.Error("Test Failed - portfolio_test.go - ExchangeAddressExists error")
}
}
func TestAddExchangeAddress(t *testing.T) {
newbase := Base{}
newbase.AddExchangeAddress("ANX", "BTC", 100)
newbase.AddExchangeAddress("ANX", "BTC", 200)
if !newbase.ExchangeAddressExists("ANX", "BTC") {
t.Error("Test Failed - TestExchangeAddressExists address doesn't exist")
}
}
func TestUpdateAddressBalance(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
newbase.UpdateAddressBalance("someaddress", 0.03)
value := newbase.GetPortfolioSummary()
if value.Totals[0].Coin != "LTC" && value.Totals[0].Balance != 0.03 {
t.Error("Test Failed - portfolio_test.go - UpdateUpdateAddressBalance error")
}
}
func TestRemoveAddress(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddr", "LTC", "LTCWALLETTEST", 420)
if !newbase.AddressExists("someaddr") {
t.Error("Test failed - portfolio_test.go - TestRemoveAddress")
}
newbase.RemoveAddress("someaddr", "LTC", "LTCWALLETTEST")
if newbase.AddressExists("someaddr") {
t.Error("Test failed - portfolio_test.go - TestRemoveAddress")
}
}
func TestRemoveExchangeAddress(t *testing.T) {
newbase := Base{}
exchangeName := "BallerExchange"
coinType := "LTC"
newbase.AddExchangeAddress(exchangeName, coinType, 420)
if !newbase.ExchangeAddressExists(exchangeName, coinType) {
t.Error("Test failed - portfolio_test.go - TestRemoveAddress")
}
newbase.RemoveExchangeAddress(exchangeName, coinType)
if newbase.ExchangeAddressExists(exchangeName, coinType) {
t.Error("Test failed - portfolio_test.go - TestRemoveAddress")
}
}
func TestUpdateExchangeAddressBalance(t *testing.T) {
newbase := Base{}
newbase.AddExchangeAddress("someaddress", "LTC", 0.02)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
portfolio.UpdateExchangeAddressBalance("someaddress", "LTC", 0.04)
value := portfolio.GetPortfolioSummary()
if value.Totals[0].Coin != "LTC" && value.Totals[0].Balance != 0.04 {
t.Error("Test Failed - portfolio_test.go - UpdateExchangeAddressBalance error")
}
}
func TestAddAddress(t *testing.T) {
newbase := Base{}
newbase.AddAddress("Gibson", "LTC", "LTCWALLETTEST", 0.02)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
if !portfolio.AddressExists("Gibson") {
t.Error("Test Failed - portfolio_test.go - AddAddress error")
}
// Test updating balance to <= 0, expected result is to remove the address.
// Fail if address still exists.
newbase.AddAddress("Gibson", "LTC", "LTCWALLETTEST", -1)
if newbase.AddressExists("Gibson") {
t.Error("Test Failed - portfolio_test.go - AddAddress error")
}
}
func TestUpdatePortfolio(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.UpdatePortfolio(
[]string{"LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL"}, "LTC",
)
if !value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio([]string{"Testy"}, "LTC")
if value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio(
[]string{"LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL", "LVa8wZ983PvWtdwXZ8viK6SocMENLCXkEy"},
"LTC",
)
if !value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio(
[]string{"LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL", "Testy"}, "LTC",
)
if value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio(
[]string{"0xb794f5ea0ba39494ce839613fffba74279579268",
"0xe853c56864a2ebe4576a807d26fdc4a0ada51919"}, "ETH",
)
if !value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio(
[]string{"0xb794f5ea0ba39494ce839613fffba74279579268", "TESTY"}, "ETH",
)
if value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
value = portfolio.UpdatePortfolio(
[]string{PortfolioAddressExchange, PortfolioAddressPersonal}, "LTC")
if !value {
t.Error("Test Failed - portfolio_test.go - UpdatePortfolio error")
}
}
func TestGetPortfolioByExchange(t *testing.T) {
newbase := Base{}
newbase.AddExchangeAddress("ANX", "LTC", 0.07)
newbase.AddExchangeAddress("Bitfinex", "LTC", 0.05)
newbase.AddAddress("someaddress", "LTC", PortfolioAddressPersonal, 0.03)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.GetPortfolioByExchange("ANX")
result, ok := value["LTC"]
if !ok {
t.Error("Test Failed - portfolio_test.go - GetPortfolioByExchange error")
}
if result != 0.07 {
t.Error("Test Failed - portfolio_test.go - GetPortfolioByExchange result != 0.10")
}
value = portfolio.GetPortfolioByExchange("Bitfinex")
result, ok = value["LTC"]
if !ok {
t.Error("Test Failed - portfolio_test.go - GetPortfolioByExchange error")
}
if result != 0.05 {
t.Error("Test Failed - portfolio_test.go - GetPortfolioByExchange result != 0.05")
}
}
func TestGetExchangePortfolio(t *testing.T) {
newbase := Base{}
newbase.AddAddress("ANX", "LTC", PortfolioAddressExchange, 0.03)
newbase.AddAddress("Bitfinex", "LTC", PortfolioAddressExchange, 0.05)
newbase.AddAddress("someaddress", "LTC", PortfolioAddressPersonal, 0.03)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.GetExchangePortfolio()
result, ok := value["LTC"]
if !ok {
t.Error("Test Failed - portfolio_test.go - GetExchangePortfolio error")
}
if result != 0.08 {
t.Error("Test Failed - portfolio_test.go - GetExchangePortfolio result != 0.08")
}
}
func TestGetPersonalPortfolio(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
newbase.AddAddress("anotheraddress", "LTC", "LTCWALLETTEST", 0.03)
newbase.AddAddress("Exchange", "LTC", PortfolioAddressExchange, 0.01)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.GetPersonalPortfolio()
result, ok := value["LTC"]
if !ok {
t.Error("Test Failed - portfolio_test.go - GetPersonalPortfolio error")
}
if result != 0.05 {
t.Error("Test Failed - portfolio_test.go - GetPersonalPortfolio result != 0.05")
}
}
func TestGetPortfolioSummary(t *testing.T) {
newbase := Base{}
// Personal holdings
newbase.AddAddress("someaddress", "LTC", PortfolioAddressPersonal, 1)
newbase.AddAddress("someaddress2", "LTC", PortfolioAddressPersonal, 2)
newbase.AddAddress("someaddress3", "BTC", PortfolioAddressPersonal, 100)
newbase.AddAddress("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", "ETH",
PortfolioAddressPersonal, 865346880000000000)
newbase.AddAddress("0x9edc81c813b26165f607a8d1b8db87a02f34307f", "ETH",
PortfolioAddressPersonal, 165346880000000000)
// Exchange holdings
newbase.AddExchangeAddress("Bitfinex", "LTC", 20)
newbase.AddExchangeAddress("Bitfinex", "BTC", 100)
newbase.AddExchangeAddress("ANX", "ETH", 42)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.GetPortfolioSummary()
getTotalsVal := func(s string) Coin {
for x := range value.Totals {
if value.Totals[x].Coin == s {
return value.Totals[x]
}
}
return Coin{}
}
if getTotalsVal("LTC").Coin != "LTC" {
t.Error("Test Failed - portfolio_test.go - TestGetPortfolioSummary error")
}
if getTotalsVal("ETH").Coin != "ETH" {
t.Error("Test Failed - portfolio_test.go - TestGetPortfolioSummary error")
}
if getTotalsVal("LTC").Balance != 23 {
t.Error("Test Failed - portfolio_test.go - TestGetPortfolioSummary error")
}
if getTotalsVal("BTC").Balance != 200 {
t.Error("Test Failed - portfolio_test.go - TestGetPortfolioSummary error")
}
}
func TestGetPortfolioGroupedCoin(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
newbase.AddAddress("Exchange", "LTC", PortfolioAddressExchange, 0.05)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
value := portfolio.GetPortfolioGroupedCoin()
if value["LTC"][0] != "someaddress" && len(value["LTC"][0]) != 1 {
t.Error("Test Failed - portfolio_test.go - GetPortfolioGroupedCoin error")
}
}
func TestSeedPortfolio(t *testing.T) {
newbase := Base{}
newbase.AddAddress("someaddress", "LTC", "LTCWALLETTEST", 0.02)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newbase)
if !portfolio.AddressExists("someaddress") {
t.Error("Test Failed - portfolio_test.go - SeedPortfolio error")
}
}
func TestStartPortfolioWatcher(t *testing.T) {
newBase := Base{}
newBase.AddAddress("LX2LMYXtuv5tiYEMztSSoEZcafFPYJFRK1", "LTC", PortfolioAddressPersonal, 0.02)
newBase.AddAddress("Testy", "LTC", PortfolioAddressPersonal, 0.02)
portfolio := GetPortfolio()
portfolio.SeedPortfolio(newBase)
if !portfolio.AddressExists("LX2LMYXtuv5tiYEMztSSoEZcafFPYJFRK1") {
t.Error("Test Failed - portfolio_test.go - TestStartPortfolioWatcher")
}
go StartPortfolioWatcher()
}
func TestGetPortfolio(t *testing.T) {
ptrBASE := GetPortfolio()
if reflect.TypeOf(ptrBASE).String() != "*portfolio.Base" {
t.Error("Test Failed - portfolio_test.go - GetoPortfolio error")
}
}