forked from meilisearch/meilisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
309 lines (277 loc) · 8.29 KB
/
main_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
package meilisearch
import (
"crypto/tls"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
)
type docTest struct {
ID string `json:"id"`
Name string `json:"name"`
}
type docTestBooks struct {
BookID int `json:"book_id"`
Title string `json:"title"`
Tag string `json:"tag"`
Year int `json:"year"`
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func deleteAllIndexes(client ClientInterface) (ok bool, err error) {
list, err := client.GetIndexes(nil)
if err != nil {
return false, err
}
for _, index := range list.Results {
task, _ := client.DeleteIndex(index.UID)
_, err := client.WaitForTask(task.TaskUID)
if err != nil {
return false, err
}
}
return true, nil
}
func deleteAllKeys(client ClientInterface) (ok bool, err error) {
list, err := client.GetKeys(nil)
if err != nil {
return false, err
}
for _, key := range list.Results {
if strings.Contains(key.Description, "Test") || (key.Description == "") {
_, err = client.DeleteKey(key.Key)
if err != nil {
return false, err
}
}
}
return true, nil
}
func cleanup(c ClientInterface) func() {
return func() {
_, _ = deleteAllIndexes(c)
_, _ = deleteAllKeys(c)
}
}
func testWaitForTask(t *testing.T, i *Index, u *TaskInfo) {
_, err := i.WaitForTask(u.TaskUID)
require.NoError(t, err)
}
func testWaitForBatchTask(t *testing.T, i *Index, u []TaskInfo) {
for _, id := range u {
_, err := i.WaitForTask(id.TaskUID)
require.NoError(t, err)
}
}
func GetPrivateKey() (key string) {
list, err := defaultClient.GetKeys(nil)
if err != nil {
return ""
}
for _, key := range list.Results {
if strings.Contains(key.Name, "Default Admin API Key") || (key.Description == "") {
return key.Key
}
}
return ""
}
func GetPrivateUIDKey() (key string) {
list, err := defaultClient.GetKeys(nil)
if err != nil {
return ""
}
for _, key := range list.Results {
if strings.Contains(key.Name, "Default Admin API Key") || (key.Description == "") {
return key.UID
}
}
return ""
}
func SetUpEmptyIndex(index *IndexConfig) (resp *Index, err error) {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})
task, err := client.CreateIndex(index)
if err != nil {
fmt.Println(err)
return nil, err
}
finalTask, _ := client.WaitForTask(task.TaskUID)
if finalTask.Status != "succeeded" {
os.Exit(1)
}
return client.GetIndex(index.Uid)
}
func SetUpBasicIndex(indexUID string) {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})
index := client.Index(indexUID)
documents := []map[string]interface{}{
{"book_id": 123, "title": "Pride and Prejudice"},
{"book_id": 456, "title": "Le Petit Prince"},
{"book_id": 1, "title": "Alice In Wonderland"},
{"book_id": 1344, "title": "The Hobbit"},
{"book_id": 4, "title": "Harry Potter and the Half-Blood Prince"},
{"book_id": 42, "title": "The Hitchhiker's Guide to the Galaxy"},
}
task, err := index.AddDocuments(documents)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
finalTask, _ := index.WaitForTask(task.TaskUID)
if finalTask.Status != "succeeded" {
os.Exit(1)
}
}
func SetUpIndexWithNestedFields(indexUID string) {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})
index := client.Index(indexUID)
documents := []map[string]interface{}{
{"id": 1, "title": "Pride and Prejudice", "info": map[string]interface{}{"comment": "A great book", "reviewNb": 50}},
{"id": 2, "title": "Le Petit Prince", "info": map[string]interface{}{"comment": "A french book", "reviewNb": 600}},
{"id": 3, "title": "Le Rouge et le Noir", "info": map[string]interface{}{"comment": "Another french book", "reviewNb": 700}},
{"id": 4, "title": "Alice In Wonderland", "comment": "A weird book", "info": map[string]interface{}{"comment": "A weird book", "reviewNb": 800}},
{"id": 5, "title": "The Hobbit", "info": map[string]interface{}{"comment": "An awesome book", "reviewNb": 900}},
{"id": 6, "title": "Harry Potter and the Half-Blood Prince", "info": map[string]interface{}{"comment": "The best book", "reviewNb": 1000}},
{"id": 7, "title": "The Hitchhiker's Guide to the Galaxy"},
}
task, err := index.AddDocuments(documents)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
finalTask, _ := index.WaitForTask(task.TaskUID)
if finalTask.Status != "succeeded" {
os.Exit(1)
}
}
func SetUpIndexForFaceting() {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})
index := client.Index("indexUID")
booksTest := []docTestBooks{
{BookID: 123, Title: "Pride and Prejudice", Tag: "Romance", Year: 1813},
{BookID: 456, Title: "Le Petit Prince", Tag: "Tale", Year: 1943},
{BookID: 1, Title: "Alice In Wonderland", Tag: "Tale", Year: 1865},
{BookID: 1344, Title: "The Hobbit", Tag: "Epic fantasy", Year: 1937},
{BookID: 4, Title: "Harry Potter and the Half-Blood Prince", Tag: "Epic fantasy", Year: 2005},
{BookID: 42, Title: "The Hitchhiker's Guide to the Galaxy", Tag: "Epic fantasy", Year: 1978},
{BookID: 742, Title: "The Great Gatsby", Tag: "Tragedy", Year: 1925},
{BookID: 834, Title: "One Hundred Years of Solitude", Tag: "Tragedy", Year: 1967},
{BookID: 17, Title: "In Search of Lost Time", Tag: "Modernist literature", Year: 1913},
{BookID: 204, Title: "Ulysses", Tag: "Novel", Year: 1922},
{BookID: 7, Title: "Don Quixote", Tag: "Satiric", Year: 1605},
{BookID: 10, Title: "Moby Dick", Tag: "Novel", Year: 1851},
{BookID: 730, Title: "War and Peace", Tag: "Historical fiction", Year: 1865},
{BookID: 69, Title: "Hamlet", Tag: "Tragedy", Year: 1598},
{BookID: 32, Title: "The Odyssey", Tag: "Epic", Year: 1571},
{BookID: 71, Title: "Madame Bovary", Tag: "Novel", Year: 1857},
{BookID: 56, Title: "The Divine Comedy", Tag: "Epic", Year: 1303},
{BookID: 254, Title: "Lolita", Tag: "Novel", Year: 1955},
{BookID: 921, Title: "The Brothers Karamazov", Tag: "Novel", Year: 1879},
{BookID: 1032, Title: "Crime and Punishment", Tag: "Crime fiction", Year: 1866},
}
task, err := index.AddDocuments(booksTest)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
finalTask, _ := index.WaitForTask(task.TaskUID)
if finalTask.Status != "succeeded" {
os.Exit(1)
}
}
var (
masterKey = "masterKey"
defaultClient = NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})
defaultRankingRules = []string{
"words", "typo", "proximity", "attribute", "sort", "exactness",
}
defaultTypoTolerance = TypoTolerance{
Enabled: true,
MinWordSizeForTypos: MinWordSizeForTypos{
OneTypo: 5,
TwoTypos: 9,
},
DisableOnWords: []string{},
DisableOnAttributes: []string{},
}
defaultPagination = Pagination{
MaxTotalHits: 1000,
}
defaultFaceting = Faceting{
MaxValuesPerFacet: 100,
}
)
var customClient = NewFastHTTPCustomClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
},
&fasthttp.Client{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
Name: "custom-client",
})
var timeoutClient = NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
Timeout: 1,
})
var privateClient = NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: GetPrivateKey(),
})
func TestMain(m *testing.M) {
_, _ = deleteAllIndexes(defaultClient)
code := m.Run()
_, _ = deleteAllIndexes(defaultClient)
os.Exit(code)
}
func Test_deleteAllIndexes(t *testing.T) {
indexUIDS := []string{
"Test_deleteAllIndexes",
"Test_deleteAllIndexes2",
"Test_deleteAllIndexes3",
}
_, _ = deleteAllIndexes(defaultClient)
for _, uid := range indexUIDS {
task, err := defaultClient.CreateIndex(&IndexConfig{
Uid: uid,
})
if err != nil {
t.Fatal(err)
}
_, err = defaultClient.WaitForTask(task.TaskUID)
if err != nil {
t.Fatal(err)
}
}
_, _ = deleteAllIndexes(defaultClient)
for _, uid := range indexUIDS {
resp, err := defaultClient.GetIndex(uid)
if resp != nil {
t.Fatal(resp)
}
if err == nil {
t.Fatal("deleteAllIndexes: One or more indexes were not deleted")
}
}
}