| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestRefreshBuildURL(t *testing.T) { |
| 13 |
client := setupTestClient(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Indices []string |
| 17 |
Expected string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
[]string{}, |
| 21 |
"/_refresh", |
| 22 |
}, |
| 23 |
{ |
| 24 |
[]string{"index1"}, |
| 25 |
"/index1/_refresh", |
| 26 |
}, |
| 27 |
{ |
| 28 |
[]string{"index1", "index2"}, |
| 29 |
"/index1%2Cindex2/_refresh", |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
for i, test := range tests { |
| 34 |
path, _, err := client.Refresh().Index(test.Indices...).buildURL() |
| 35 |
if err != nil { |
| 36 |
t.Errorf("case #%d: %v", i+1, err) |
| 37 |
continue |
| 38 |
} |
| 39 |
if path != test.Expected { |
| 40 |
t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestRefresh(t *testing.T) { |
| 46 |
client := setupTestClientAndCreateIndex(t) |
| 47 |
|
| 48 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 49 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 50 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 51 |
|
| 52 |
// Add some documents |
| 53 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 59 |
if err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
|
| 63 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
|
| 68 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 69 |
if err != nil { |
| 70 |
t.Fatal(err) |
| 71 |
} |
| 72 |
|
| 73 |
// Refresh indices |
| 74 |
res, err := client.Refresh(testIndexName, testIndexName2).Do(context.TODO()) |
| 75 |
if err != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
if res == nil { |
| 79 |
t.Fatal("expected result; got nil") |
| 80 |
} |
| 81 |
} |