-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-range_test.go
168 lines (162 loc) · 4.44 KB
/
time-range_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
package main
import (
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"reflect"
"strings"
"testing"
"time"
)
const layout = "2006-01-02T15:04:05.000Z"
func Test_generateRangeTable(t *testing.T) {
type args struct {
r v1.Range
step int
}
tests := []struct {
name string
args args
want []v1.Range
}{
{name: "one day", args: args{
r: v1.Range{
Start: time.Date(2021, 1, 10, 00, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 11, 14, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
step: 15,
}, want: []v1.Range{
{
Start: time.Date(2021, 1, 10, 00, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 06, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 10, 06, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 12, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 10, 12, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 18, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 10, 18, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 11, 00, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 11, 00, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 11, 06, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 11, 06, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 11, 12, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 11, 12, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 11, 14, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Step = tt.args.step
if got := generateRangeTable(tt.args.r); !reflect.DeepEqual(got, tt.want) {
t.Errorf("generateRangeTable() = \r\n%v\r\n want\r\n%v", got, tt.want)
}
})
}
}
func Test_initRange(t *testing.T) {
tests := []struct {
name string
days int
step int
want v1.Range
}{
{name: "one day", days: 1, step: 10, want: v1.Range{
Start: time.Now().UTC().AddDate(0, 0, -config.Days).Truncate(time.Hour * 24).Add(time.Minute * -timeRangeOverSize),
End: time.Now().UTC().Truncate(time.Minute).Add(time.Minute * timeRangeOverSize),
Step: time.Duration(10) * time.Second,
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Days = tt.days
config.Step = tt.step
if got := initRange(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("initRange() = \r\n%v\r\n%v", got, tt.want)
}
})
}
}
func Test_initRangeFromTo(t *testing.T) {
type args struct {
from time.Time
to time.Time
step int
}
tests := []struct {
name string
args args
want v1.Range
}{
{name: "one day", args: args{
from: time.Date(2021, 1, 10, 10, 5, 0, 0, time.UTC),
to: time.Date(2021, 1, 11, 15, 0, 0, 0, time.UTC),
step: 15,
}, want: v1.Range{
Start: time.Date(2021, 1, 10, 10, 5, 0, 0, time.UTC).Add(time.Minute * -timeRangeOverSize),
End: time.Date(2021, 1, 11, 15, 0, 0, 0, time.UTC).Add(time.Minute * timeRangeOverSize),
Step: time.Duration(15) * time.Second,
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Step = tt.args.step
if got := initRangeFromTo(tt.args.from, tt.args.to); !reflect.DeepEqual(got, tt.want) {
t.Errorf("initRangeFromTo() = \r\n%v\r\n%v", got, tt.want)
}
})
}
}
func Test_printTimeRanges(t *testing.T) {
type args struct {
r []v1.Range
}
tests := []struct {
name string
args args
want int
}{
{name: "test lines", args: args{r: []v1.Range{
{
Start: time.Date(2021, 1, 10, 00, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 06, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 10, 06, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 12, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
{
Start: time.Date(2021, 1, 10, 12, 0, 0, 0, time.UTC),
End: time.Date(2021, 1, 10, 18, 0, 0, 0, time.UTC),
Step: time.Duration(15) * time.Second,
},
},
}, want: 4},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := printTimeRanges(tt.args.r); strings.Count(got, "\n") != tt.want {
t.Errorf("printTimeRanges() = %v, want %v", got, tt.want)
}
})
}
}