-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbr_test.go
99 lines (85 loc) · 4.69 KB
/
br_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
package br
import (
"math"
"reflect"
"sort"
"testing"
"time"
)
func Test_newBranch(t *testing.T) {
got := newBranch("b", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
want := newBranch("b", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
if !reflect.DeepEqual(got, want) {
t.Errorf("newBranch() = %v, want %v", got, want)
}
}
func TestBranch_ByBuySorter(t *testing.T) {
got := []Branch{
newBranch("bank", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
sort.Sort(ByBuySorter(got))
want := []Branch{
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
if !reflect.DeepEqual(got, want) {
t.Errorf("branch = %v, want %v", got, want)
}
}
func TestBranch_BySellSorter(t *testing.T) {
got := []Branch{
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
sort.Sort(BySellSorter(got))
want := []Branch{
newBranch("bank", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
if !reflect.DeepEqual(got, want) {
t.Errorf("branch = %v, want %v", got, want)
}
}
func TestRates_String(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Moscow")
b := newBranch(
"Банк «Открытие»",
"м. Октябрьская",
"USD",
79.61,
81.64,
time.Date(2023, time.January, 24, 16, 54, 0, 0, loc))
r := &Branches{}
r.Currency = currency
r.City = Novosibirsk
r.Items = []Branch{b}
got := r.String()
want := `{"currency":"USD","city":"novosibirsk","items":[{` +
`"bank":"Банк «Открытие»","subway":"м. Октябрьская",` +
`"currency":"USD","buy":79.61,"sell":81.64,"updated":"2023-01-24T16:54:00+03:00"}]}`
if got != want {
t.Errorf("got= %v, want= %v", got, want)
}
// Errors
r.Items[0].Buy = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}
r.Items[0].Sell = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}
}