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