delete_test.go 3.7 KB

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