-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some basic test coverage to tsplot. (#22)
- Loading branch information
Showing
2 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |