indices_flush_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 TestFlush(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. // Flush all indices
  12. res, err := client.Flush().Do(context.TODO())
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if res == nil {
  17. t.Errorf("expected res to be != nil; got: %v", res)
  18. }
  19. }
  20. func TestFlushBuildURL(t *testing.T) {
  21. client := setupTestClientAndCreateIndex(t)
  22. tests := []struct {
  23. Indices []string
  24. Expected string
  25. ExpectValidateFailure bool
  26. }{
  27. {
  28. []string{},
  29. "/_flush",
  30. false,
  31. },
  32. {
  33. []string{"index1"},
  34. "/index1/_flush",
  35. false,
  36. },
  37. {
  38. []string{"index1", "index2"},
  39. "/index1%2Cindex2/_flush",
  40. false,
  41. },
  42. }
  43. for i, test := range tests {
  44. err := NewIndicesFlushService(client).Index(test.Indices...).Validate()
  45. if err == nil && test.ExpectValidateFailure {
  46. t.Errorf("case #%d: expected validate to fail", i+1)
  47. continue
  48. }
  49. if err != nil && !test.ExpectValidateFailure {
  50. t.Errorf("case #%d: expected validate to succeed", i+1)
  51. continue
  52. }
  53. if !test.ExpectValidateFailure {
  54. path, _, err := NewIndicesFlushService(client).Index(test.Indices...).buildURL()
  55. if err != nil {
  56. t.Fatalf("case #%d: %v", i+1, err)
  57. }
  58. if path != test.Expected {
  59. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  60. }
  61. }
  62. }
  63. }