-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiod_rule_test.go
121 lines (114 loc) · 4.11 KB
/
period_rule_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
// Copyright © 2020 Danila Petrunko. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rdate
import (
"testing"
"time"
)
func TestPeriodRules_Perform(t *testing.T) {
testCases := []struct {
name string
rule PeriodRule
date time.Time
expectedFrom time.Time
expectedTo time.Time
}{
{
name: "ThisDay",
rule: &periodRuleThisDay{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 12, 11, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 11, 23, 59, 59, 999999999, time.UTC),
},
{
name: "ThisWeek",
rule: &periodRuleThisWeek{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 12, 9, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 15, 23, 59, 59, 999999999, time.UTC),
},
{
name: "ThisMonth",
rule: &periodRuleThisMonth{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 12, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "ThisQuart",
rule: &periodRuleThisQuart{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 10, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "ThisHalfYear",
rule: &periodRuleThisHalfYear{},
date: time.Date(2019, 8, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "ThisYear",
rule: &periodRuleThisYear{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevDay",
rule: &periodRulePrevDay{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 12, 10, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 10, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevWeek",
rule: &periodRulePrevWeek{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 12, 2, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 12, 8, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevMonth",
rule: &periodRulePrevMonth{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 11, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 11, 30, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevQuart",
rule: &periodRulePrevQuart{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 9, 30, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevHalfYear",
rule: &periodRulePrevHalfYear{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2019, 6, 30, 23, 59, 59, 999999999, time.UTC),
},
{
name: "PrevYear",
rule: &periodRulePrevYear{},
date: time.Date(2019, 12, 11, 0, 2, 1, 6, time.UTC),
expectedFrom: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
expectedTo: time.Date(2018, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
}
tf := NewTimeFactory()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualFrom, actualTo := tc.rule.Calculate(tc.date, tf)
if !actualFrom.Time().Equal(tc.expectedFrom) {
t.Errorf("Period.From = %s; expected %s", actualFrom.Time(), tc.expectedFrom)
}
if !actualTo.Time().Equal(tc.expectedTo) {
t.Errorf("Period.To = %s; expected %s", actualTo.Time(), tc.expectedTo)
}
})
}
}