indices_put_settings_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 TestIndicesPutSettingsBuildURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Expected string
  14. }{
  15. {
  16. []string{},
  17. "/_settings",
  18. },
  19. {
  20. []string{"*"},
  21. "/%2A/_settings",
  22. },
  23. {
  24. []string{"store-1", "store-2"},
  25. "/store-1%2Cstore-2/_settings",
  26. },
  27. }
  28. for _, test := range tests {
  29. path, _, err := client.IndexPutSettings().Index(test.Indices...).buildURL()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if path != test.Expected {
  34. t.Errorf("expected %q; got: %q", test.Expected, path)
  35. }
  36. }
  37. }
  38. func TestIndicesSettingsLifecycle(t *testing.T) {
  39. client := setupTestClientAndCreateIndex(t)
  40. body := `{
  41. "index":{
  42. "refresh_interval":"-1"
  43. }
  44. }`
  45. // Put settings
  46. putres, err := client.IndexPutSettings().Index(testIndexName).BodyString(body).Do(context.TODO())
  47. if err != nil {
  48. t.Fatalf("expected put settings to succeed; got: %v", err)
  49. }
  50. if putres == nil {
  51. t.Fatalf("expected put settings response; got: %v", putres)
  52. }
  53. if !putres.Acknowledged {
  54. t.Fatalf("expected put settings ack; got: %v", putres.Acknowledged)
  55. }
  56. // Read settings
  57. getres, err := client.IndexGetSettings().Index(testIndexName).Do(context.TODO())
  58. if err != nil {
  59. t.Fatalf("expected get mapping to succeed; got: %v", err)
  60. }
  61. if getres == nil {
  62. t.Fatalf("expected get mapping response; got: %v", getres)
  63. }
  64. // Check settings
  65. index, found := getres[testIndexName]
  66. if !found {
  67. t.Fatalf("expected to return settings for index %q; got: %#v", testIndexName, getres)
  68. }
  69. // Retrieve "index" section of the settings for index testIndexName
  70. sectionIntf, ok := index.Settings["index"]
  71. if !ok {
  72. t.Fatalf("expected settings to have %q field; got: %#v", "index", getres)
  73. }
  74. section, ok := sectionIntf.(map[string]interface{})
  75. if !ok {
  76. t.Fatalf("expected settings to be of type map[string]interface{}; got: %#v", getres)
  77. }
  78. refintv, ok := section["refresh_interval"]
  79. if !ok {
  80. t.Fatalf(`expected JSON to include "refresh_interval" field; got: %#v`, getres)
  81. }
  82. if got, want := refintv, "-1"; got != want {
  83. t.Fatalf("expected refresh_interval = %v; got: %v", want, got)
  84. }
  85. }