search_aggs_metrics_sum_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 TestSumAggregation(t *testing.T) {
  10. agg := NewSumAggregation().Field("price")
  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 := `{"sum":{"field":"price"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestSumAggregationWithFormat(t *testing.T) {
  26. agg := NewSumAggregation().Field("price").Format("00000.00")
  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 := `{"sum":{"field":"price","format":"00000.00"}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestSumAggregationWithMetaData(t *testing.T) {
  42. agg := NewSumAggregation().Field("price").Meta(map[string]interface{}{"name": "Oliver"})
  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 := `{"meta":{"name":"Oliver"},"sum":{"field":"price"}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }