indices_stats_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 TestIndexStatsBuildURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Metrics []string
  14. Expected string
  15. }{
  16. {
  17. []string{},
  18. []string{},
  19. "/_stats",
  20. },
  21. {
  22. []string{"index1"},
  23. []string{},
  24. "/index1/_stats",
  25. },
  26. {
  27. []string{},
  28. []string{"metric1"},
  29. "/_stats/metric1",
  30. },
  31. {
  32. []string{"index1"},
  33. []string{"metric1"},
  34. "/index1/_stats/metric1",
  35. },
  36. {
  37. []string{"index1", "index2"},
  38. []string{"metric1"},
  39. "/index1%2Cindex2/_stats/metric1",
  40. },
  41. {
  42. []string{"index1", "index2"},
  43. []string{"metric1", "metric2"},
  44. "/index1%2Cindex2/_stats/metric1%2Cmetric2",
  45. },
  46. }
  47. for i, test := range tests {
  48. path, _, err := client.IndexStats().Index(test.Indices...).Metric(test.Metrics...).buildURL()
  49. if err != nil {
  50. t.Fatalf("case #%d: %v", i+1, err)
  51. }
  52. if path != test.Expected {
  53. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  54. }
  55. }
  56. }
  57. func TestIndexStats(t *testing.T) {
  58. client := setupTestClientAndCreateIndexAndAddDocs(t)
  59. stats, err := client.IndexStats(testIndexName).Do(context.TODO())
  60. if err != nil {
  61. t.Fatalf("expected no error; got: %v", err)
  62. }
  63. if stats == nil {
  64. t.Fatalf("expected response; got: %v", stats)
  65. }
  66. stat, found := stats.Indices[testIndexName]
  67. if !found {
  68. t.Fatalf("expected stats about index %q; got: %v", testIndexName, found)
  69. }
  70. if stat.Total == nil {
  71. t.Fatalf("expected total to be != nil; got: %v", stat.Total)
  72. }
  73. if stat.Total.Docs == nil {
  74. t.Fatalf("expected total docs to be != nil; got: %v", stat.Total.Docs)
  75. }
  76. if stat.Total.Docs.Count == 0 {
  77. t.Fatalf("expected total docs count to be > 0; got: %d", stat.Total.Docs.Count)
  78. }
  79. }