-
Notifications
You must be signed in to change notification settings - Fork 6
/
exchange_rates_test.go
152 lines (107 loc) · 2.9 KB
/
exchange_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
package finance_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/pieterclaerhout/go-finance"
)
func TestExchangeRatesValid(t *testing.T) {
rates, err := finance.ExchangeRates()
assert.NoErrorf(t, err, "err should be nil, is: %v", err)
assert.NotNilf(t, rates, "rates should not be nil")
assert.NotEmpty(t, rates)
}
func TestExchangeRatesInvalidURL(t *testing.T) {
finance.RatesURL = "ht&@-tp://:aa"
defer resetRatesURL()
rates, err := finance.ExchangeRates()
assert.Error(t, err)
assert.Empty(t, rates)
}
func TestExchangeRatesTimeout(t *testing.T) {
finance.DefaultTimeout = 250 * time.Millisecond
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(500 * time.Millisecond)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("hello"))
}),
)
defer s.Close()
finance.RatesURL = s.URL
defer resetRatesURL()
rates, err := finance.ExchangeRates()
assert.Error(t, err)
assert.Empty(t, rates)
}
func TestExchangeRatesReadBodyError(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "1")
}),
)
defer s.Close()
finance.RatesURL = s.URL
defer resetRatesURL()
rates, err := finance.ExchangeRates()
assert.Error(t, err)
assert.Empty(t, rates)
}
func TestExchangeRatesInvalidXML(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("hello"))
}),
)
defer s.Close()
finance.RatesURL = s.URL
defer resetRatesURL()
rates, err := finance.ExchangeRates()
assert.Error(t, err)
assert.Empty(t, rates)
}
func TestConvertRate(t *testing.T) {
type test struct {
name string
value float64
from string
to string
expectsError bool
}
var tests = []test{
{"invalid", 1, "", "", true},
{"invalid-from", 1, "EUR", "", true},
{"invalid-to", 1, "", "EUR", true},
{"valid-eur-eur", 2, "EUR", "EUR", false},
{"valid-eur-usd", 2, "EUR", "USD", false},
{"valid-aud-usd", 2, "AUD", "USD", false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual, err := finance.ConvertRate(tc.value, tc.from, tc.to)
if tc.expectsError {
assert.Zero(t, actual, "actual")
assert.Error(t, err, "error")
} else {
assert.NotZero(t, actual, "actual")
assert.NoError(t, err, "error")
if tc.from != tc.to {
assert.NotEqual(t, tc.value, actual, "should-not-be-equal")
}
}
})
}
}
func TestConvertRateInvalidURL(t *testing.T) {
finance.RatesURL = "ht&@-tp://:aa"
defer resetRatesURL()
actual, err := finance.ConvertRate(1, "EUR", "USD")
assert.Zero(t, actual, "actual")
assert.Error(t, err, "error")
}
func resetRatesURL() {
finance.RatesURL = finance.DefaultRatesURL
}