-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvestment_test.go
120 lines (100 loc) · 2.88 KB
/
investment_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
package lendinvest
import (
"testing"
"time"
"github.com/Sieciechu/lendinvest/calendar"
)
func TestCalculateNumberOfPaychecks(t *testing.T) {
date := calendar.NewDate
cases := []struct {
start time.Time
end time.Time
expectedNumber int
}{
{date("2019-01-01"), date("2019-01-01"), 1},
{date("2019-01-31"), date("2019-01-31"), 1},
{date("2019-01-16"), date("2019-02-01"), 2},
{date("2019-01-01"), date("2019-02-01"), 2},
{date("2019-01-01"), date("2019-01-31"), 1},
{date("2019-10-02"), date("2019-11-15"), 2},
{date("2019-10-02"), date("2020-11-15"), 14},
}
for _, c := range cases {
got := calculateNumberOfPaychecks(c.start, c.end)
if got != c.expectedNumber {
t.Errorf("calculateNumberOfPaychecks(%q, %q), got %d , want %d",
c.start, c.end, got, c.expectedNumber)
}
}
}
func TestGetNearestPeriodEndDate(t *testing.T) {
date := calendar.NewDate
cases := []struct {
start time.Time
end time.Time
expected time.Time
}{
{date("2019-07-01"), date("2019-07-01"), date("2019-07-01")},
{date("2019-07-04"), date("2019-07-15"), date("2019-07-15")},
{date("2019-07-04"), date("2019-08-15"), date("2019-07-31")},
{date("2019-07-31"), date("2019-08-01"), date("2019-07-31")},
{date("2019-07-07"), date("2019-12-01"), date("2019-07-31")},
}
for _, c := range cases {
got := getNearestPeriodEndDate(c.start, c.end)
if !c.expected.Equal(got) {
t.Errorf("Expected %q, but got %q", c.expected, got)
}
}
}
func TestCalculateMoneyToPayForPeriod(t *testing.T) {
date := calendar.NewDate
cases := []struct {
money Cash
start, end time.Time
percent uint
expectedIncome Cash
}{
{1000, date("2019-10-03"), date("2019-11-15"), 3, 28.06},
{500, date("2019-10-10"), date("2019-11-15"), 6, 21.29},
}
_ = cases
// given
for _, c := range cases {
i := investment{
investedMoney: c.money,
startDate: c.start,
endDate: c.end,
monthlyInterestPercetage: c.percent,
}
// when
moneyForFirstMonth := i.calculateMoneyToPayForPeriod(c.start, date("2019-10-31"))
// then
if moneyForFirstMonth != c.expectedIncome {
t.Errorf("Expected %f, but got %f", c.expectedIncome, moneyForFirstMonth)
}
}
}
func TestCalculatePaychecks(t *testing.T) {
date := calendar.NewDate
// given
i := investment{
investedMoney: 1000,
startDate: date("2019-10-03"),
endDate: date("2019-11-15"),
monthlyInterestPercetage: 3,
paychecks: nil,
}
// when
i.calculatePaychecks()
// then
if n := len(i.paychecks); n != 3 {
t.Errorf("Expected 3 paychecks, but got %d", n)
}
if m := i.paychecks[0].moneyToPay; m != 28.06 {
t.Errorf("Expected 28.06 for first paycheck, but got %s", m)
}
if m := i.paychecks[2].moneyToPay; m != 1000 {
t.Errorf("Expected to return the invested 1000, but got %s", m)
}
}