From a7e746c78654d62eab84486dbbfe5b9b84eb3ff8 Mon Sep 17 00:00:00 2001 From: Joshua Harshman Date: Mon, 22 Jan 2024 10:24:56 -0700 Subject: [PATCH] Add some basic test coverage to tsplot. (#22) --- tsplot/color_palettes_test.go | 70 ++++++++++++++++ tsplot/plot_test.go | 154 ++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tsplot/color_palettes_test.go create mode 100644 tsplot/plot_test.go diff --git a/tsplot/color_palettes_test.go b/tsplot/color_palettes_test.go new file mode 100644 index 0000000..3b5adf0 --- /dev/null +++ b/tsplot/color_palettes_test.go @@ -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) + } + }) + } +} diff --git a/tsplot/plot_test.go b/tsplot/plot_test.go new file mode 100644 index 0000000..a1783fc --- /dev/null +++ b/tsplot/plot_test.go @@ -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) + } + }) + } +}