-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
155 lines (118 loc) · 3.84 KB
/
client_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
package gofixerio_test
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
gofixerio "github.com/LordotU/go-fixerio"
)
var referenceSymbols = []string{
"USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "CNY", "NZD", "RUB",
}
func getClient() (*gofixerio.FixerIO, error) {
APIKey := os.Getenv("FIXERIO_API_KEY")
if APIKey == "" {
fmt.Println("APIKey shoud be specified for tests running")
os.Exit(1)
}
return gofixerio.New(
APIKey,
"eur",
false,
)
}
func TestNew(t *testing.T) {
_, err := gofixerio.New("", "", false)
assert.EqualError(t, err, "gofixerio error: APIKey is required")
_, err = gofixerio.New("186f2ecd6621d35c3319bb53e1787d90", "", false)
assert.Nil(t, err)
}
func TestSetBase(t *testing.T) {
client, _ := getClient()
assert.Equal(t, "EUR", client.Base)
client.SetBase("usd")
assert.Equal(t, "USD", client.Base)
}
func TestGetSymbols(t *testing.T) {
client, _ := getClient()
response, err := client.GetSymbols()
if err != nil {
t.Error(err)
}
for _, symbol := range referenceSymbols {
if _, ok := response.Symbols[symbol]; !ok {
t.Errorf("Reference symbol %s is absent in response %+v", symbol, response.Symbols)
}
}
}
func TestGetLatest(t *testing.T) {
client, _ := getClient()
response, err := client.GetLatest([]string{})
if err != nil {
t.Error(err)
}
assert.Equal(t, client.Base, response.Base)
assert.True(t, regexp.MustCompile(`^\d+$`).MatchString(strconv.Itoa(response.Timestamp)))
_, err = time.Parse("2006-01-02", response.Date)
assert.Nil(t, err)
assert.True(t, len(response.Rates) > 0)
}
func TestGetLatest_WithSymbols(t *testing.T) {
client, _ := getClient()
response, err := client.GetLatest(referenceSymbols)
if err != nil {
t.Error(err)
}
assert.True(t, len(response.Rates) == len(referenceSymbols))
}
func TestGetHistorical(t *testing.T) {
client, _ := getClient()
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
response, err := client.GetHistorical(yesterday, []string{})
if err != nil {
t.Error(err)
}
assert.Equal(t, yesterday, response.Date)
}
func TestGetHistorical_WithSymbols(t *testing.T) {
client, _ := getClient()
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
response, err := client.GetHistorical(yesterday, referenceSymbols)
if err != nil {
t.Error(err)
}
assert.True(t, len(response.Rates) == len(referenceSymbols))
}
func TestGetConversion(t *testing.T) {
client, _ := getClient()
amount := float64(100)
date := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
_, err := client.GetConversion(referenceSymbols[0], referenceSymbols[1], amount, date)
assert.EqualError(t, err, "gofixerio error: fixer.io request error with info: Access Restricted - Your current Subscription Plan does not support this API Function.")
}
func TestGetTimeseries(t *testing.T) {
client, _ := getClient()
beforeYesterday := time.Now().AddDate(0, 0, -2).Format("2006-01-02")
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
_, err := client.GetTimeseries(beforeYesterday, yesterday)
assert.EqualError(t, err, "gofixerio error: fixer.io request error with info: Access Restricted - Your current Subscription Plan does not support this API Function.")
}
func TestGetFluctuation(t *testing.T) {
client, _ := getClient()
beforeYesterday := time.Now().AddDate(0, 0, -2).Format("2006-01-02")
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
_, err := client.GetFluctuation(beforeYesterday, yesterday)
assert.EqualError(t, err, "gofixerio error: fixer.io request error with info: Access Restricted - Your current Subscription Plan does not support this API Function.")
}
func ExampleNew() {
fixerio, _ := gofixerio.New("your API key here", "EUR", false)
latestRates, err := fixerio.GetLatest([]string{})
if err != nil {
log.Panic(err)
}
log.Printf("%+v", latestRates)
}