Skip to content

Commit 7afe27d

Browse files
authored
Simplify validation tests (#56)
1 parent 5ed032f commit 7afe27d

File tree

9 files changed

+10
-49
lines changed

9 files changed

+10
-49
lines changed

go/tests/validation/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ type (
1919
prototests []prototest
2020
)
2121

22-
func validateProtos(t *testing.T, tests prototests, validator protovalidate.Validator) {
22+
func validateProtos(t *testing.T, tests prototests) {
2323
for _, tt := range tests {
2424
t.Run(tt.name, func(t *testing.T) {
25-
err := validator.Validate(tt.msg)
25+
err := protovalidate.Validate(tt.msg)
2626
if err != nil && !tt.wantErr {
2727
t.Errorf("validate error = %v, wantErr %v", err, tt.wantErr)
2828
}

go/tests/validation/filesystem_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
"github.com/metal-stack/api/go/enum"
87
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
98
"github.com/stretchr/testify/require"
109
"google.golang.org/protobuf/proto"
1110
)
1211

1312
func TestValidateFilesystem(t *testing.T) {
14-
validator, err := protovalidate.New()
15-
require.NoError(t, err)
16-
1713
tests := prototests{
1814
{
1915
name: "Valid Filesystem minimal config",
@@ -71,7 +67,7 @@ func TestValidateFilesystem(t *testing.T) {
7167
- name: must be within 2 and 128 characters [string.is_name]`},
7268
}
7369

74-
validateProtos(t, tests, validator)
70+
validateProtos(t, tests)
7571
}
7672

7773
func TestGetStringValue(t *testing.T) {

go/tests/validation/firewall-rule_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8-
"github.com/stretchr/testify/require"
97
)
108

119
func TestValidateFirewallRules(t *testing.T) {
12-
validator, err := protovalidate.New()
13-
require.NoError(t, err)
14-
1510
tests := prototests{
1611
{
1712
name: "Invalid Rule with valid comment",
@@ -40,5 +35,5 @@ func TestValidateFirewallRules(t *testing.T) {
4035
},
4136
}
4237

43-
validateProtos(t, tests, validator)
38+
validateProtos(t, tests)
4439
}

go/tests/validation/image_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
87
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
9-
"github.com/stretchr/testify/require"
108
"google.golang.org/protobuf/proto"
119
)
1210

1311
func TestValidateImage(t *testing.T) {
14-
validator, err := protovalidate.New()
15-
require.NoError(t, err)
16-
1712
tests := prototests{
1813
{
1914
name: "Valid Image minimal config",
@@ -86,5 +81,5 @@ func TestValidateImage(t *testing.T) {
8681
},
8782
}
8883

89-
validateProtos(t, tests, validator)
84+
validateProtos(t, tests)
9085
}

go/tests/validation/ip_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8-
"github.com/stretchr/testify/require"
97
"google.golang.org/protobuf/proto"
108
)
119

1210
func TestValidateIP(t *testing.T) {
13-
validator, err := protovalidate.New()
14-
require.NoError(t, err)
15-
1611
tests := prototests{
1712
{
1813
name: "Invalid IP",
@@ -168,5 +163,5 @@ func TestValidateIP(t *testing.T) {
168163
},
169164
}
170165

171-
validateProtos(t, tests, validator)
166+
validateProtos(t, tests)
172167
}

go/tests/validation/machine_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8-
"github.com/stretchr/testify/require"
97
)
108

119
func TestValidateMachine(t *testing.T) {
12-
validator, err := protovalidate.New()
13-
require.NoError(t, err)
14-
1510
tests := prototests{
1611
{
1712
name: "MachineNicQuery with invalid MACs",
@@ -55,5 +50,5 @@ func TestValidateMachine(t *testing.T) {
5550
- ips: given ips must be valid [repeated.ips]`,
5651
},
5752
}
58-
validateProtos(t, tests, validator)
53+
validateProtos(t, tests)
5954
}

go/tests/validation/network_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
87
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
9-
"github.com/stretchr/testify/require"
108
"google.golang.org/protobuf/proto"
119
)
1210

1311
func TestValidateNetwork(t *testing.T) {
14-
validator, err := protovalidate.New()
15-
require.NoError(t, err)
16-
1712
tests := prototests{
1813
{
1914
name: "Valid NetworkCreateRequest minimal config",
@@ -57,5 +52,5 @@ func TestValidateNetwork(t *testing.T) {
5752
- destination_prefixes: given prefixes must be valid [repeated.prefixes]`,
5853
},
5954
}
60-
validateProtos(t, tests, validator)
55+
validateProtos(t, tests)
6156
}

go/tests/validation/switch_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8-
"github.com/stretchr/testify/require"
97
"google.golang.org/protobuf/proto"
108
)
119

1210
func TestValidateSwitch(t *testing.T) {
13-
validator, err := protovalidate.New()
14-
require.NoError(t, err)
15-
1611
tests := prototests{
1712
{
1813
name: "SwitchNic with invalid MAC",
@@ -71,5 +66,5 @@ func TestValidateSwitch(t *testing.T) {
7166
- id: value must be a valid hostname [string.hostname]`,
7267
},
7368
}
74-
validateProtos(t, tests, validator)
69+
validateProtos(t, tests)
7570
}

go/tests/validation/token_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ package validation
33
import (
44
"testing"
55

6-
"buf.build/go/protovalidate"
76
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8-
"github.com/stretchr/testify/require"
97
)
108

119
func TestValidateToken(t *testing.T) {
12-
validator, err := protovalidate.New()
13-
require.NoError(t, err)
14-
1510
tests := prototests{
1611
{
1712
name: "Valid Token Create Request",
@@ -42,5 +37,5 @@ func TestValidateToken(t *testing.T) {
4237
},
4338
}
4439

45-
validateProtos(t, tests, validator)
40+
validateProtos(t, tests)
4641
}

0 commit comments

Comments
 (0)