forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs_test.go
785 lines (667 loc) · 18.9 KB
/
pairs_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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
package currency
import (
"encoding/json"
"errors"
"testing"
)
func TestPairsUpper(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
if err != nil {
t.Fatal(err)
}
if expected := "BTC_USD,BTC_AUD,BTC_LTC"; pairs.Upper().Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Upper().Join())
}
}
func TestPairsLower(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"BTC_USD", "BTC_AUD", "BTC_LTC"})
if err != nil {
t.Fatal(err)
}
if expected := "btc_usd,btc_aud,btc_ltc"; pairs.Lower().Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Lower().Join())
}
}
func TestPairsString(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
if err != nil {
t.Fatal(err)
}
expected := []string{"btc_usd", "btc_aud", "btc_ltc"}
for i, p := range pairs {
if p.String() != expected[i] {
t.Errorf("Pairs String() error expected %s but received %s",
expected, p.String())
}
}
}
func TestPairsFromString(t *testing.T) {
t.Parallel()
if _, err := NewPairsFromString("", ""); !errors.Is(err, errNoDelimiter) {
t.Fatalf("received: '%v' but expected: '%v'", err, errNoDelimiter)
}
if _, err := NewPairsFromString("", ","); !errors.Is(err, errCannotCreatePair) {
t.Fatalf("received: '%v' but expected: '%v'", err, errCannotCreatePair)
}
pairs, err := NewPairsFromString("ALGO-AUD,BAT-AUD,BCH-AUD,BSV-AUD,BTC-AUD,COMP-AUD,ENJ-AUD,ETC-AUD,ETH-AUD,ETH-BTC,GNT-AUD,LINK-AUD,LTC-AUD,LTC-BTC,MCAU-AUD,OMG-AUD,POWR-AUD,UNI-AUD,USDT-AUD,XLM-AUD,XRP-AUD,XRP-BTC", ",")
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
expected := []string{"ALGO-AUD", "BAT-AUD", "BCH-AUD", "BSV-AUD", "BTC-AUD",
"COMP-AUD", "ENJ-AUD", "ETC-AUD", "ETH-AUD", "ETH-BTC", "GNT-AUD",
"LINK-AUD", "LTC-AUD", "LTC-BTC", "MCAU-AUD", "OMG-AUD", "POWR-AUD",
"UNI-AUD", "USDT-AUD", "XLM-AUD", "XRP-AUD", "XRP-BTC"}
returned := pairs.Strings()
for x := range returned {
if returned[x] != expected[x] {
t.Fatalf("received: '%v' but expected: '%v'", returned[x], expected[x])
}
}
}
func TestPairsJoin(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
if err != nil {
t.Fatal(err)
}
expected := "btc_usd,btc_aud,btc_ltc"
if pairs.Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Join())
}
}
func TestPairsFormat(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
if err != nil {
t.Fatal(err)
}
expected := "BTC-USD,BTC-AUD,BTC-LTC"
formatting := PairFormat{Delimiter: "-", Index: "", Uppercase: true}
if pairs.Format(formatting).Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Format(formatting).Join())
}
expected = "btc:usd,btc:aud,btc:ltc"
formatting = PairFormat{Delimiter: ":", Index: "", Uppercase: false}
if pairs.Format(formatting).Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Format(formatting).Join())
}
formatting = PairFormat{Delimiter: ":", Index: "KRW", Uppercase: false}
if pairs.Format(formatting).Join() != "" {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Format(formatting).Join())
}
pairs, err = NewPairsFromStrings([]string{"DASHKRW", "BTCKRW"})
if err != nil {
t.Fatal(err)
}
expected = "dash-krw,btc-krw"
formatting = PairFormat{Delimiter: "-", Index: "KRW", Uppercase: false}
if pairs.Format(formatting).Join() != expected {
t.Errorf("Pairs Join() error expected %s but received %s",
expected, pairs.Format(formatting).Join())
}
}
func TestPairsUnmarshalJSON(t *testing.T) {
t.Parallel()
var unmarshalHere Pairs
configPairs := ""
encoded, err := json.Marshal(configPairs)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = json.Unmarshal([]byte{1, 3, 3, 7}, &unmarshalHere)
if err == nil {
t.Fatal("error cannot be nil")
}
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
configPairs = "btc_usd,btc_aud,btc_ltc"
encoded, err = json.Marshal(configPairs)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
if unmarshalHere.Join() != configPairs {
t.Errorf("Pairs UnmarshalJSON() error expected %s but received %s",
configPairs, unmarshalHere.Join())
}
}
func TestPairsMarshalJSON(t *testing.T) {
t.Parallel()
pairs, err := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
if err != nil {
t.Fatal(err)
}
quickstruct := struct {
Pairs Pairs `json:"soManyPairs"`
}{
Pairs: pairs,
}
encoded, err := json.Marshal(quickstruct)
if err != nil {
t.Fatal("Pairs MarshalJSON() error", err)
}
expected := `{"soManyPairs":"btc_usd,btc_aud,btc_ltc"}`
if string(encoded) != expected {
t.Errorf("Pairs MarshalJSON() error expected %s but received %s",
expected, string(encoded))
}
}
func TestRemovePairsByFilter(t *testing.T) {
t.Parallel()
var pairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LTC, USDT),
}
pairs = pairs.RemovePairsByFilter(USDT)
if pairs.Contains(NewPair(LTC, USDT), true) {
t.Error("TestRemovePairsByFilter unexpected result")
}
}
func TestGetPairsByFilter(t *testing.T) {
t.Parallel()
var pairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LTC, USDT),
}
filtered := pairs.GetPairsByFilter(LTC)
if !filtered.Contains(NewPair(LTC, USDT), true) &&
!filtered.Contains(NewPair(LTC, USD), true) {
t.Error("TestRemovePairsByFilter unexpected result")
}
}
func TestRemove(t *testing.T) {
t.Parallel()
var oldPairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LTC, USDT),
}
compare := make(Pairs, len(oldPairs))
copy(compare, oldPairs)
p := NewPair(BTC, USD)
newPairs, err := oldPairs.Remove(p)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
err = compare.ContainsAll(oldPairs, true)
if err != nil {
t.Fatal(err)
}
if newPairs.Contains(p, true) || len(newPairs) != 2 {
t.Error("TestRemove unexpected result")
}
_, err = newPairs.Remove(p)
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrPairNotFound)
}
newPairs, err = oldPairs.Remove(p)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
newPairs, err = newPairs.Remove(NewPair(LTC, USD))
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
err = compare.ContainsAll(oldPairs, true)
if err != nil {
t.Fatal(err)
}
_, err = newPairs.Remove(NewPair(LTC, USD))
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrPairNotFound)
}
newPairs, err = newPairs.Remove(NewPair(LTC, USDT))
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
if len(newPairs) != 0 {
t.Error("unexpected value")
}
_, err = newPairs.Remove(NewPair(LTC, USDT))
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrPairNotFound)
}
}
func TestAdd(t *testing.T) {
t.Parallel()
var pairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LTC, USDT),
}
// Test adding a new pair to the list of pairs
p := NewPair(BTC, USDT)
pairs = pairs.Add(p)
if !pairs.Contains(p, true) || len(pairs) != 4 {
t.Error("TestAdd unexpected result")
}
// Now test adding a pair which already exists
pairs = pairs.Add(p)
if len(pairs) != 4 {
t.Error("TestAdd unexpected result")
}
}
func TestContains(t *testing.T) {
t.Parallel()
var pairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, ZRX),
}
if !pairs.Contains(NewPair(BTC, USD), true) {
t.Errorf("TestContains: Expected pair was not found")
}
if pairs.Contains(NewPair(USD, BTC), true) {
t.Errorf("TestContains: Unexpected pair was found")
}
if !pairs.Contains(NewPair(USD, BTC), false) {
t.Errorf("TestContains: Expected pair was not found")
}
if pairs.Contains(NewPair(ETH, USD), false) {
t.Errorf("TestContains: Non-existent pair was found")
}
}
func TestContainsAll(t *testing.T) {
t.Parallel()
var pairs = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, ZRX),
}
err := pairs.ContainsAll(nil, true)
if !errors.Is(err, errPairsEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, errPairsEmpty)
}
err = pairs.ContainsAll(Pairs{NewPair(BTC, USD)}, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
err = pairs.ContainsAll(Pairs{NewPair(USD, BTC)}, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
err = pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, false)
if !errors.Is(err, ErrPairNotContainedInAvailablePairs) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotContainedInAvailablePairs)
}
err = pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, true)
if !errors.Is(err, ErrPairNotContainedInAvailablePairs) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotContainedInAvailablePairs)
}
err = pairs.ContainsAll(pairs, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
err = pairs.ContainsAll(pairs, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
var duplication = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, ZRX),
NewPair(USD, ZRX),
}
err = pairs.ContainsAll(duplication, false)
if !errors.Is(err, ErrPairDuplication) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairDuplication)
}
}
func TestDeriveFrom(t *testing.T) {
t.Parallel()
_, err := Pairs{}.DeriveFrom("")
if !errors.Is(err, errPairsEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, errPairsEmpty)
}
var testCases = Pairs{
NewPair(BTC, USDT),
NewPair(USDC, USDT),
NewPair(USDC, USD),
NewPair(BTC, LTC),
NewPair(LTC, SAFEMARS),
}
_, err = testCases.DeriveFrom("")
if !errors.Is(err, errSymbolEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, errSymbolEmpty)
}
_, err = testCases.DeriveFrom("btcUSD")
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotFound)
}
got, err := testCases.DeriveFrom("USDCUSD")
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
if got.Upper().String() != "USDCUSD" {
t.Fatalf("received: '%v' but expected: '%v'", got.Upper().String(), "USDCUSD")
}
}
func TestGetCrypto(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
}
contains(t, []Code{BTC, LTC, USDT}, pairs.GetCrypto())
}
func TestGetFiat(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
}
contains(t, []Code{USD, NZD}, pairs.GetFiat())
}
func TestGetCurrencies(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
}
contains(t, []Code{BTC, USD, LTC, NZD, USDT}, pairs.GetCurrencies())
}
func TestGetStables(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(DAI, USDT),
NewPair(LTC, USDC),
NewPair(USDP, USDT),
}
contains(t, []Code{USDT, USDP, USDC, DAI}, pairs.GetStables())
}
func contains(t *testing.T, c1, c2 []Code) {
t.Helper()
codes:
for x := range c1 {
for y := range c2 {
if c1[x].Equal(c2[y]) {
continue codes
}
}
t.Fatalf("cannot find currency %s in returned currency list %v", c1[x], c2)
}
}
// Current: 6176922 260.0 ns/op 48 B/op 1 allocs/op
// Prior: 2575473 474.2 ns/op 112 B/op 3 allocs/op
func BenchmarkGetCrypto(b *testing.B) {
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
}
for x := 0; x < b.N; x++ {
_ = pairs.GetCrypto()
}
}
func TestGetMatch(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
}
_, err := pairs.GetMatch(NewPair(BTC, WABI))
if !errors.Is(err, ErrPairNotFound) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrPairNotFound)
}
expected := NewPair(BTC, USD)
match, err := pairs.GetMatch(expected)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
if !match.Equal(expected) {
t.Fatalf("received: '%v' but expected '%v'", match, expected)
}
match, err = pairs.GetMatch(NewPair(USD, BTC))
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
if !match.Equal(expected) {
t.Fatalf("received: '%v' but expected '%v'", match, expected)
}
}
func TestGetStablesMatch(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
stablePairs := pairs.GetStablesMatch(BTC)
if len(stablePairs) != 0 {
t.Fatal("unexpected value")
}
stablePairs = pairs.GetStablesMatch(USD)
if len(stablePairs) != 0 {
t.Fatal("unexpected value")
}
stablePairs = pairs.GetStablesMatch(LTC)
if len(stablePairs) != 2 {
t.Fatal("unexpected value")
}
if !stablePairs[0].Equal(NewPair(LTC, USDT)) {
t.Fatal("unexpected value")
}
if !stablePairs[1].Equal(NewPair(LTC, DAI)) {
t.Fatal("unexpected value")
}
stablePairs = pairs.GetStablesMatch(XRP)
if len(stablePairs) != 2 {
t.Fatal("unexpected value")
}
if !stablePairs[0].Equal(NewPair(USDT, XRP)) {
t.Fatal("unexpected value")
}
if !stablePairs[1].Equal(NewPair(DAI, XRP)) {
t.Fatal("unexpected value")
}
}
// Current: 5594431 217.4 ns/op 168 B/op 8 allocs/op
// Prev: 3490366 373.4 ns/op 296 B/op 11 allocs/op
func BenchmarkPairsString(b *testing.B) {
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
for x := 0; x < b.N; x++ {
_ = pairs.Strings()
}
}
// Current: 6691011 184.6 ns/op 352 B/op 1 allocs/op
// Prev: 3746151 317.1 ns/op 720 B/op 4 allocs/op
func BenchmarkPairsFormat(b *testing.B) {
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
formatting := PairFormat{Delimiter: "/", Index: "", Uppercase: false}
for x := 0; x < b.N; x++ {
_ = pairs.Format(formatting)
}
}
// current: 13075897 100.4 ns/op 352 B/op 1 allocs/o
// prev: 8188616 148.0 ns/op 336 B/op 3 allocs/op
func BenchmarkRemovePairsByFilter(b *testing.B) {
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
for x := 0; x < b.N; x++ {
_ = pairs.RemovePairsByFilter(USD)
}
}
func TestPairsContainsCurrency(t *testing.T) {
t.Parallel()
pairs := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
if !pairs.ContainsCurrency(BTC) {
t.Fatalf("expected %s to be %v", BTC, true)
}
if !pairs.ContainsCurrency(USD) {
t.Fatalf("expected %s to be %v", USD, true)
}
if !pairs.ContainsCurrency(LTC) {
t.Fatalf("expected %s to be %v", LTC, true)
}
if !pairs.ContainsCurrency(DAI) {
t.Fatalf("expected %s to be %v", DAI, true)
}
if !pairs.ContainsCurrency(XRP) {
t.Fatalf("expected %s to be %v", XRP, true)
}
if pairs.ContainsCurrency(ATOM3L) {
t.Fatalf("expected %s to be %v", ATOM3L, false)
}
}
func TestGetPairsByCurrencies(t *testing.T) {
t.Parallel()
available := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(DAI, XRP),
}
enabled := available.GetPairsByCurrencies(Currencies{USD})
if len(enabled) != 0 {
t.Fatalf("received %v but expected %v", enabled, "no pairs")
}
enabled = available.GetPairsByCurrencies(Currencies{USD, BTC})
if !enabled.Contains(NewPair(BTC, USD), true) {
t.Fatalf("received %v but expected to contain %v", enabled, NewPair(BTC, USD))
}
enabled = available.GetPairsByCurrencies(Currencies{USD, BTC, LTC, NZD, USDT, DAI})
if len(enabled) != 5 {
t.Fatalf("received %v but expected %v", enabled, 5)
}
}
func TestValidateAndConform(t *testing.T) {
t.Parallel()
conformMe := Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
NewPair(EMPTYCODE, EMPTYCODE),
}
_, err := conformMe.ValidateAndConform(EMPTYFORMAT, false)
if !errors.Is(err, ErrCurrencyPairEmpty) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrCurrencyPairEmpty)
}
duplication, err := NewPairFromString("linkusdt")
if err != nil {
t.Fatal(err)
}
conformMe = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LINK, USDT),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
duplication,
}
_, err = conformMe.ValidateAndConform(EMPTYFORMAT, false)
if !errors.Is(err, ErrPairDuplication) {
t.Fatalf("received: '%v' but expected '%v'", err, ErrPairDuplication)
}
conformMe = Pairs{
NewPair(BTC, USD),
NewPair(LTC, USD),
NewPair(LINK, USDT),
NewPair(USD, NZD),
NewPair(LTC, USDT),
NewPair(LTC, DAI),
NewPair(USDT, XRP),
}
formatted, err := conformMe.ValidateAndConform(EMPTYFORMAT, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
expected := "btcusd,ltcusd,linkusdt,usdnzd,ltcusdt,ltcdai,usdtxrp"
if formatted.Join() != expected {
t.Fatalf("received: '%v' but expected '%v'", formatted.Join(), expected)
}
formatted, err = formatted.ValidateAndConform(PairFormat{Delimiter: DashDelimiter, Uppercase: true}, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
expected = "BTC-USD,LTC-USD,LINK-USDT,USD-NZD,LTC-USDT,LTC-DAI,USDT-XRP"
if formatted.Join() != expected {
t.Fatalf("received: '%v' but expected '%v'", formatted.Join(), expected)
}
formatted, err = formatted.ValidateAndConform(PairFormat{
Delimiter: UnderscoreDelimiter,
Uppercase: false},
true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected '%v'", err, nil)
}
expected = "BTC-USD,LTC-USD,LINK-USDT,USD-NZD,LTC-USDT,LTC-DAI,USDT-XRP"
if formatted.Join() != expected {
t.Fatalf("received: '%v' but expected '%v'", formatted.Join(), expected)
}
}