-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperiod_test.go
219 lines (162 loc) · 6.1 KB
/
period_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package period
import (
"github.com/stretchr/testify/assert"
// "github.com/studiofrenetic/period"
// "fmt"
"testing"
"time"
)
func TestCreateFromWeek(t *testing.T) {
StartWeek = time.Monday
p, err := CreateFromWeek(2015, 1)
if err != nil {
assert.Fail(t, "Error create period from week", "OutOfRangeError")
}
startRequested, _ := time.Parse(YMDHIS, "2014-12-29 00:00:00")
endRequested, _ := time.Parse(YMDHIS, "2015-01-05 00:00:00")
assert.Equal(t, p.Start, startRequested, "they should be equal")
assert.Equal(t, p.End, endRequested, "they should be equal")
}
func TestCreateFromMonth(t *testing.T) {
p, err := CreateFromMonth(2015, 3)
if err != nil {
assert.Fail(t, "Error create period from week", "OutOfRangeError")
}
startRequested, _ := time.Parse(YMDHIS, "2015-03-01 00:00:00")
endRequested, _ := time.Parse(YMDHIS, "2015-04-01 00:00:00")
assert.Equal(t, p.Start, startRequested, "they should be equal")
assert.Equal(t, p.End, endRequested, "they should be equal")
}
func TestCreateFromQuarter(t *testing.T) {
p, err := CreateFromQuarter(2015, 1)
if err != nil {
assert.Fail(t, "Error create period from quarter", "OutOfRangeError")
}
expected := Period{
time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2015, 4, 1, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestCreateFromSemester(t *testing.T) {
p, err := CreateFromSemester(2015, 1)
if err != nil {
assert.Fail(t, "Error create period from semester", "OutOfRangeError")
}
expected := Period{
time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2015, 7, 1, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestCreateFromDuration(t *testing.T) {
from := time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC)
duration := time.Duration(24*7) * time.Hour
p := CreateFromDuration(from, duration)
expected := Period{
time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC),
time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestCreateFromDurationBeforeEnd(t *testing.T) {
from := time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC)
duration := time.Duration(24*7) * time.Hour
p := CreateFromDurationBeforeEnd(from, duration)
expected := Period{
time.Date(2015, 5, 18, 0, 0, 0, 0, time.UTC),
time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestNext(t *testing.T) {
from := time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC)
duration := time.Duration(24*2) * time.Hour
p := CreateFromDuration(from, duration)
p.Next()
expected := Period{
time.Date(2015, 5, 27, 0, 0, 0, 0, time.UTC),
time.Date(2015, 5, 29, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestPrevious(t *testing.T) {
from := time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC)
duration := time.Duration(24*2) * time.Hour
p := CreateFromDuration(from, duration)
p.Previous()
expected := Period{
time.Date(2015, 5, 23, 0, 0, 0, 0, time.UTC),
time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, expected, p)
}
func TestDiff(t *testing.T) {
period, err := CreateFromYear(2013)
if err != nil {
t.Fatalf("err : %v\n", err)
}
t.Logf("period: %s", period)
alt := CreateFromDuration(time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC), time.Duration(24*7)*time.Hour)
if err != nil {
t.Fatalf("err : %v\n", err)
}
t.Logf("alt: %s", alt)
diff, err := alt.Diff(period)
if err != nil {
t.Fatalf("err : %v\n", err)
}
t.Logf("diff: %s", diff)
}
func TestSameValueAs(t *testing.T) {
from := time.Date(2015, 5, 25, 0, 0, 0, 0, time.UTC)
duration := time.Duration(24*2) * time.Hour
p := CreateFromDuration(from, duration)
altPeriod := CreateFromDuration(from, duration)
var isSame bool = p == altPeriod
t.Logf("p: %s, is same: %t", p, isSame)
}
func TestDiffWithEqualsPeriod(t *testing.T) {
period, _ := CreateFromYear(2015)
alt := CreateFromDuration(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), (time.Duration(365*24) * time.Hour))
diff, _ := alt.Diff(period)
t.Logf("diff: %v\nalt: %v", diff, alt)
}
func TestDiffWithPeriodSharingOneEndpoints(t *testing.T) {
period, _ := CreateFromYear(2015)
alt := CreateFromDuration(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), (time.Duration(90*24) * time.Hour))
diff, _ := alt.Diff(period)
t.Logf("%#v", diff)
}
func TestDiffWithOverlapsPeriod(t *testing.T) {
period := CreateFromDuration(time.Date(2015, 1, 1, 10, 0, 0, 0, time.UTC), (time.Duration(3) * time.Hour))
alt := CreateFromDuration(time.Date(2015, 1, 1, 11, 0, 0, 0, time.UTC), (time.Duration(3) * time.Hour))
diff, _ := alt.Diff(period)
assert.Len(t, diff, 2, "The size of slice is not 2")
assert.Equal(t, (time.Duration(1) * time.Hour), diff[0].GetDurationInterval(), "first diff should be 1 hour as time.Duration")
assert.Equal(t, (time.Duration(1) * time.Hour), diff[1].GetDurationInterval(), "second diff should be 1 hour as time.Duration")
}
func TestDurationDiff(t *testing.T) {
period := CreateFromDuration(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), (time.Duration(1) * time.Hour))
alt := CreateFromDuration(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
diff := period.DurationDiff(alt)
assert.Equal(t, (time.Duration(-1) * time.Hour), diff, "Should be 1 hour diff")
}
func TestContains(t *testing.T) {
period := CreateFromDuration(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
shouldContains := time.Date(2015, 1, 1, 0, 30, 0, 0, time.UTC)
contains := period.Contains(shouldContains)
assert.Equal(t, true, contains, "Should be true")
}
func TestBefore(t *testing.T) {
period := CreateFromDuration(time.Date(2015, 1, 1, 13, 0, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
alt := CreateFromDuration(time.Date(2015, 1, 1, 15, 30, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
isBefore := period.Before(alt)
assert.Equal(t, true, isBefore, "Should be true")
}
func TestAfter(t *testing.T) {
period := CreateFromDuration(time.Date(2015, 1, 1, 13, 0, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
alt := CreateFromDuration(time.Date(2015, 1, 1, 1, 11, 0, 0, time.UTC), (time.Duration(2) * time.Hour))
isAfter := period.After(alt)
assert.Equal(t, true, isAfter, "Should be true")
}