Skip to content

Commit a7e746c

Browse files
authored
Add some basic test coverage to tsplot. (#22)
1 parent 1cee736 commit a7e746c

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

tsplot/color_palettes_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package tsplot
2+
3+
import (
4+
"golang.org/x/image/colornames"
5+
"image/color"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func Test_getUnusedColor(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
setUsed func()
14+
want color.RGBA
15+
}{
16+
{
17+
name: "basic success, unused color is navy",
18+
setUsed: func() {
19+
usedColors = map[string]color.RGBA{
20+
"brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff},
21+
"crimson": color.RGBA{0xdc, 0x14, 0x3c, 0xff},
22+
"darkkhaki": color.RGBA{0xbd, 0xb7, 0x6b, 0xff},
23+
"deepskyblue": color.RGBA{0x00, 0xbf, 0xff, 0xff},
24+
"goldenrod": color.RGBA{0xda, 0xa5, 0x20, 0xff},
25+
"gray": color.RGBA{0x80, 0x80, 0x80, 0xff},
26+
"green": color.RGBA{0x00, 0x80, 0x00, 0xff},
27+
"limegreen": color.RGBA{0x32, 0xcd, 0x32, 0xff},
28+
"magenta": color.RGBA{0xff, 0x00, 0xff, 0xff},
29+
"mediumturquoise": color.RGBA{0x48, 0xd1, 0xcc, 0xff},
30+
"orangered": color.RGBA{0xff, 0x45, 0x00, 0xff},
31+
"purple": color.RGBA{0x80, 0x00, 0x80, 0xff},
32+
"royalblue": color.RGBA{0x41, 0x69, 0xe1, 0xff},
33+
"violet": color.RGBA{0xee, 0x82, 0xee, 0xff},
34+
}
35+
},
36+
want: availableColors["navy"],
37+
},
38+
{
39+
name: "no unused color available, default line color to black",
40+
setUsed: func() {
41+
usedColors = map[string]color.RGBA{
42+
"navy": color.RGBA{0x00, 0x00, 0x80, 0xff},
43+
"brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff},
44+
"crimson": color.RGBA{0xdc, 0x14, 0x3c, 0xff},
45+
"darkkhaki": color.RGBA{0xbd, 0xb7, 0x6b, 0xff},
46+
"deepskyblue": color.RGBA{0x00, 0xbf, 0xff, 0xff},
47+
"goldenrod": color.RGBA{0xda, 0xa5, 0x20, 0xff},
48+
"gray": color.RGBA{0x80, 0x80, 0x80, 0xff},
49+
"green": color.RGBA{0x00, 0x80, 0x00, 0xff},
50+
"limegreen": color.RGBA{0x32, 0xcd, 0x32, 0xff},
51+
"magenta": color.RGBA{0xff, 0x00, 0xff, 0xff},
52+
"mediumturquoise": color.RGBA{0x48, 0xd1, 0xcc, 0xff},
53+
"orangered": color.RGBA{0xff, 0x45, 0x00, 0xff},
54+
"purple": color.RGBA{0x80, 0x00, 0x80, 0xff},
55+
"royalblue": color.RGBA{0x41, 0x69, 0xe1, 0xff},
56+
"violet": color.RGBA{0xee, 0x82, 0xee, 0xff},
57+
}
58+
},
59+
want: colornames.Black,
60+
},
61+
}
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
tt.setUsed()
65+
if got := getUnusedColor(); !reflect.DeepEqual(got, tt.want) {
66+
t.Errorf("getUnusedColor() = %v, want %v", got, tt.want)
67+
}
68+
})
69+
}
70+
}

tsplot/plot_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package tsplot
2+
3+
import (
4+
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
5+
"testing"
6+
)
7+
8+
func Test_findMaxFromFloat64Data(t *testing.T) {
9+
type args []*monitoringpb.Point
10+
tests := []struct {
11+
name string
12+
args args
13+
want float64
14+
}{
15+
{
16+
name: "find max value between two points",
17+
args: []*monitoringpb.Point{
18+
{
19+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0)}},
20+
},
21+
{
22+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
23+
},
24+
},
25+
want: float64(1),
26+
},
27+
{
28+
name: "find max value between multiple points",
29+
args: []*monitoringpb.Point{
30+
{
31+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0)}},
32+
},
33+
{
34+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
35+
},
36+
{
37+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(3)}},
38+
},
39+
{
40+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(4)}},
41+
},
42+
},
43+
want: float64(4),
44+
},
45+
{
46+
name: "find max value between multiple non whole numbers",
47+
args: []*monitoringpb.Point{
48+
{
49+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0.1)}},
50+
},
51+
{
52+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1.5)}},
53+
},
54+
{
55+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(3.8)}},
56+
},
57+
{
58+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0.4)}},
59+
},
60+
},
61+
want: float64(3.8),
62+
},
63+
{
64+
name: "find max value between multiple points of the same value",
65+
args: []*monitoringpb.Point{
66+
{
67+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
68+
},
69+
{
70+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
71+
},
72+
{
73+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
74+
},
75+
{
76+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
77+
},
78+
},
79+
want: float64(1),
80+
},
81+
}
82+
for _, tt := range tests {
83+
t.Run(tt.name, func(t *testing.T) {
84+
if got := findMaxFromFloat64Data(tt.args); got != tt.want {
85+
t.Errorf("findMaxFromFloat64Data() = %v, want %v", got, tt.want)
86+
}
87+
})
88+
}
89+
}
90+
91+
func Test_findMaxFromInt64Data(t *testing.T) {
92+
type args []*monitoringpb.Point
93+
tests := []struct {
94+
name string
95+
args args
96+
want float64
97+
}{
98+
{
99+
name: "find max value between two points",
100+
args: []*monitoringpb.Point{
101+
{
102+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(0)}},
103+
},
104+
{
105+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
106+
},
107+
},
108+
want: float64(1),
109+
},
110+
{
111+
name: "find max value between multiple points",
112+
args: []*monitoringpb.Point{
113+
{
114+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(0)}},
115+
},
116+
{
117+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
118+
},
119+
{
120+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(3)}},
121+
},
122+
{
123+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(4)}},
124+
},
125+
},
126+
want: float64(4),
127+
},
128+
{
129+
name: "find max value between multiple points of the same value",
130+
args: []*monitoringpb.Point{
131+
{
132+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
133+
},
134+
{
135+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
136+
},
137+
{
138+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
139+
},
140+
{
141+
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
142+
},
143+
},
144+
want: float64(1),
145+
},
146+
}
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
if got := findMaxFromInt64Data(tt.args); got != tt.want {
150+
t.Errorf("findMaxFromInt64Data() = %v, want %v", got, tt.want)
151+
}
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)