This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
fiatFeedOxr_test.go
127 lines (104 loc) · 3.45 KB
/
fiatFeedOxr_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
package plugins
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stellar/kelp/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_GetPrice_ShouldReturnZero_ClientError(t *testing.T) {
oxrFeed := newFiatFeedOxr(tests.RandomString())
price, err := oxrFeed.GetPrice()
assert.Equal(t, price, float64(0))
assert.Contains(t, err.Error(), "oxr: error ")
}
func Test_GetPrice_ShouldReturnZero_OxrError(t *testing.T) {
keys := make([]int, 0, len(oxrErrorCodeMsg))
for k, _ := range oxrErrorCodeMsg {
keys = append(keys, k)
}
expected := oxrError{
Err: true,
Status: keys[tests.RandomIntWithMax(len(keys))],
Message: tests.RandomString(),
Description: tests.RandomString(),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(expected.Status)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(expected)
require.NoError(t, err)
}))
defer ts.Close()
oxrFeed := newFiatFeedOxr(ts.URL)
price, err := oxrFeed.GetPrice()
assert.Equal(t, float64(0), price)
assert.Equal(t, expected, err)
}
func Test_GetPrice_ShouldReturnInvalidRateLength(t *testing.T) {
response := createOxrResponse("")
response.Rates = nil
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()
oxrFeed := newFiatFeedOxr(ts.URL)
expected, err := oxrFeed.GetPrice()
assert.Equal(t, float64(0), expected)
assert.Equal(t, fmt.Errorf("oxr: error rates must contain single value found len %d", len(response.Rates)), err)
}
func Test_GetPrice_ShouldReturnRates(t *testing.T) {
symbol := tests.RandomString()
response := createOxrResponse(symbol)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()
oxrFeed := newFiatFeedOxr(ts.URL)
price, err := oxrFeed.GetPrice()
assert.Equal(t, response.Rates[symbol], price)
assert.NoError(t, err)
}
func Test_GetPrice_ShouldReturnZero_ClientInvalidResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write(nil)
}))
defer ts.Close()
oxrFeed := newFiatFeedOxr(ts.URL)
price, err := oxrFeed.GetPrice()
assert.Equal(t, float64(0), price)
assert.Contains(t, err.Error(), "oxr: error ")
}
func Test_GetPrice_ShouldReturnZero_ClientInvalidOXRRate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode("{ bad_json: `invalid_format' }")
require.NoError(t, err)
}))
defer ts.Close()
oxrFeed := newFiatFeedOxr(ts.URL)
price, err := oxrFeed.GetPrice()
assert.Equal(t, float64(0), price)
assert.Contains(t, err.Error(), "oxr: error ")
}
func createOxrResponse(symbol string) oxrRates {
return oxrRates{
Disclaimer: tests.RandomString(),
License: tests.RandomString(),
Timestamp: int64(tests.RandomInt()),
Base: tests.RandomString(),
Rates: map[string]float64{
symbol: float64(tests.RandomInt()),
},
}
}