-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rates_test.go
188 lines (158 loc) · 4.72 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
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
package tonicpow
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// newTestRate creates a dummy profile for testing
func newTestRate() *Rate {
return &Rate{
Currency: testRateCurrency,
CurrencyAmount: 0.01,
PriceInSatoshis: 4200,
}
}
// TestClient_GetCurrentRate will test the method GetCurrentRate()
func TestClient_GetCurrentRate(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("get current rate (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
rates := newTestRate()
endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates)
assert.NoError(t, err)
var currentRate *Rate
var response *StandardResponse
currentRate, response, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.NoError(t, err)
assert.NotNil(t, currentRate)
assert.NotNil(t, response)
assert.Equal(t, testRateCurrency, currentRate.Currency)
})
t.Run("missing currency", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
rates := newTestRate()
endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates)
assert.NoError(t, err)
var currentRate *Rate
var response *StandardResponse
currentRate, response, err = client.GetCurrentRate("", 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
assert.Nil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
rates := newTestRate()
endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, rates)
assert.NoError(t, err)
var currentRate *Rate
var response *StandardResponse
currentRate, response, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
assert.NotNil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)
apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)
var currentRate *Rate
var response *StandardResponse
currentRate, response, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
assert.NotNil(t, response)
assert.Equal(t, apiError.Message, err.Error())
})
}
// ExampleClient_GetCurrentRate example using GetCurrentRate()
//
// See more examples in /examples/
func ExampleClient_GetCurrentRate() {
// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
// For mocking
rates := newTestRate()
// Mock response (for example only)
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
),
http.StatusOK,
rates,
)
// Get rate (using mocking response)
var currentRate *Rate
if currentRate, _, err = client.GetCurrentRate(
testRateCurrency, 0.00,
); err != nil {
fmt.Printf("error getting profile: " + err.Error())
return
}
fmt.Printf("current rate: %s %f usd is %d sats", currentRate.Currency, currentRate.CurrencyAmount, currentRate.PriceInSatoshis)
// Output:current rate: usd 0.010000 usd is 4200 sats
}
// BenchmarkClient_GetCurrentRate benchmarks the method GetCurrentRate()
func BenchmarkClient_GetCurrentRate(b *testing.B) {
client, _ := newTestClient()
rate := newTestRate()
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
),
http.StatusOK,
rate,
)
for i := 0; i < b.N; i++ {
_, _, _ = client.GetCurrentRate(testRateCurrency, 0.00)
}
}