Replaced test validations with testutils on storage/remote/codec_test.go (#6097)

* Replaced test validations with testutils on storage/remote/codec_test.go

Signed-off-by: George Felix <george.felix@ubeeqo.com>

* gofmt

Signed-off-by: George Felix <george.felix@ubeeqo.com>

* Removed shouldPass assertion

Signed-off-by: George Felix <gfelixc@gmail.com>

* Fixes to improve readability

Signed-off-by: George Felix <george.felix@ubeeqo.com>

* Fixes based on code review comments

Signed-off-by: George Felix <george.felix@ubeeqo.com>
This commit is contained in:
George Felix 2019-10-07 19:35:53 +02:00 committed by Chris Marchbanks
parent 16f1e252f4
commit 895abbb7d0

View File

@ -27,7 +27,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
tests := []struct { tests := []struct {
input labels.Labels input labels.Labels
expectedErr string expectedErr string
shouldPass bool
description string description string
}{ }{
{ {
@ -36,7 +35,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"labelName", "labelValue", "labelName", "labelValue",
), ),
expectedErr: "", expectedErr: "",
shouldPass: true,
description: "regular labels", description: "regular labels",
}, },
{ {
@ -45,7 +43,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"_labelName", "labelValue", "_labelName", "labelValue",
), ),
expectedErr: "", expectedErr: "",
shouldPass: true,
description: "label name with _", description: "label name with _",
}, },
{ {
@ -54,7 +51,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"@labelName", "labelValue", "@labelName", "labelValue",
), ),
expectedErr: "invalid label name: @labelName", expectedErr: "invalid label name: @labelName",
shouldPass: false,
description: "label name with @", description: "label name with @",
}, },
{ {
@ -63,7 +59,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"123labelName", "labelValue", "123labelName", "labelValue",
), ),
expectedErr: "invalid label name: 123labelName", expectedErr: "invalid label name: 123labelName",
shouldPass: false,
description: "label name starts with numbers", description: "label name starts with numbers",
}, },
{ {
@ -72,7 +67,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"", "labelValue", "", "labelValue",
), ),
expectedErr: "invalid label name: ", expectedErr: "invalid label name: ",
shouldPass: false,
description: "label name is empty string", description: "label name is empty string",
}, },
{ {
@ -81,7 +75,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"labelName", string([]byte{0xff}), "labelName", string([]byte{0xff}),
), ),
expectedErr: "invalid label value: " + string([]byte{0xff}), expectedErr: "invalid label value: " + string([]byte{0xff}),
shouldPass: false,
description: "label value is an invalid UTF-8 value", description: "label value is an invalid UTF-8 value",
}, },
{ {
@ -89,7 +82,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"__name__", "@invalid_name", "__name__", "@invalid_name",
), ),
expectedErr: "invalid metric name: @invalid_name", expectedErr: "invalid metric name: @invalid_name",
shouldPass: false,
description: "metric name starts with @", description: "metric name starts with @",
}, },
{ {
@ -98,7 +90,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"__name__", "name2", "__name__", "name2",
), ),
expectedErr: "duplicate label with name: __name__", expectedErr: "duplicate label with name: __name__",
shouldPass: false,
description: "duplicate label names", description: "duplicate label names",
}, },
{ {
@ -107,7 +98,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"label2", "name", "label2", "name",
), ),
expectedErr: "", expectedErr: "",
shouldPass: true,
description: "duplicate label values", description: "duplicate label values",
}, },
{ {
@ -116,7 +106,6 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
"label2", "name", "label2", "name",
), ),
expectedErr: "invalid label name: ", expectedErr: "invalid label name: ",
shouldPass: false,
description: "don't report as duplicate label name", description: "don't report as duplicate label name",
}, },
} }
@ -124,16 +113,11 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.description, func(t *testing.T) { t.Run(test.description, func(t *testing.T) {
err := validateLabelsAndMetricName(test.input) err := validateLabelsAndMetricName(test.input)
if err == nil { if test.expectedErr != "" {
if !test.shouldPass { testutil.NotOk(t, err)
t.Fatalf("Test should fail, but passed instead.") testutil.Equals(t, test.expectedErr, err.Error())
}
} else { } else {
if test.shouldPass { testutil.Ok(t, err)
t.Fatalf("Test should pass, got unexpected error: %v", err)
} else if err.Error() != test.expectedErr {
t.Fatalf("Test should fail with: %s got unexpected error instead: %v", test.expectedErr, err)
}
} }
}) })
} }
@ -151,21 +135,11 @@ func TestConcreteSeriesSet(t *testing.T) {
c := &concreteSeriesSet{ c := &concreteSeriesSet{
series: []storage.Series{series1, series2}, series: []storage.Series{series1, series2},
} }
if !c.Next() { testutil.Assert(t, c.Next(), "Expected Next() to be true.")
t.Fatalf("Expected Next() to be true.") testutil.Equals(t, series1, c.At(), "Unexpected series returned.")
} testutil.Assert(t, c.Next(), "Expected Next() to be true.")
if c.At() != series1 { testutil.Equals(t, series2, c.At(), "Unexpected series returned.")
t.Fatalf("Unexpected series returned.") testutil.Assert(t, !c.Next(), "Expected Next() to be false.")
}
if !c.Next() {
t.Fatalf("Expected Next() to be true.")
}
if c.At() != series2 {
t.Fatalf("Unexpected series returned.")
}
if c.Next() {
t.Fatalf("Expected Next() to be false.")
}
} }
func TestConcreteSeriesClonesLabels(t *testing.T) { func TestConcreteSeriesClonesLabels(t *testing.T) {