| 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 TestIndicesGetValidate(t *testing.T) { |
| 13 |
client := setupTestClient(t) |
| 14 |
|
| 15 |
// No index name -> fail with error |
| 16 |
res, err := NewIndicesGetService(client).Index("").Do(context.TODO()) |
| 17 |
if err == nil { |
| 18 |
t.Fatalf("expected IndicesGet to fail without index name") |
| 19 |
} |
| 20 |
if res != nil { |
| 21 |
t.Fatalf("expected result to be == nil; got: %v", res) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func TestIndicesGetURL(t *testing.T) { |
| 26 |
client := setupTestClientAndCreateIndex(t) |
| 27 |
|
| 28 |
tests := []struct { |
| 29 |
Indices []string |
| 30 |
Features []string |
| 31 |
Expected string |
| 32 |
}{ |
| 33 |
{ |
| 34 |
[]string{}, |
| 35 |
[]string{}, |
| 36 |
"/_all", |
| 37 |
}, |
| 38 |
{ |
| 39 |
[]string{}, |
| 40 |
[]string{"_mappings"}, |
| 41 |
"/_all/_mappings", |
| 42 |
}, |
| 43 |
{ |
| 44 |
[]string{"twitter"}, |
| 45 |
[]string{"_mappings", "_settings"}, |
| 46 |
"/twitter/_mappings%2C_settings", |
| 47 |
}, |
| 48 |
{ |
| 49 |
[]string{"store-1", "store-2"}, |
| 50 |
[]string{"_mappings", "_settings"}, |
| 51 |
"/store-1%2Cstore-2/_mappings%2C_settings", |
| 52 |
}, |
| 53 |
} |
| 54 |
|
| 55 |
for _, test := range tests { |
| 56 |
path, _, err := NewIndicesGetService(client).Index(test.Indices...).Feature(test.Features...).buildURL() |
| 57 |
if err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
if path != test.Expected { |
| 61 |
t.Errorf("expected %q; got: %q", test.Expected, path) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestIndicesGetService(t *testing.T) { |
| 67 |
client := setupTestClientAndCreateIndex(t) |
| 68 |
|
| 69 |
esversion, err := client.ElasticsearchVersion(DefaultURL) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
if esversion < "1.4.0" { |
| 74 |
t.Skip("Index Get API is available since 1.4") |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
res, err := client.IndexGet().Index(testIndexName).Do(context.TODO()) |
| 79 |
if err != nil { |
| 80 |
t.Fatal(err) |
| 81 |
} |
| 82 |
if res == nil { |
| 83 |
t.Fatalf("expected result; got: %v", res) |
| 84 |
} |
| 85 |
info, found := res[testIndexName] |
| 86 |
if !found { |
| 87 |
t.Fatalf("expected index %q to be found; got: %v", testIndexName, found) |
| 88 |
} |
| 89 |
if info == nil { |
| 90 |
t.Fatalf("expected index %q to be != nil; got: %v", testIndexName, info) |
| 91 |
} |
| 92 |
if info.Mappings == nil { |
| 93 |
t.Errorf("expected mappings to be != nil; got: %v", info.Mappings) |
| 94 |
} |
| 95 |
if info.Settings == nil { |
| 96 |
t.Errorf("expected settings to be != nil; got: %v", info.Settings) |
| 97 |
} |
| 98 |
} |