-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_definition_test.go
80 lines (76 loc) · 2.41 KB
/
index_definition_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
package elasticorm_test
import (
"encoding/json"
"testing"
"github.com/fvosberg/elasticorm"
)
func TestIndexDefinition(t *testing.T) {
tests := []struct {
title string
defFuncs []elasticorm.IndexDefinitionFunc
expectedJSON string
}{
{
title: `Empty Index definition`,
defFuncs: []elasticorm.IndexDefinitionFunc{},
expectedJSON: `{"settings":{}}`,
},
{
title: `Index definition with number of shards setting`,
defFuncs: []elasticorm.IndexDefinitionFunc{
elasticorm.SetNumberOfShards(3),
},
expectedJSON: `{"settings":{"number_of_shards":3}}`,
},
{
title: `Index definition with number of replicas setting`,
defFuncs: []elasticorm.IndexDefinitionFunc{
elasticorm.SetNumberOfReplicas(2),
},
expectedJSON: `{"settings":{"number_of_replicas":2}}`,
},
{
title: `Index definition with a setting and a customer mapping`,
defFuncs: []elasticorm.IndexDefinitionFunc{
elasticorm.SetNumberOfReplicas(2),
elasticorm.AddMappingFromStruct(
`customer`,
(func() interface{} {
type User struct {
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name"`
}
return &User{}
})(),
),
},
expectedJSON: `{"settings":{"number_of_replicas":2},"mappings":{"customer":{"properties":{"first_name":{"type":"text"},"last_name":{"type":"text"}}}}}`,
},
{
title: `Index definition with a setting and a customer mapping with an case insensitive reference ID`,
defFuncs: []elasticorm.IndexDefinitionFunc{
elasticorm.SetNumberOfReplicas(2),
elasticorm.AddMappingFromStruct(
`customer`,
(func() interface{} {
type User struct {
FirstName string `json:"first_name,omitempty"`
Email string `json:"email" elasticorm:"ref_id,case_sensitive=false"`
}
return &User{}
})(),
),
},
expectedJSON: `{"settings":{"number_of_replicas":2,"analysis":{"analyzer":{"case_insensitive_ref_id":{"type":"custom","tokenizer":"keyword","filter":["lowercase"]}}}},"mappings":{"customer":{"properties":{"email":{"type":"text","analyzer":"case_insensitive_ref_id"},"first_name":{"type":"text"}}}}}`,
},
}
for _, tt := range tests {
t.Run(tt.title, func(t *testing.T) {
def, err := elasticorm.NewIndexDefinition(tt.defFuncs...)
ok(t, err)
actualJSON, err := json.Marshal(def)
ok(t, err)
equals(t, tt.expectedJSON, string(actualJSON))
})
}
}