| 1 |
package elastic |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestIndicesAnalyzeURL(t *testing.T) { |
| 9 |
client := setupTestClient(t) |
| 10 |
|
| 11 |
tests := []struct { |
| 12 |
Index string |
| 13 |
Expected string |
| 14 |
}{ |
| 15 |
{ |
| 16 |
"", |
| 17 |
"/_analyze", |
| 18 |
}, |
| 19 |
{ |
| 20 |
"tweets", |
| 21 |
"/tweets/_analyze", |
| 22 |
}, |
| 23 |
} |
| 24 |
|
| 25 |
for _, test := range tests { |
| 26 |
path, _, err := client.IndexAnalyze().Index(test.Index).buildURL() |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
if path != test.Expected { |
| 31 |
t.Errorf("expected %q; got: %q", test.Expected, path) |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestIndicesAnalyze(t *testing.T) { |
| 37 |
client := setupTestClient(t) |
| 38 |
// client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 39 |
|
| 40 |
res, err := client.IndexAnalyze().Text("hello hi guy").Do(context.TODO()) |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("expected no error, got %v", err) |
| 43 |
} |
| 44 |
if len(res.Tokens) != 3 { |
| 45 |
t.Fatalf("expected %d, got %d (%+v)", 3, len(res.Tokens), res.Tokens) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestIndicesAnalyzeDetail(t *testing.T) { |
| 50 |
client := setupTestClient(t) |
| 51 |
// client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 52 |
|
| 53 |
res, err := client.IndexAnalyze().Text("hello hi guy").Explain(true).Do(context.TODO()) |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("expected no error, got %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
if len(res.Detail.Analyzer.Tokens) != 3 { |
| 59 |
t.Fatalf("expected %d tokens, got %d (%+v)", 3, len(res.Detail.Tokenizer.Tokens), res.Detail.Tokenizer.Tokens) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestIndicesAnalyzeWithIndex(t *testing.T) { |
| 64 |
client := setupTestClient(t) |
| 65 |
|
| 66 |
_, err := client.IndexAnalyze().Index("foo").Text("hello hi guy").Do(context.TODO()) |
| 67 |
if err == nil { |
| 68 |
t.Fatal("expected error, got nil") |
| 69 |
} |
| 70 |
if want, have := "elastic: Error 404 (Not Found): no such index [type=index_not_found_exception]", err.Error(); want != have { |
| 71 |
t.Fatalf("expected error %q, got %q", want, have) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestIndicesAnalyzeValidate(t *testing.T) { |
| 76 |
client := setupTestClient(t) |
| 77 |
|
| 78 |
_, err := client.IndexAnalyze().Do(context.TODO()) |
| 79 |
if err == nil { |
| 80 |
t.Fatal("expected error, got nil") |
| 81 |
} |
| 82 |
if want, have := "missing required fields: [Text]", err.Error(); want != have { |
| 83 |
t.Fatalf("expected error %q, got %q", want, have) |
| 84 |
} |
| 85 |
} |