-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rates_test.go
95 lines (77 loc) · 3.03 KB
/
rates_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
package bsvrates
import (
"context"
"testing"
"github.com/mrz1836/go-whatsonchain"
"github.com/stretchr/testify/assert"
)
// newMockClient returns a client for mocking
func newMockClient(wocClient whatsonchain.ChainService, paprikaClient CoinPaprikaInterface,
providers ...Provider) ClientInterface {
client := NewClient(nil, nil, providers...)
client.SetWhatsOnChain(wocClient)
client.SetCoinPaprika(paprikaClient)
return client
}
// TestClient_GetRate will test the method GetRate()
func TestClient_GetRate(t *testing.T) {
// t.Parallel()
t.Run("valid get rate - default", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{})
assert.NotNil(t, client)
rate, provider, err := client.GetRate(context.Background(), CurrencyDollars)
assert.NoError(t, err)
assert.Equal(t, 158.49415248, rate)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "CoinPaprika", provider.Name())
})
t.Run("valid get rate - whats on chain", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, ProviderWhatsOnChain)
assert.NotNil(t, client)
rate, provider, err := client.GetRate(context.Background(), CurrencyDollars)
assert.NoError(t, err)
assert.Equal(t, 159.01, rate)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "WhatsOnChain", provider.Name())
})
t.Run("valid get rate - custom providers", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, ProviderCoinPaprika, ProviderWhatsOnChain)
assert.NotNil(t, client)
rate, provider, err := client.GetRate(context.Background(), CurrencyDollars)
assert.NoError(t, err)
assert.Equal(t, 158.49415248, rate)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "CoinPaprika", provider.Name())
})
t.Run("non accepted currency", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{})
assert.NotNil(t, client)
_, _, rateErr := client.GetRate(context.Background(), 123)
assert.Error(t, rateErr)
})
t.Run("failed rate - default", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{})
assert.NotNil(t, client)
rate, provider, err := client.GetRate(context.Background(), CurrencyDollars)
assert.NoError(t, err)
assert.Equal(t, 159.01, rate)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "WhatsOnChain", provider.Name())
})
t.Run("failed rate - whats on chain", func(t *testing.T) {
client := newMockClient(&mockWOCFailed{}, &mockPaprikaValid{})
assert.NotNil(t, client)
rate, provider, err := client.GetRate(context.Background(), CurrencyDollars)
assert.NoError(t, err)
assert.Equal(t, 158.49415248, rate)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "CoinPaprika", provider.Name())
})
t.Run("failed rate - all providers", func(t *testing.T) {
client := newMockClient(&mockWOCFailed{}, &mockPaprikaFailed{})
assert.NotNil(t, client)
rate, _, err := client.GetRate(context.Background(), CurrencyDollars)
assert.Error(t, err)
assert.Equal(t, float64(0), rate)
})
}