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 TestWeightedAvgAggregation(t *testing.T) {
13	agg := NewWeightedAvgAggregation().
14		Value(&MultiValuesSourceFieldConfig{
15			FieldName: "grade",
16		}).
17		Weight(&MultiValuesSourceFieldConfig{
18			FieldName: "weight",
19			Missing:   3,
20		})
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 := `{"weighted_avg":{"value":{"field":"grade"},"weight":{"field":"weight","missing":3}}}`
31	if got != expected {
32		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
33	}
34}
35