index_test.go
1.4 KB
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
package es
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const testTime = "2020-09-13T08:22:29.294Z"
func TestBuildIndexFormatter(t *testing.T) {
tests := []struct {
name string
val string
attrs map[string]interface{}
expect string
}{
{
name: "plain text only",
val: "yyyy/MM/dd",
expect: "yyyy/MM/dd",
},
{
name: "time only",
val: "{{yyyy/MM/dd}}",
expect: time.Now().Format("2006/01/02"),
},
{
name: "attr without time",
val: "{.event}",
attrs: map[string]interface{}{
"event": "foo",
},
expect: "foo",
},
{
name: "attr with time",
val: "{.event}-{{yyyy/MM/dd}}",
attrs: map[string]interface{}{
"event": "foo",
timestampKey: testTime,
},
expect: "foo-2020/09/13",
},
{
name: "attr with time, with missing",
val: "{.event}-{.foo}-{{yyyy/MM/dd}}",
attrs: map[string]interface{}{
"event": "foo",
timestampKey: testTime,
},
expect: "foo--2020/09/13",
},
{
name: "attr with time, leading alphas",
val: "{the.event}-{{yyyy/MM/dd}}",
attrs: map[string]interface{}{
"event": "foo",
timestampKey: testTime,
},
expect: "foo-2020/09/13",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
formatter := buildIndexFormatter(test.val, time.Local)
assert.Equal(t, test.expect, formatter(test.attrs))
})
}
}