-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch_test.go
174 lines (151 loc) · 4.29 KB
/
elasticsearch_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
package kasper
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
elastic "gopkg.in/olivere/elastic.v5"
)
var store *Elasticsearch
var vorgansharax = []byte(`{"color": "green", "name": "Vorgansharax"}`)
var falkor = []byte(`{"color": "white", "name": "Falkor"}`)
var saphira = []byte(`{"color": "blue", "name": "Saphira"}`)
var mushu = []byte(`{"color": "red", "name": "Mushu"}`)
var finFangFoom = []byte(`{"color": "green", "name": "Fin Fang Foom"}`)
func TestElasticsearch_Get_Put(t *testing.T) {
if testing.Short() {
t.Skip()
}
// Get non-existing key
item, err := store.Get("vorgansharax")
assert.Nil(t, item)
assert.Nil(t, err)
// Put key
err = store.Put("vorgansharax", vorgansharax)
assert.Nil(t, err)
// Get key again, should find it this time
dragon, err := store.Get("vorgansharax")
assert.Equal(t, vorgansharax, dragon)
// Test failed PUT (400)
_, err = store.client.DeleteIndex("kasper").Do(store.context)
assert.Nil(t, err)
defer func() {
_, err = store.client.DeleteIndex("kasper").Do(store.context)
assert.Nil(t, err)
}()
// First trick MultiElasticsearch into thinking Color is a date field...
err = store.Put("vorgansharax", []byte(`{"color": "2009-11-15T14:12:12", "name": "Vorgansharax"}`))
assert.Nil(t, err)
// Then try to put a regular string in
err = store.Put("vorgansharax", []byte(`{"color": "", "name": "Vorgansharax"}`))
assert.NotNil(t, err)
}
func TestElasticsearch_Delete(t *testing.T) {
if testing.Short() {
t.Skip()
}
// Put key
err := store.Put("falkor", falkor)
assert.Nil(t, err)
// Delete key
err = store.Delete("falkor")
assert.Nil(t, err)
// Get key again, should not find it this time
item, err := store.Get("falkor")
assert.Nil(t, err)
assert.Nil(t, item)
// Delete key again does nothing
err = store.Delete("falkor")
assert.Nil(t, err)
}
func TestElasticsearch_GetAll_PutAll(t *testing.T) {
if testing.Short() {
t.Skip()
}
// Put 3 keys
err := store.Put("saphira", saphira)
assert.Nil(t, err)
err = store.Put("mushu", mushu)
assert.Nil(t, err)
err = store.Put("fin-fang-foom", finFangFoom)
assert.Nil(t, err)
// GetAll on 4 keys, one non existing
kvs, err := store.GetAll([]string{
"saphira",
"draco",
"mushu",
"fin-fang-foom",
})
assert.Nil(t, err)
// Check the 3 keys
assert.Equal(t, 3, len(kvs))
assert.Equal(t, saphira, kvs["saphira"])
assert.Equal(t, mushu, kvs["mushu"])
assert.Equal(t, finFangFoom, kvs["fin-fang-foom"])
// Delete everything
_, err = store.client.DeleteIndex("kasper").Do(store.context)
assert.Nil(t, err)
// PutAll all 3 dragons again
err = store.PutAll(kvs)
assert.Nil(t, err)
// Check the 3 keys once more
kvs, err = store.GetAll([]string{
"saphira",
"mushu",
"fin-fang-foom",
})
assert.Nil(t, err)
assert.Equal(t, 3, len(kvs))
assert.Equal(t, saphira, kvs["saphira"])
assert.Equal(t, mushu, kvs["mushu"])
assert.Equal(t, finFangFoom, kvs["fin-fang-foom"])
// Test failed PUT (400)
_, err = store.client.DeleteIndex("kasper").Do(store.context)
assert.Nil(t, err)
// First trick MultiElasticsearch into thinking Color is a date field...
err = store.Put("vorgansharax", []byte(`{"color": "2009-11-15T14:12:12", "name": "Vorgansharax"}`))
assert.Nil(t, err)
// Then try to put a regular string in
err = store.PutAll(map[string][]byte{
"vorgansharax1": vorgansharax,
"vorgansharax2": vorgansharax,
"vorgansharax3": vorgansharax,
"vorgansharax4": vorgansharax,
"vorgansharax5": vorgansharax,
"vorgansharax6": vorgansharax,
"vorgansharax7": vorgansharax,
})
assert.NotNil(t, err)
// GetAll of empty list should work
_, err = store.GetAll(nil)
assert.Nil(t, err)
_, err = store.GetAll([]string{})
assert.Nil(t, err)
}
func TestElasticsearch_Flush(t *testing.T) {
if testing.Short() {
t.Skip()
}
err := store.Flush()
assert.Nil(t, err)
}
func init() {
if testing.Short() {
return
}
config := &Config{
TopicProcessorName: "test",
Logger: &noopLogger{},
MetricsProvider: &NoopMetricsProvider{},
}
url := fmt.Sprintf("http://%s:9200", getCIHost())
client, err := elastic.NewClient(
elastic.SetURL(url),
elastic.SetSniff(false),
)
if err != nil {
panic(err)
}
store = NewElasticsearch(config, client, "kasper", "dragon")
store.client.DeleteIndex("kasper").Do(store.context)
store.client.CreateIndex("kasper").Do(store.context)
}