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.

search_aggs_metrics_stats_test.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 TestStatsAggregation(t *testing.T) {
  10. agg := NewStatsAggregation().Field("grade")
  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 := `{"stats":{"field":"grade"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestStatsAggregationWithOptions(t *testing.T) {
  26. agg := NewStatsAggregation().
  27. Field("grade").
  28. Format("0000.0").
  29. Missing(1.2)
  30. src, err := agg.Source()
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. data, err := json.Marshal(src)
  35. if err != nil {
  36. t.Fatalf("marshaling to JSON failed: %v", err)
  37. }
  38. got := string(data)
  39. expected := `{"stats":{"field":"grade","format":"0000.0","missing":1.2}}`
  40. if got != expected {
  41. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  42. }
  43. }
  44. func TestStatsAggregationWithMetaData(t *testing.T) {
  45. agg := NewStatsAggregation().Field("grade").Meta(map[string]interface{}{"name": "Oliver"})
  46. src, err := agg.Source()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. data, err := json.Marshal(src)
  51. if err != nil {
  52. t.Fatalf("marshaling to JSON failed: %v", err)
  53. }
  54. got := string(data)
  55. expected := `{"meta":{"name":"Oliver"},"stats":{"field":"grade"}}`
  56. if got != expected {
  57. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  58. }
  59. }