-
Notifications
You must be signed in to change notification settings - Fork 0
/
fund_test.go
123 lines (103 loc) · 2.88 KB
/
fund_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
package pensiondata
import (
"errors"
"reflect"
"testing"
"time"
)
func TestGetFundByISIN(t *testing.T) {
t.Run("return fund successfully", func(t *testing.T) {
date, _ := time.Parse("2006-02-01", "2020-07-07")
want := Fund{
Isin: "BE123",
Name: "First Fund",
Bank: "Banka",
LaunchDate: date,
Currency: "EUR",
}
r := FundRepositoryMock{}
r.FindByISINFn = func(isin string) (Fund, error) {
return want, nil
}
fundService := NewFundService(r)
got, _ := fundService.GetFundByISIN("BE123")
if !reflect.DeepEqual(newPublicFund(want), got) {
t.Errorf("want %v, got %v", newPublicFund(want), got)
}
})
t.Run("return error", func(t *testing.T) {
r := FundRepositoryMock{}
r.FindByISINFn = func(isin string) (Fund, error) {
return Fund{}, errors.New("error")
}
fundService := NewFundService(r)
_, err := fundService.GetFundByISIN("BE123")
if err == nil {
t.Errorf("want error")
}
})
}
func TestGetFunds(t *testing.T) {
t.Run("return funds successfully", func(t *testing.T) {
date, _ := time.Parse("2006-02-01", "2020-07-07")
wants := []Fund{
{Isin: "BE123", Name: "First Fund", Bank: "Banka", LaunchDate: date, Currency: "EUR"},
{Isin: "LU123", Name: "Second Fund", Bank: "Banko", LaunchDate: date, Currency: "EUR"},
}
r := FundRepositoryMock{}
r.FindAllFn = func() ([]Fund, error) {
return wants, nil
}
fundService := NewFundService(r)
got, _ := fundService.GetFunds()
if len(wants) != len(got) {
t.Errorf("want %d, got %d", len(wants), len(got))
}
var wantPublicFunds []PublicFund
for _, want := range wants {
wantPublicFunds = append(wantPublicFunds, newPublicFund(want))
}
if !reflect.DeepEqual(wantPublicFunds, got) {
t.Errorf("want %v, got %v", wantPublicFunds, got)
}
})
t.Run("return error", func(t *testing.T) {
r := FundRepositoryMock{}
r.FindAllFn = func() ([]Fund, error) {
return []Fund{}, errors.New("error")
}
fundService := NewFundService(r)
_, err := fundService.GetFunds()
if err == nil {
t.Errorf("want error")
}
})
}
func TestNewPublicFund(t *testing.T) {
t.Run("return correctly formatted PublicFund", func(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2020-06-27")
want := Fund{
Isin: "BE123",
Name: "First Fund",
Bank: "Banka",
LaunchDate: date,
Currency: "EUR",
}
got := newPublicFund(want)
if want.Isin != got.Isin {
t.Errorf("want %s, got %s", want.Isin, got.Isin)
}
if want.Name != got.Name {
t.Errorf("want %s, got %s", want.Name, got.Name)
}
if want.Bank != got.Bank {
t.Errorf("want %s, got %s", want.Bank, got.Bank)
}
if want.LaunchDate.Format("2006-01-02") != got.LaunchDate {
t.Errorf("want %s, got -%s", want.LaunchDate.Format("2006-01-02"), got.LaunchDate)
}
if want.Currency != got.Currency {
t.Errorf("want %s, got %s", want.Currency, got.Currency)
}
})
}