-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloan_test.go
138 lines (111 loc) · 3.31 KB
/
loan_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
package lendinvest
import (
"errors"
"testing"
"github.com/Sieciechu/lendinvest/calendar"
)
var date = calendar.NewDate
type InvestorMock struct{}
func (f *InvestorMock) LendMoney(money Cash) (Cash, error) {
return 0, errors.New("Cannot lend money")
}
func (f *InvestorMock) TakeMoney(money Cash) {}
func TestWhenInvestorCannotLendMoneyThenInvestmentShouldNotBeMade(t *testing.T) {
// given
john := InvestorMock{}
trancheA := tranche{
id: "A",
maximumInvestment: 2000,
investmentsLeft: 2000,
monthlyInterestPercentage: 3,
investments: nil,
}
investmentRequest := InvestmentRequest{
&john, 2000, 0, "A", date("2019-01-01"), date("2019-01-31"),
}
// when
investment, err := trancheA.makeInvestment(investmentRequest)
// then
if nil == err || nil != investment {
t.Errorf("Expected error, but got investment")
}
}
func TestWhenInvestmentExceedsTrancheAvailableInvestmentsItShouldBeNotAllowedToInvest(t *testing.T) {
// given
john := User{1000}
trancheA := tranche{
id: "A",
maximumInvestment: 2000,
investmentsLeft: 30,
monthlyInterestPercentage: 3,
investments: nil,
}
investmentRequest := InvestmentRequest{
&john, 1000, 0, "A", date("2019-01-01"), date("2019-01-31"),
}
// when
investment, err := trancheA.makeInvestment(investmentRequest)
// then
if nil == err || nil != investment {
t.Errorf("Expected error, but got investment")
}
}
func TestTrancheMakeInvestment(t *testing.T) {
// given
john := User{1000}
trancheA := tranche{
id: "A",
maximumInvestment: 2000,
investmentsLeft: 1500,
monthlyInterestPercentage: 3,
investments: nil,
}
investmentRequest := InvestmentRequest{
&john, 1000, 0, "A", date("2019-01-01"), date("2019-01-31"),
}
// when
investment, err := trancheA.makeInvestment(investmentRequest)
// then
if nil != err || nil == investment {
t.Errorf("Expected investment, but got error")
}
if investment.investedMoney != investmentRequest.MoneyToInvest {
t.Errorf("Expected to invest %s money, but %s was invested",
investmentRequest.MoneyToInvest, investment.investedMoney)
}
if investment.investor != investmentRequest.Inv {
t.Errorf("Investition was made by someone else")
}
if investment.startDate != investmentRequest.StartDate {
t.Errorf("Expected to start invest on %s, but is started on %s",
investmentRequest.StartDate, investment.startDate)
}
if investment.endDate != investmentRequest.EndDate {
t.Errorf("Expected to end invest on %s, but is ended on %s",
investmentRequest.EndDate, investment.endDate)
}
}
func TestCheckInvestmentDates(t *testing.T) {
// given
l := loan{start: date("2019-01-01"), end: date("2019-02-16")}
cases := []struct {
start string
end string
expectedResult bool
}{
{"2019-01-01", "2019-01-01", true},
{"2019-01-01", "2019-01-17", true},
{"2019-01-01", "2019-02-16", true},
{"2019-01-01", "2019-02-17", false},
{"2018-12-31", "2019-02-16", false},
{"2018-12-31", "2019-02-17", false},
}
for _, c := range cases {
// when
ok, _ := l.checkInvestmentDates(date(c.start), date(c.end))
// then
if ok != c.expectedResult {
t.Errorf("Expected %v, but got %v", c.expectedResult, ok)
}
}
}