| 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 TestExists(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 14 |
|
| 15 |
exists, err := client.Exists().Index(testIndexName).Type("comment").Id("1").Parent("tweet").Do(context.TODO()) |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
if !exists { |
| 20 |
t.Fatal("expected document to exist") |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func TestExistsValidate(t *testing.T) { |
| 25 |
client := setupTestClient(t) |
| 26 |
|
| 27 |
// No index -> fail with error |
| 28 |
res, err := NewExistsService(client).Type("tweet").Id("1").Do(context.TODO()) |
| 29 |
if err == nil { |
| 30 |
t.Fatalf("expected Delete to fail without index name") |
| 31 |
} |
| 32 |
if res != false { |
| 33 |
t.Fatalf("expected result to be false; got: %v", res) |
| 34 |
} |
| 35 |
|
| 36 |
// No type -> fail with error |
| 37 |
res, err = NewExistsService(client).Index(testIndexName).Id("1").Do(context.TODO()) |
| 38 |
if err == nil { |
| 39 |
t.Fatalf("expected Delete to fail without index name") |
| 40 |
} |
| 41 |
if res != false { |
| 42 |
t.Fatalf("expected result to be false; got: %v", res) |
| 43 |
} |
| 44 |
|
| 45 |
// No id -> fail with error |
| 46 |
res, err = NewExistsService(client).Index(testIndexName).Type("tweet").Do(context.TODO()) |
| 47 |
if err == nil { |
| 48 |
t.Fatalf("expected Delete to fail without index name") |
| 49 |
} |
| 50 |
if res != false { |
| 51 |
t.Fatalf("expected result to be false; got: %v", res) |
| 52 |
} |
| 53 |
} |