| 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 TestDelete(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 16 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 17 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 18 |
|
| 19 |
// Add all documents |
| 20 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
|
| 25 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
|
| 30 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
|
| 35 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
// Count documents |
| 41 |
count, err := client.Count(testIndexName).Do(context.TODO()) |
| 42 |
if err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
if count != 3 { |
| 46 |
t.Errorf("expected Count = %d; got %d", 3, count) |
| 47 |
} |
| 48 |
|
| 49 |
// Delete document 1 |
| 50 |
res, err := client.Delete().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO()) |
| 51 |
if err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
if res.Found != true { |
| 55 |
t.Errorf("expected Found = true; got %v", res.Found) |
| 56 |
} |
| 57 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
count, err = client.Count(testIndexName).Do(context.TODO()) |
| 62 |
if err != nil { |
| 63 |
t.Fatal(err) |
| 64 |
} |
| 65 |
if count != 2 { |
| 66 |
t.Errorf("expected Count = %d; got %d", 2, count) |
| 67 |
} |
| 68 |
|
| 69 |
// Delete non existent document 99 |
| 70 |
res, err = client.Delete().Index(testIndexName).Type("tweet").Id("99").Refresh("true").Do(context.TODO()) |
| 71 |
if err == nil { |
| 72 |
t.Fatal("expected error") |
| 73 |
} |
| 74 |
if !IsNotFound(err) { |
| 75 |
t.Fatalf("expected 404, got: %v", err) |
| 76 |
} |
| 77 |
if _, ok := err.(*Error); !ok { |
| 78 |
t.Fatalf("expected error type *Error, got: %T", err) |
| 79 |
} |
| 80 |
if res == nil { |
| 81 |
t.Fatal("expected response") |
| 82 |
} |
| 83 |
if res.Found { |
| 84 |
t.Errorf("expected Found = false; got %v", res.Found) |
| 85 |
} |
| 86 |
if have, want := res.Id, "99"; have != want { |
| 87 |
t.Errorf("expected _id = %q, got %q", have, want) |
| 88 |
} |
| 89 |
if have, want := res.Index, testIndexName; have != want { |
| 90 |
t.Errorf("expected _index = %q, got %q", have, want) |
| 91 |
} |
| 92 |
if have, want := res.Type, "tweet"; have != want { |
| 93 |
t.Errorf("expected _type = %q, got %q", have, want) |
| 94 |
} |
| 95 |
if have, want := res.Result, "not_found"; have != want { |
| 96 |
t.Errorf("expected result = %q, got %q", have, want) |
| 97 |
} |
| 98 |
|
| 99 |
count, err = client.Count(testIndexName).Do(context.TODO()) |
| 100 |
if err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
if count != 2 { |
| 104 |
t.Errorf("expected Count = %d; got %d", 2, count) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestDeleteValidate(t *testing.T) { |
| 109 |
client := setupTestClientAndCreateIndexAndAddDocs(t) |
| 110 |
|
| 111 |
// No index name -> fail with error |
| 112 |
res, err := NewDeleteService(client).Type("tweet").Id("1").Do(context.TODO()) |
| 113 |
if err == nil { |
| 114 |
t.Fatalf("expected Delete to fail without index name") |
| 115 |
} |
| 116 |
if res != nil { |
| 117 |
t.Fatalf("expected result to be == nil; got: %v", res) |
| 118 |
} |
| 119 |
|
| 120 |
// No type -> fail with error |
| 121 |
res, err = NewDeleteService(client).Index(testIndexName).Id("1").Do(context.TODO()) |
| 122 |
if err == nil { |
| 123 |
t.Fatalf("expected Delete to fail without type") |
| 124 |
} |
| 125 |
if res != nil { |
| 126 |
t.Fatalf("expected result to be == nil; got: %v", res) |
| 127 |
} |
| 128 |
|
| 129 |
// No id -> fail with error |
| 130 |
res, err = NewDeleteService(client).Index(testIndexName).Type("tweet").Do(context.TODO()) |
| 131 |
if err == nil { |
| 132 |
t.Fatalf("expected Delete to fail without id") |
| 133 |
} |
| 134 |
if res != nil { |
| 135 |
t.Fatalf("expected result to be == nil; got: %v", res) |
| 136 |
} |
| 137 |
} |