indices_stats_test.go 1.9 KB

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