@@ -120,3 +120,66 @@ func TestFormatInterval(t *testing.T) {
120
120
})
121
121
}
122
122
}
123
+
124
+ func TestGetIntervalFrom (t * testing.T ) {
125
+ testCases := []struct {
126
+ name string
127
+ queryInterval string
128
+ queryIntervalMs int64
129
+ defaultInterval time.Duration
130
+ expected time.Duration
131
+ }{
132
+ {"45s" , "45s" , 0 , time .Second * 15 , time .Second * 45 },
133
+ {"45" , "45" , 0 , time .Second * 15 , time .Second * 45 },
134
+ {"2m" , "2m" , 0 , time .Second * 15 , time .Minute * 2 },
135
+ {"1d" , "1d" , 0 , time .Second * 15 , time .Hour * 24 },
136
+ {"intervalMs" , "" , 45000 , time .Second * 15 , time .Second * 45 },
137
+ {"intervalMs sub-seconds" , "" , 45200 , time .Second * 15 , time .Millisecond * 45200 },
138
+ {"defaultInterval when interval empty" , "" , 0 , time .Second * 15 , time .Second * 15 },
139
+ {"defaultInterval when intervalMs 0" , "" , 0 , time .Second * 15 , time .Second * 15 },
140
+ }
141
+
142
+ for _ , tc := range testCases {
143
+ t .Run (tc .name , func (t * testing.T ) {
144
+ actual , err := GetIntervalFrom (tc .queryInterval , "" , tc .queryIntervalMs , tc .defaultInterval )
145
+ require .Nil (t , err )
146
+ require .Equal (t , tc .expected , actual )
147
+ })
148
+ }
149
+ }
150
+
151
+ func TestParseIntervalStringToTimeDuration (t * testing.T ) {
152
+ tcs := []struct {
153
+ inp string
154
+ duration time.Duration
155
+ err * regexp.Regexp
156
+ }{
157
+ {inp : "1s" , duration : time .Second },
158
+ {inp : "1m" , duration : time .Minute },
159
+ {inp : "1h" , duration : time .Hour },
160
+ {inp : "1d" , duration : 24 * time .Hour },
161
+ {inp : "1w" , duration : 7 * 24 * time .Hour },
162
+ {inp : "2w" , duration : 2 * 7 * 24 * time .Hour },
163
+ {inp : "1M" , duration : time .Duration (730.5 * float64 (time .Hour ))},
164
+ {inp : "1y" , duration : 365.25 * 24 * time .Hour },
165
+ {inp : "5y" , duration : 5 * 365.25 * 24 * time .Hour },
166
+ {inp : "invalid-duration" , err : regexp .MustCompile (`^time: invalid duration "?invalid-duration"?$` )},
167
+ // ParseIntervalStringToTimeDuration specific conditions
168
+ {inp : "10" , duration : 10 * time .Second },
169
+ {inp : "<10s>" , duration : 10 * time .Second },
170
+ {inp : "10s>" , duration : 10 * time .Second },
171
+ {inp : "<10s" , duration : 10 * time .Second },
172
+ }
173
+ for i , tc := range tcs {
174
+ t .Run (fmt .Sprintf ("testcase %d" , i ), func (t * testing.T ) {
175
+ res , err := ParseIntervalStringToTimeDuration (tc .inp )
176
+ if tc .err == nil {
177
+ require .NoError (t , err , "input %q" , tc .inp )
178
+ require .Equal (t , tc .duration , res , "input %q" , tc .inp )
179
+ } else {
180
+ require .Error (t , err , "input %q" , tc .inp )
181
+ require .Regexp (t , tc .err , err .Error ())
182
+ }
183
+ })
184
+ }
185
+ }
0 commit comments