| 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 TestValidate(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 16 |
|
| 17 |
// Add a document |
| 18 |
indexResult, err := client.Index(). |
| 19 |
Index(testIndexName). |
| 20 |
Type("tweet"). |
| 21 |
BodyJson(&tweet1). |
| 22 |
Refresh("true"). |
| 23 |
Do(context.TODO()) |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
if indexResult == nil { |
| 28 |
t.Errorf("expected result to be != nil; got: %v", indexResult) |
| 29 |
} |
| 30 |
|
| 31 |
query := NewTermQuery("user", "olivere") |
| 32 |
explain := true |
| 33 |
valid, err := client.Validate(testIndexName).Type("tweet").Explain(&explain).Query(query).Do(context.TODO()) |
| 34 |
if err != nil { |
| 35 |
t.Fatal(err) |
| 36 |
} |
| 37 |
if valid == nil { |
| 38 |
t.Fatal("expected to return an validation") |
| 39 |
} |
| 40 |
if !valid.Valid { |
| 41 |
t.Errorf("expected valid to be %v; got: %v", true, valid.Valid) |
| 42 |
} |
| 43 |
|
| 44 |
invalidQuery := NewTermQuery("", false) |
| 45 |
valid, err = client.Validate(testIndexName).Type("tweet").Query(invalidQuery).Do(context.TODO()) |
| 46 |
if err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
if valid == nil { |
| 50 |
t.Fatal("expected to return an validation") |
| 51 |
} |
| 52 |
if valid.Valid { |
| 53 |
t.Errorf("expected valid to be %v; got: %v", false, valid.Valid) |
| 54 |
} |
| 55 |
} |