search_aggs_matrix_stats_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 TestMatrixStatsAggregation(t *testing.T) {
  10. agg := NewMatrixStatsAggregation().
  11. Fields("poverty", "income").
  12. Missing(map[string]interface{}{
  13. "income": 50000,
  14. }).
  15. Mode("avg").
  16. Format("0000.0").
  17. ValueType("double")
  18. src, err := agg.Source()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. data, err := json.Marshal(src)
  23. if err != nil {
  24. t.Fatalf("marshaling to JSON failed: %v", err)
  25. }
  26. got := string(data)
  27. expected := `{"matrix_stats":{"fields":["poverty","income"],"format":"0000.0","missing":{"income":50000},"mode":"avg","value_type":"double"}}`
  28. if got != expected {
  29. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  30. }
  31. }
  32. func TestMatrixStatsAggregationWithMetaData(t *testing.T) {
  33. agg := NewMatrixStatsAggregation().
  34. Fields("poverty", "income").
  35. Meta(map[string]interface{}{"name": "Oliver"})
  36. src, err := agg.Source()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. data, err := json.Marshal(src)
  41. if err != nil {
  42. t.Fatalf("marshaling to JSON failed: %v", err)
  43. }
  44. got := string(data)
  45. expected := `{"matrix_stats":{"fields":["poverty","income"]},"meta":{"name":"Oliver"}}`
  46. if got != expected {
  47. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  48. }
  49. }