search_aggs_metrics_percentile_ranks_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "encoding/json"
  7. "testing"
  8. )
  9. func TestPercentileRanksAggregation(t *testing.T) {
  10. agg := NewPercentileRanksAggregation().Field("load_time")
  11. src, err := agg.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"percentile_ranks":{"field":"load_time"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestPercentileRanksAggregationWithCustomValues(t *testing.T) {
  26. agg := NewPercentileRanksAggregation().Field("load_time").Values(15, 30)
  27. src, err := agg.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"percentile_ranks":{"field":"load_time","values":[15,30]}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestPercentileRanksAggregationWithFormat(t *testing.T) {
  42. agg := NewPercentileRanksAggregation().Field("load_time").Format("000.0")
  43. src, err := agg.Source()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `{"percentile_ranks":{"field":"load_time","format":"000.0"}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestPercentileRanksAggregationWithMetaData(t *testing.T) {
  58. agg := NewPercentileRanksAggregation().Field("load_time").Meta(map[string]interface{}{"name": "Oliver"})
  59. src, err := agg.Source()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. data, err := json.Marshal(src)
  64. if err != nil {
  65. t.Fatalf("marshaling to JSON failed: %v", err)
  66. }
  67. got := string(data)
  68. expected := `{"meta":{"name":"Oliver"},"percentile_ranks":{"field":"load_time"}}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }