-
Notifications
You must be signed in to change notification settings - Fork 68
/
helpers_test.go
387 lines (328 loc) · 11.2 KB
/
helpers_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package moira
import (
"math"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestBytesScanner(t *testing.T) {
type BytesScannerTestCase struct {
input string
output []string
}
Convey("", t, func() {
cases := []BytesScannerTestCase{
{input: "", output: []string{}},
{input: "a", output: []string{"a"}},
{input: " ", output: []string{"", ""}},
{input: "a ", output: []string{"a", ""}},
{input: " a", output: []string{"", "a"}},
{input: " a ", output: []string{"", "a", ""}},
{input: "a a", output: []string{"a", "a"}},
}
for _, c := range cases {
actualOutput := make([]string, 0)
scanner := NewBytesScanner([]byte(c.input), ' ')
for scanner.HasNext() {
actualOutput = append(actualOutput, string(scanner.Next()))
}
So(actualOutput, ShouldResemble, c.output)
}
})
}
func TestInt64ToTime(t *testing.T) {
int64timeStamp := int64(1527330278)
humanReadableTimestamp := time.Date(2018, 5, 26, 10, 24, 38, 0, time.UTC)
Convey("Convert int64 timestamp into datetime", t, func() {
converted := Int64ToTime(int64timeStamp)
So(converted, ShouldResemble, humanReadableTimestamp)
})
Convey("Convert int64 timestamp + 1 minute into datetime", t, func() {
int64timeStamp += 60
converted := Int64ToTime(int64timeStamp)
So(converted, ShouldResemble, humanReadableTimestamp.Add(time.Minute))
})
}
func TestSubset(t *testing.T) {
Convey("Test subsets", t, func() {
So(Subset([]string{"1", "2", "3"}, []string{"3", "2", "1"}), ShouldBeTrue)
So(Subset([]string{"1", "2", "3"}, []string{"1", "1", "1", "2", "2", "2", "3", "3", "3"}), ShouldBeTrue)
So(Subset([]string{"1", "2", "3"}, []string{"123", "2", "3"}), ShouldBeFalse)
So(Subset([]string{"1", "2", "3"}, []string{"1", "2", "4"}), ShouldBeFalse)
})
}
func TestGetStringListsDiff(t *testing.T) {
Convey("Test Get Difference between string lists", t, func() {
first := []string{"1", "2", "3"}
second := []string{"1", "2", "3"}
diff := GetStringListsDiff(first, second)
So(diff, ShouldResemble, []string{})
first = []string{"1", "2", "3", "4", "5"}
diff = GetStringListsDiff(first, second)
So(diff, ShouldResemble, []string{"4", "5"})
second = []string{"6", "7", "8"}
diff = GetStringListsDiff(first, second)
So(diff, ShouldResemble, first)
third := []string{"8", "9", "10"}
diff = GetStringListsDiff(first, second, third)
So(diff, ShouldResemble, first)
first = []string{"1", "2", "3", "4", "5", "6", "7", "8"}
second = []string{"6", "7", "8"}
third = []string{"8", "9", "10"}
diff = GetStringListsDiff(first, second, third)
So(diff, ShouldResemble, []string{"1", "2", "3", "4", "5"})
})
}
func TestGetStringListsUnion(t *testing.T) {
Convey("Test Get Union between string lists", t, func() {
{
union := GetStringListsUnion()
So(union, ShouldResemble, []string{})
}
{
union := GetStringListsUnion(nil)
So(union, ShouldResemble, []string{})
}
{
union := GetStringListsUnion(nil, nil)
So(union, ShouldResemble, []string{})
}
{
first := []string{"1", "2", "3"}
second := []string{"1", "2", "3"}
union := GetStringListsUnion(first, second)
So(union, ShouldResemble, []string{"1", "2", "3"})
}
{
first := []string{"1", "2", "3"}
second := []string{"4", "5", "6"}
union := GetStringListsUnion(first, second)
So(union, ShouldResemble, []string{"1", "2", "3", "4", "5", "6"})
}
})
}
func TestGetTriggerListsDiff(t *testing.T) {
Convey("Test Get Difference between trigger lists", t, func() {
first := []*Trigger{triggerVal1, triggerVal2}
second := []*Trigger{triggerVal3, triggerVal1, triggerVal2, triggerVal4}
diff := GetTriggerListsDiff(first, second)
So(diff, ShouldResemble, []*Trigger{})
first = []*Trigger{triggerVal1, triggerVal2, triggerVal3, triggerVal4}
second = []*Trigger{triggerVal2, triggerVal2}
third := []*Trigger{triggerVal3, triggerVal3, triggerVal3}
diff = GetTriggerListsDiff(first, second, third)
So(diff, ShouldResemble, []*Trigger{triggerVal1, triggerVal4})
Convey("One trigger in first array is nil", func() {
first = []*Trigger{nil, triggerVal2, triggerVal3, triggerVal4}
second = []*Trigger{triggerVal2}
diff = GetTriggerListsDiff(first, second)
So(diff, ShouldResemble, []*Trigger{triggerVal3, triggerVal4})
})
Convey("One trigger in additional arrays in nil", func() {
first = []*Trigger{triggerVal1, triggerVal2, triggerVal3, triggerVal4}
second = []*Trigger{nil}
diff = GetTriggerListsDiff(first, second)
So(diff, ShouldResemble, []*Trigger{triggerVal1, triggerVal2, triggerVal3, triggerVal4})
})
Convey("First array is empty", func() {
first = []*Trigger{nil, nil, nil}
second = []*Trigger{triggerVal1}
diff = GetTriggerListsDiff(first, second)
So(diff, ShouldResemble, []*Trigger{})
})
Convey("Additional arrays is empty", func() {
first = []*Trigger{triggerVal1}
second = []*Trigger{nil}
third = []*Trigger{nil}
diff = GetTriggerListsDiff(first, second, third)
So(diff, ShouldResemble, first)
})
})
}
var triggerVal1 = &Trigger{
ID: "trigger-id-1",
Name: "Super Trigger 1",
Tags: []string{"test", "super", "1"},
}
var triggerVal2 = &Trigger{
ID: "trigger-id-2",
Name: "Super Trigger 2",
Tags: []string{"test", "2"},
}
var triggerVal3 = &Trigger{
ID: "trigger-id-3",
Name: "Super Trigger 3",
Tags: []string{"super", "3"},
}
var triggerVal4 = &Trigger{
ID: "trigger-id-4",
Name: "Super Trigger 4",
TriggerSource: GraphiteRemote,
TTL: 600,
Tags: []string{"4"},
}
func TestChunkSlice(t *testing.T) {
Convey("Test chunking slices", t, func() {
originalSlice := []string{"123", "234", "345", "456", "567", "678", "789", "890"}
actual := ChunkSlice(originalSlice, 10)
So(actual, ShouldResemble, [][]string{originalSlice})
actual = ChunkSlice(originalSlice, 1)
So(actual, ShouldResemble, [][]string{{"123"}, {"234"}, {"345"}, {"456"}, {"567"}, {"678"}, {"789"}, {"890"}})
actual = ChunkSlice(originalSlice, 5)
So(actual, ShouldResemble, [][]string{{"123", "234", "345", "456", "567"}, {"678", "789", "890"}})
actual = ChunkSlice(originalSlice, 0)
So(actual, ShouldBeEmpty)
})
}
func TestIsValidFloat64(t *testing.T) {
Convey("values +Inf -Inf and NaN is invalid", t, func() {
So(IsFiniteNumber(math.NaN()), ShouldBeFalse)
So(IsFiniteNumber(math.Inf(-1)), ShouldBeFalse)
So(IsFiniteNumber(math.Inf(1)), ShouldBeFalse)
So(IsFiniteNumber(3.14), ShouldBeTrue)
})
}
func TestRoundToNearestRetention(t *testing.T) {
var baseTimestamp, retentionSeconds int64
baseTimestamp = 1582286400 // 17:00:00
Convey("Test even retention: 60s", t, func() {
retentionSeconds = 60
testRounding(baseTimestamp, retentionSeconds)
})
Convey("Test odd retention: 15s", t, func() {
retentionSeconds = 15
testRounding(baseTimestamp, retentionSeconds)
})
}
func testRounding(baseTimestamp, retention int64) {
halfRetention := retention / 2
nextTimestamp := baseTimestamp + retention
Convey("round to self", func() {
So(RoundToNearestRetention(baseTimestamp, retention), ShouldEqual, baseTimestamp)
})
So(RoundToNearestRetention(baseTimestamp+1, retention), ShouldEqual, baseTimestamp)
So(RoundToNearestRetention(baseTimestamp+(halfRetention-1), retention), ShouldEqual, baseTimestamp)
So(RoundToNearestRetention(baseTimestamp+halfRetention+retention%2, retention), ShouldEqual, nextTimestamp)
So(RoundToNearestRetention(baseTimestamp-1, retention), ShouldEqual, baseTimestamp)
So(RoundToNearestRetention(baseTimestamp-halfRetention, retention), ShouldEqual, baseTimestamp)
}
func TestReplaceSubstring(t *testing.T) {
Convey("Test replace substring", t, func() {
Convey("replacement string in the middle", func() {
So(ReplaceSubstring("telebot: Post https://api.telegram.org/botXXX/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/bot[DELETED]/getMe")
})
Convey("replacement string at the beginning", func() {
So(ReplaceSubstring("/botXXX/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "/bot[DELETED]/getMe")
})
Convey("replacement string at the end", func() {
So(ReplaceSubstring("telebot: Post https://api.telegram.org/botXXX/", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/bot[DELETED]/")
})
Convey("no replacement string", func() {
So(ReplaceSubstring("telebot: Post https://api.telegram.org/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/getMe")
})
Convey("there is the beginning of replacement string, but no end", func() {
So(ReplaceSubstring("https://api.telegram.org/botXXX error", "/bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/botXXX error")
})
})
}
type myInt int
func (m myInt) Less(other Comparable) (bool, error) {
otherInt := other.(myInt)
return m < otherInt, nil
}
type myTest struct {
value int
}
func (test myTest) Less(other Comparable) (bool, error) {
otherTest := other.(myTest)
return test.value < otherTest.value, nil
}
func TestMergeToSorted(t *testing.T) {
Convey("Test MergeToSorted function", t, func() {
Convey("Test with two nil arrays", func() {
merged, err := MergeToSorted[myInt](nil, nil)
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{})
})
Convey("Test with one nil array", func() {
merged, err := MergeToSorted(nil, []myInt{1, 2, 3})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 2, 3})
})
Convey("Test with two arrays", func() {
merged, err := MergeToSorted([]myInt{4, 5}, []myInt{1, 2, 3})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 2, 3, 4, 5})
})
Convey("Test with empty array", func() {
merged, err := MergeToSorted([]myInt{-4, 5}, []myInt{})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{-4, 5})
})
Convey("Test with sorted values but mixed up", func() {
merged, err := MergeToSorted([]myInt{1, 9, 10}, []myInt{4, 8, 12})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 4, 8, 9, 10, 12})
})
Convey("Test with structure type", func() {
arr1 := []myTest{
{
value: 1,
},
{
value: 2,
},
}
arr2 := []myTest{
{
value: -2,
},
{
value: -1,
},
}
expected := append(arr2, arr1...)
merged, err := MergeToSorted(arr1, arr2)
So(err, ShouldBeNil)
So(merged, ShouldResemble, expected)
})
})
}
func TestValidateStruct(t *testing.T) {
type ValidationStruct struct {
TestInt int `validate:"required,gt=0"`
TestURL string `validate:"required,url"`
TestBool bool
}
const (
validURL = "https://github.com/moira-alert/moira"
validInt = 1
)
Convey("Test ValidateStruct", t, func() {
Convey("With TestInt less than zero", func() {
testStruct := ValidationStruct{
TestInt: -1,
TestURL: validURL,
}
err := ValidateStruct(testStruct)
So(err, ShouldNotBeNil)
})
Convey("With invalid TestURL format", func() {
testStruct := ValidationStruct{
TestInt: validInt,
TestURL: "test",
TestBool: true,
}
err := ValidateStruct(testStruct)
So(err, ShouldNotBeNil)
})
Convey("With valid structure", func() {
testStruct := ValidationStruct{
TestInt: validInt,
TestURL: validURL,
}
err := ValidateStruct(testStruct)
So(err, ShouldBeNil)
})
})
}