Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some basic test coverage to tsplot. #22

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions tsplot/color_palettes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tsplot

import (
"golang.org/x/image/colornames"
"image/color"
"reflect"
"testing"
)

func Test_getUnusedColor(t *testing.T) {
tests := []struct {
name string
setUsed func()
want color.RGBA
}{
{
name: "basic success, unused color is navy",
setUsed: func() {
usedColors = map[string]color.RGBA{
"brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff},
"crimson": color.RGBA{0xdc, 0x14, 0x3c, 0xff},
"darkkhaki": color.RGBA{0xbd, 0xb7, 0x6b, 0xff},
"deepskyblue": color.RGBA{0x00, 0xbf, 0xff, 0xff},
"goldenrod": color.RGBA{0xda, 0xa5, 0x20, 0xff},
"gray": color.RGBA{0x80, 0x80, 0x80, 0xff},
"green": color.RGBA{0x00, 0x80, 0x00, 0xff},
"limegreen": color.RGBA{0x32, 0xcd, 0x32, 0xff},
"magenta": color.RGBA{0xff, 0x00, 0xff, 0xff},
"mediumturquoise": color.RGBA{0x48, 0xd1, 0xcc, 0xff},
"orangered": color.RGBA{0xff, 0x45, 0x00, 0xff},
"purple": color.RGBA{0x80, 0x00, 0x80, 0xff},
"royalblue": color.RGBA{0x41, 0x69, 0xe1, 0xff},
"violet": color.RGBA{0xee, 0x82, 0xee, 0xff},
}
},
want: availableColors["navy"],
},
{
name: "no unused color available, default line color to black",
setUsed: func() {
usedColors = map[string]color.RGBA{
"navy": color.RGBA{0x00, 0x00, 0x80, 0xff},
"brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff},
"crimson": color.RGBA{0xdc, 0x14, 0x3c, 0xff},
"darkkhaki": color.RGBA{0xbd, 0xb7, 0x6b, 0xff},
"deepskyblue": color.RGBA{0x00, 0xbf, 0xff, 0xff},
"goldenrod": color.RGBA{0xda, 0xa5, 0x20, 0xff},
"gray": color.RGBA{0x80, 0x80, 0x80, 0xff},
"green": color.RGBA{0x00, 0x80, 0x00, 0xff},
"limegreen": color.RGBA{0x32, 0xcd, 0x32, 0xff},
"magenta": color.RGBA{0xff, 0x00, 0xff, 0xff},
"mediumturquoise": color.RGBA{0x48, 0xd1, 0xcc, 0xff},
"orangered": color.RGBA{0xff, 0x45, 0x00, 0xff},
"purple": color.RGBA{0x80, 0x00, 0x80, 0xff},
"royalblue": color.RGBA{0x41, 0x69, 0xe1, 0xff},
"violet": color.RGBA{0xee, 0x82, 0xee, 0xff},
}
},
want: colornames.Black,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setUsed()
if got := getUnusedColor(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getUnusedColor() = %v, want %v", got, tt.want)
}
})
}
}
154 changes: 154 additions & 0 deletions tsplot/plot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package tsplot

import (
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
"testing"
)

func Test_findMaxFromFloat64Data(t *testing.T) {
type args []*monitoringpb.Point
tests := []struct {
name string
args args
want float64
}{
{
name: "find max value between two points",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
},
want: float64(1),
},
{
name: "find max value between multiple points",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(3)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(4)}},
},
},
want: float64(4),
},
{
name: "find max value between multiple non whole numbers",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0.1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1.5)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(3.8)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(0.4)}},
},
},
want: float64(3.8),
},
{
name: "find max value between multiple points of the same value",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_DoubleValue{DoubleValue: float64(1)}},
},
},
want: float64(1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findMaxFromFloat64Data(tt.args); got != tt.want {
t.Errorf("findMaxFromFloat64Data() = %v, want %v", got, tt.want)
}
})
}
}

func Test_findMaxFromInt64Data(t *testing.T) {
type args []*monitoringpb.Point
tests := []struct {
name string
args args
want float64
}{
{
name: "find max value between two points",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(0)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
},
want: float64(1),
},
{
name: "find max value between multiple points",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(0)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(3)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(4)}},
},
},
want: float64(4),
},
{
name: "find max value between multiple points of the same value",
args: []*monitoringpb.Point{
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
{
Value: &monitoringpb.TypedValue{Value: &monitoringpb.TypedValue_Int64Value{Int64Value: int64(1)}},
},
},
want: float64(1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findMaxFromInt64Data(tt.args); got != tt.want {
t.Errorf("findMaxFromInt64Data() = %v, want %v", got, tt.want)
}
})
}
}
Loading