Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

delete_test.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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("doc").Id("1").BodyJson(&tweet1).Do(context.TODO())
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. _, err = client.Index().Index(testIndexName).Type("doc").Id("2").BodyJson(&tweet2).Do(context.TODO())
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. _, err = client.Index().Index(testIndexName).Type("doc").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("doc").Id("1").Do(context.TODO())
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if want, have := "deleted", res.Result; want != have {
  45. t.Errorf("expected Result = %q; got %q", want, have)
  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("doc").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 have, want := res.Id, "99"; have != want {
  73. t.Errorf("expected _id = %q, got %q", have, want)
  74. }
  75. if have, want := res.Index, testIndexName; have != want {
  76. t.Errorf("expected _index = %q, got %q", have, want)
  77. }
  78. if have, want := res.Type, "doc"; have != want {
  79. t.Errorf("expected _type = %q, got %q", have, want)
  80. }
  81. if have, want := res.Result, "not_found"; have != want {
  82. t.Errorf("expected Result = %q, got %q", have, want)
  83. }
  84. count, err = client.Count(testIndexName).Do(context.TODO())
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if count != 2 {
  89. t.Errorf("expected Count = %d; got %d", 2, count)
  90. }
  91. }
  92. func TestDeleteValidate(t *testing.T) {
  93. client := setupTestClientAndCreateIndexAndAddDocs(t)
  94. // No index name -> fail with error
  95. res, err := NewDeleteService(client).Type("doc").Id("1").Do(context.TODO())
  96. if err == nil {
  97. t.Fatalf("expected Delete to fail without index name")
  98. }
  99. if res != nil {
  100. t.Fatalf("expected result to be == nil; got: %v", res)
  101. }
  102. // No type -> fail with error
  103. res, err = NewDeleteService(client).Index(testIndexName).Id("1").Do(context.TODO())
  104. if err == nil {
  105. t.Fatalf("expected Delete to fail without type")
  106. }
  107. if res != nil {
  108. t.Fatalf("expected result to be == nil; got: %v", res)
  109. }
  110. // No id -> fail with error
  111. res, err = NewDeleteService(client).Index(testIndexName).Type("doc").Do(context.TODO())
  112. if err == nil {
  113. t.Fatalf("expected Delete to fail without id")
  114. }
  115. if res != nil {
  116. t.Fatalf("expected result to be == nil; got: %v", res)
  117. }
  118. }