-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_test.go
118 lines (112 loc) · 3.77 KB
/
window_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
package window
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/araddon/dateparse"
)
func Test(t *testing.T) {
now := dateparse.MustParse("1 May 2022 00:00:00")
type test struct {
text string
windowFunc func() Window
}
tests := []test{
// 1. Abs-Abs
{"1 April 2022 to 2 April 2022", func() Window {
d1 := dateparse.MustParse("01 Apr 2022 00:00:00.000000000")
d2 := dateparse.MustParse("2 Apr 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
// 2. Abs-Rel
{"1 April 2022 within 1 day", func() Window {
d1 := dateparse.MustParse("01 Apr 2022 00:00:00.000000000")
d2 := dateparse.MustParse("2 Apr 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
// 3. Abs-RelN
{"1 April 2022 to tomorrow", func() Window {
d1 := dateparse.MustParse("1 Apr 2022 00:00:00")
d2 := dateparse.MustParse("2 May 2022 00:00:00")
return Window{from: &d1, to: &d2}
}},
// 4. Rel-Abs
{"1 day to 2 April 2022", func() Window {
d1 := dateparse.MustParse("01 Apr 2022 00:00:00.000000000")
d2 := dateparse.MustParse("2 Apr 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
// 5. Rel-Rel (Sliding window)
{"30 days", func() Window {
return Window{slide: 30 * 24 * time.Hour}
}},
// 6. Rel-RelN
{"2 days to next week", func() Window {
d1 := dateparse.MustParse("30 Apr 2022 00:00:00.000000000")
d2 := dateparse.MustParse("2 May 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
{"1 days to next day", func() Window {
d1 := dateparse.MustParse("1 May 2022 00:00:00.000000000")
d2 := dateparse.MustParse("2 May 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
{"1 days to next month", func() Window {
d1 := dateparse.MustParse("31 May 2022 00:00:00.000000000")
d2 := dateparse.MustParse("1 Jun 2022 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
{"30 days to next year", func() Window {
d1 := dateparse.MustParse("2 Dec 2022 00:00:00.000000000")
d2 := dateparse.MustParse("1 Jan 2023 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
// 7. RelN-Abs
{"next year to 20 May 2024", func() Window {
d1 := dateparse.MustParse("31 Dec 2023 23:59:59.999999999")
d2 := dateparse.MustParse("20 May 2024 00:00:00.000000000")
return Window{from: &d1, to: &d2}
}},
// 8. RelN-Rel
{"next year within 3 days and 2 hours", func() Window {
d1 := dateparse.MustParse("31 Dec 2023 23:59:59.999999999")
d2 := dateparse.MustParse("4 Jan 2024 01:59:59.999999999")
return Window{from: &d1, to: &d2}
}},
{"today within 1 minute", func() Window {
d1 := dateparse.MustParse("1 May 2022 23:59:59.999999999")
d2 := dateparse.MustParse("2 May 2022 00:00:59.999999999")
return Window{from: &d1, to: &d2}
}},
// 9. RelN-RelN
{"last hour to next minute", func() Window {
d1 := dateparse.MustParse("30 Apr 2022 23:59:59.999999999")
d2 := dateparse.MustParse("1 May 2022 00:01:00.000000000")
return Window{from: &d1, to: &d2}
}},
{"last second to next millisecond", func() Window {
d1 := dateparse.MustParse("30 Apr 2022 23:59:59.999999999")
d2 := dateparse.MustParse("1 May 2022 00:00:00.001000000")
return Window{from: &d1, to: &d2}
}},
{"last microsecond to next nanosecond", func() Window {
d1 := dateparse.MustParse("30 Apr 2022 23:59:59.999999999")
d2 := dateparse.MustParse("1 May 2022 00:00:00.000000001")
return Window{from: &d1, to: &d2}
}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
winSpec, err := Start(tt.text)
if err != nil {
t.Fatal(err)
}
win := winSpec.ResolveAt(now)
expectedWin := tt.windowFunc()
if !reflect.DeepEqual(win, &expectedWin) {
t.Errorf("window [%v] should be [%v]", win, tt.windowFunc())
}
})
}
}