This repository was archived by the owner on Dec 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaggregator_test.go
More file actions
170 lines (136 loc) · 3.01 KB
/
aggregator_test.go
File metadata and controls
170 lines (136 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package appoptics
import "testing"
func TestUpdateValue(t *testing.T) {
s := &Aggregator{
Count: 3,
Sum: 8,
Min: 3,
Max: 5,
Last: 3,
}
t.Run("UpdateValue", func(t *testing.T) {
newValue := float64(4)
preUpdate := *s
s.UpdateValue(newValue)
newCount := preUpdate.Count + 1
if s.Count != newCount {
t.Errorf("expected Count to be %v but was %v", newCount, s.Count)
}
newSum := preUpdate.Sum + newValue
if s.Sum != newSum {
t.Errorf("expected Sum to be %v but was %v", newSum, s.Sum)
}
if s.Min != 3 {
t.Errorf("expected Min to be 3 but was %v", s.Min)
}
if s.Max != 5 {
t.Errorf("expected Max to be 5 but was %v", s.Max)
}
if s.Last != 4 {
t.Errorf("expected Last to be 4 but was %v", s.Last)
}
})
t.Run("UpdateValue with new Min", func(t *testing.T) {
newMin := float64(1)
s.UpdateValue(newMin)
if s.Min != newMin {
t.Errorf("expected Min to be %v but was %v", newMin, s.Min)
}
})
t.Run("UpdateValue with new Max", func(t *testing.T) {
newMax := float64(7)
s.UpdateValue(newMax)
if s.Max != newMax {
t.Errorf("expected Max to be %v but was %v", newMax, s.Max)
}
})
}
func TestUpdateWithZeroValues(t *testing.T) {
newAgg := Aggregator{
Count: 2,
Sum: 3,
Min: 1,
Max: 2,
Last: 2,
}
emptyAgg := &Aggregator{}
emptyAgg.Update(newAgg)
if emptyAgg.Count != newAgg.Count {
t.Errorf("expected Count to match but %v != %v", emptyAgg.Count, newAgg.Count)
}
if emptyAgg.Sum != newAgg.Sum {
t.Errorf("expected Sum to match but %v != %v", emptyAgg.Sum, newAgg.Sum)
}
if emptyAgg.Min != newAgg.Min {
t.Errorf("expected Min to match but %v != %v", emptyAgg.Min, newAgg.Min)
}
if emptyAgg.Max != newAgg.Max {
t.Errorf("expected Max to match but %v != %v", emptyAgg.Max, newAgg.Max)
}
if emptyAgg.Last != newAgg.Last {
t.Errorf("expected Last to match but %v != %v", emptyAgg.Last, newAgg.Last)
}
}
func TestUpdateAggregation(t *testing.T) {
oldAgg := Aggregator{
Count: 2,
Sum: 3,
Min: 1,
Max: 2,
Last: 2,
}
newAgg := Aggregator{
Count: 2,
Sum: 5,
Min: 2,
Max: 3,
Last: 3,
}
oldAgg.Update(newAgg)
if oldAgg.Count != 4 {
t.Errorf("expected Count to be aggregate but was %v", oldAgg.Count)
}
if oldAgg.Sum != 8 {
t.Errorf("expected Sum to be aggregate but was %v", oldAgg.Sum)
}
}
func TestUpdateWithNewMin(t *testing.T) {
oldAgg := Aggregator{
Count: 2,
Sum: 6,
Min: 2,
Max: 4,
Last: 2,
}
newAgg := Aggregator{
Count: 2,
Sum: 4,
Min: 1,
Max: 3,
Last: 3,
}
oldAgg.Update(newAgg)
if oldAgg.Min != newAgg.Min {
t.Errorf("expected Min to be reset to %v but was %v", newAgg.Min, oldAgg.Min)
}
}
func TestUpdateWithNewMax(t *testing.T) {
oldAgg := Aggregator{
Count: 2,
Sum: 3,
Min: 1,
Max: 2,
Last: 2,
}
newAgg := Aggregator{
Count: 2,
Sum: 4,
Min: 1,
Max: 3,
Last: 3,
}
oldAgg.Update(newAgg)
if oldAgg.Max != newAgg.Max {
t.Errorf("expected Max to be reset to %v but was %v", newAgg.Max, oldAgg.Max)
}
}