Debian Codegraph @

search_aggs_pipeline_mov_avg_test.go

raw
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
5 package elastic
6
7 import (
8 "encoding/json"
9 "testing"
10 )
11
12 func TestMovAvgAggregation(t *testing.T) {
13 agg := NewMovAvgAggregation().BucketsPath("the_sum")
14 src, err := agg.Source()
15 if err != nil {
16 t.Fatal(err)
17 }
18 data, err := json.Marshal(src)
19 if err != nil {
20 t.Fatalf("marshaling to JSON failed: %v", err)
21 }
22 got := string(data)
23 expected := `{"moving_avg":{"buckets_path":"the_sum"}}`
24 if got != expected {
25 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
26 }
27 }
28
29 func TestMovAvgAggregationWithSimpleModel(t *testing.T) {
30 agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewSimpleMovAvgModel())
31 src, err := agg.Source()
32 if err != nil {
33 t.Fatal(err)
34 }
35 data, err := json.Marshal(src)
36 if err != nil {
37 t.Fatalf("marshaling to JSON failed: %v", err)
38 }
39 got := string(data)
40 expected := `{"moving_avg":{"buckets_path":"the_sum","model":"simple","window":30}}`
41 if got != expected {
42 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
43 }
44 }
45
46 func TestMovAvgAggregationWithLinearModel(t *testing.T) {
47 agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewLinearMovAvgModel())
48 src, err := agg.Source()
49 if err != nil {
50 t.Fatal(err)
51 }
52 data, err := json.Marshal(src)
53 if err != nil {
54 t.Fatalf("marshaling to JSON failed: %v", err)
55 }
56 got := string(data)
57 expected := `{"moving_avg":{"buckets_path":"the_sum","model":"linear","window":30}}`
58 if got != expected {
59 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
60 }
61 }
62
63 func TestMovAvgAggregationWithEWMAModel(t *testing.T) {
64 agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewEWMAMovAvgModel().Alpha(0.5))
65 src, err := agg.Source()
66 if err != nil {
67 t.Fatal(err)
68 }
69 data, err := json.Marshal(src)
70 if err != nil {
71 t.Fatalf("marshaling to JSON failed: %v", err)
72 }
73 got := string(data)
74 expected := `{"moving_avg":{"buckets_path":"the_sum","model":"ewma","settings":{"alpha":0.5},"window":30}}`
75 if got != expected {
76 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
77 }
78 }
79
80 func TestMovAvgAggregationWithHoltLinearModel(t *testing.T) {
81 agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).
82 Model(NewHoltLinearMovAvgModel().Alpha(0.5).Beta(0.4))
83 src, err := agg.Source()
84 if err != nil {
85 t.Fatal(err)
86 }
87 data, err := json.Marshal(src)
88 if err != nil {
89 t.Fatalf("marshaling to JSON failed: %v", err)
90 }
91 got := string(data)
92 expected := `{"moving_avg":{"buckets_path":"the_sum","model":"holt","settings":{"alpha":0.5,"beta":0.4},"window":30}}`
93 if got != expected {
94 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
95 }
96 }
97
98 func TestMovAvgAggregationWithHoltWintersModel(t *testing.T) {
99 agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Predict(10).Minimize(true).
100 Model(NewHoltWintersMovAvgModel().Alpha(0.5).Beta(0.4).Gamma(0.3).Period(7).Pad(true))
101 src, err := agg.Source()
102 if err != nil {
103 t.Fatal(err)
104 }
105 data, err := json.Marshal(src)
106 if err != nil {
107 t.Fatalf("marshaling to JSON failed: %v", err)
108 }
109 got := string(data)
110 expected := `{"moving_avg":{"buckets_path":"the_sum","minimize":true,"model":"holt_winters","predict":10,"settings":{"alpha":0.5,"beta":0.4,"gamma":0.3,"pad":true,"period":7},"window":30}}`
111 if got != expected {
112 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
113 }
114 }
115
116 func TestMovAvgAggregationWithSubAggs(t *testing.T) {
117 agg := NewMovAvgAggregation().BucketsPath("the_sum")
118 agg = agg.SubAggregation("avg_sum", NewAvgAggregation().Field("height"))
119 src, err := agg.Source()
120 if err != nil {
121 t.Fatal(err)
122 }
123 data, err := json.Marshal(src)
124 if err != nil {
125 t.Fatalf("marshaling to JSON failed: %v", err)
126 }
127 got := string(data)
128 expected := `{"aggregations":{"avg_sum":{"avg":{"field":"height"}}},"moving_avg":{"buckets_path":"the_sum"}}`
129 if got != expected {
130 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
131 }
132 }