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.

indices_get_test.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 TestIndicesGetValidate(t *testing.T) {
  10. client := setupTestClient(t)
  11. // No index name -> fail with error
  12. res, err := NewIndicesGetService(client).Index("").Do(context.TODO())
  13. if err == nil {
  14. t.Fatalf("expected IndicesGet to fail without index name")
  15. }
  16. if res != nil {
  17. t.Fatalf("expected result to be == nil; got: %v", res)
  18. }
  19. }
  20. func TestIndicesGetURL(t *testing.T) {
  21. client := setupTestClientAndCreateIndex(t)
  22. tests := []struct {
  23. Indices []string
  24. Features []string
  25. Expected string
  26. }{
  27. {
  28. []string{},
  29. []string{},
  30. "/_all",
  31. },
  32. {
  33. []string{},
  34. []string{"_mappings"},
  35. "/_all/_mappings",
  36. },
  37. {
  38. []string{"twitter"},
  39. []string{"_mappings", "_settings"},
  40. "/twitter/_mappings%2C_settings",
  41. },
  42. {
  43. []string{"store-1", "store-2"},
  44. []string{"_mappings", "_settings"},
  45. "/store-1%2Cstore-2/_mappings%2C_settings",
  46. },
  47. }
  48. for _, test := range tests {
  49. path, _, err := NewIndicesGetService(client).Index(test.Indices...).Feature(test.Features...).buildURL()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if path != test.Expected {
  54. t.Errorf("expected %q; got: %q", test.Expected, path)
  55. }
  56. }
  57. }
  58. func TestIndicesGetService(t *testing.T) {
  59. client := setupTestClientAndCreateIndex(t)
  60. esversion, err := client.ElasticsearchVersion(DefaultURL)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if esversion < "1.4.0" {
  65. t.Skip("Index Get API is available since 1.4")
  66. return
  67. }
  68. res, err := client.IndexGet().Index(testIndexName).Do(context.TODO())
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if res == nil {
  73. t.Fatalf("expected result; got: %v", res)
  74. }
  75. info, found := res[testIndexName]
  76. if !found {
  77. t.Fatalf("expected index %q to be found; got: %v", testIndexName, found)
  78. }
  79. if info == nil {
  80. t.Fatalf("expected index %q to be != nil; got: %v", testIndexName, info)
  81. }
  82. if info.Mappings == nil {
  83. t.Errorf("expected mappings to be != nil; got: %v", info.Mappings)
  84. }
  85. if info.Settings == nil {
  86. t.Errorf("expected settings to be != nil; got: %v", info.Settings)
  87. }
  88. }