-
Notifications
You must be signed in to change notification settings - Fork 3
/
exbrates_test.go
114 lines (107 loc) · 2.45 KB
/
exbrates_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
package ecbrates
import (
"strconv"
"testing"
)
func TestFetchExchangeRates(t *testing.T) {
r, err := New()
if err != nil {
t.Error(err)
}
if r.Date == "" {
t.Error("Date is empty")
}
for currency, rate := range r.Rate {
str, ok := rate.(string)
if !ok {
t.Error("Parse string error:", err)
}
if !currency.IsValid() {
t.Error("Unknown currency type", currency)
}
v, err := strconv.ParseFloat(str, 32)
if !ok {
t.Error("Parse float error:", err)
}
expected := round64(100.0*round64(v, 4), 4)
value, err := r.Convert(100, EUR, currency)
if err != nil {
t.Error("Converting error:", err)
}
if expected != value {
t.Error("Expected rate", expected, "got", value)
}
}
if _, err = r.Convert(100, Currency("XXX"), EUR); err == nil {
t.Error("Expected error, got nil")
}
if _, err = r.Convert(100, EUR, Currency("XXX")); err == nil {
t.Error("Expected error, got nil")
}
if _, err = r.Convert(100, Currency("XXX"), Currency("XXX")); err == nil {
t.Error("Expected error, got nil")
}
}
func TestFetch90DaysExchangeRates(t *testing.T) {
rates, err := Load()
if err != nil {
t.Error(err)
}
if len(rates) < 50 {
t.Error("Insufficient Count of days, got", len(rates))
}
if len(rates) > 90 {
t.Error("Too big Count of days, got", len(rates))
}
for _, item := range rates {
if item.Date == "" {
t.Error("Date is empty")
}
for currency, rate := range item.Rate {
if !currency.IsValid() {
t.Error("Unknown currency type", currency)
}
if str, ok := rate.(string); ok {
if v, err := strconv.ParseFloat(str, 32); err == nil {
if v == 0 {
t.Error("Day:", item.Date, "Zero rate for", currency)
}
} else {
t.Error(err)
}
} else {
t.Error("Parse rate to string unsuccessful")
}
}
}
}
func TestFetchAllExchangeRates(t *testing.T) {
rates, err := LoadAll()
if err != nil {
t.Error(err)
}
if len(rates) <= 90 {
t.Error("Insufficient Count of days, got", len(rates))
}
for _, item := range rates {
if item.Date == "" {
t.Error("Date is empty")
}
for currency, rate := range item.Rate {
if !currency.IsValid() {
t.Error("Unknown currency type", currency)
}
if str, ok := rate.(string); ok {
if v, err := strconv.ParseFloat(str, 32); err == nil {
if v == 0 {
t.Error("Day:", item.Date, "Zero rate for", currency)
}
} else {
t.Error(err)
}
} else {
t.Error("Parse rate to string unsuccessful")
}
}
}
}