Skip to content
Open
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
40 changes: 27 additions & 13 deletions go/tests/validation/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package validation contains shared helpers for the protovalidate tests.
// The tests for fits.api.v1 live in this package, product-specific tests
// live in subpackages mirroring the proto packages (e.g. mvm, pcvm).
package validation

import (
Expand All @@ -8,31 +11,42 @@ import (
"google.golang.org/protobuf/proto"
)

// TestUUID is a valid UUID to be used in tests.
const TestUUID = "e266fcc6-f6de-4ee1-ba26-baa17bf47b13"

type (
prototest struct {
name string
msg proto.Message
wantErr bool
wantErrorMessage string
// Prototest is a single validation test case.
Prototest struct {
// Name of the test case.
Name string
// Msg is the message to validate.
Msg proto.Message
// WantErr is true if the validation is expected to fail.
WantErr bool
// WantErrorMessage is the expected error message if WantErr is true.
WantErrorMessage string
}

prototests []prototest
// Prototests is a list of validation test cases.
Prototests []Prototest
)

func validateProtos(t *testing.T, tests prototests) {
// ValidateProtos runs protovalidate for every test case and compares the result.
func ValidateProtos(t *testing.T, tests Prototests) {
t.Helper()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := protovalidate.Validate(tt.msg)
if !tt.wantErr {
t.Run(tt.Name, func(t *testing.T) {
err := protovalidate.Validate(tt.Msg)
if !tt.WantErr {
if err != nil {
t.Errorf("validate error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("validate error = %v, wantErr %v", err, tt.WantErr)
}
return
}
if err == nil {
t.Fatalf("validate error = nil, wantErr %v", tt.wantErr)
t.Fatalf("validate error = nil, wantErr %v", tt.WantErr)
}
if diff := cmp.Diff(err.Error(), tt.wantErrorMessage); diff != "" {
if diff := cmp.Diff(err.Error(), tt.WantErrorMessage); diff != "" {
t.Errorf("validate error = %v, diff %v", err, diff)
}
})
Expand Down
58 changes: 29 additions & 29 deletions go/tests/validation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,54 @@ import (
)

func TestValidateLabels(t *testing.T) {
tests := prototests{
tests := Prototests{
{
name: "empty key",
msg: &v1.Labels{Labels: map[string]string{"": "value"}},
wantErr: true,
wantErrorMessage: `validation error: labels[""]: must be at least 1 characters`,
Name: "empty key",
Msg: &v1.Labels{Labels: map[string]string{"": "value"}},
WantErr: true,
WantErrorMessage: `validation error: labels[""]: must be at least 1 characters`,
},
{
name: "value too long",
msg: &v1.Labels{Labels: map[string]string{"key": strings.Repeat("x", 257)}},
wantErr: true,
wantErrorMessage: `validation error: labels["key"]: must be at most 256 characters`,
Name: "value too long",
Msg: &v1.Labels{Labels: map[string]string{"key": strings.Repeat("x", 257)}},
WantErr: true,
WantErrorMessage: `validation error: labels["key"]: must be at most 256 characters`,
},
{
name: "untrimmed key and value",
msg: &v1.Labels{Labels: map[string]string{" key ": " value "}},
wantErr: true,
wantErrorMessage: `validation error: labels: keys and values must not start or end with whitespace`,
Name: "untrimmed key and value",
Msg: &v1.Labels{Labels: map[string]string{" key ": " value "}},
WantErr: true,
WantErrorMessage: `validation error: labels: keys and values must not start or end with whitespace`,
},
{
name: "valid",
msg: &v1.Labels{Labels: map[string]string{"key": "value"}},
wantErr: false,
Name: "valid",
Msg: &v1.Labels{Labels: map[string]string{"key": "value"}},
WantErr: false,
},
}

validateProtos(t, tests)
ValidateProtos(t, tests)
}

func TestValidateUpdateMeta(t *testing.T) {
tests := prototests{
tests := Prototests{
{
name: "undefined locking strategy",
msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy(99)},
wantErr: true,
wantErrorMessage: `validation error: locking_strategy: value must be one of the defined enum values`,
Name: "undefined locking strategy",
Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy(99)},
WantErr: true,
WantErrorMessage: `validation error: locking_strategy: value must be one of the defined enum values`,
},
{
name: "client locking strategy",
msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_CLIENT},
wantErr: false,
Name: "client locking strategy",
Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_CLIENT},
WantErr: false,
},
{
name: "server locking strategy",
msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER},
wantErr: false,
Name: "server locking strategy",
Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER},
WantErr: false,
},
}

validateProtos(t, tests)
ValidateProtos(t, tests)
}
82 changes: 0 additions & 82 deletions go/tests/validation/contact_test.go

This file was deleted.

58 changes: 0 additions & 58 deletions go/tests/validation/domain_test.go

This file was deleted.

83 changes: 83 additions & 0 deletions go/tests/validation/mvm/contact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package mvm

import (
"testing"

mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1"
v1 "github.com/fi-ts/api/go/fits/api/v1"
"github.com/fi-ts/api/go/tests/validation"
)

const msgPhone = "must be a valid phone number in E.164 format, e.g. +4915123456789"

func TestValidateContactServiceCreateRequest(t *testing.T) {
request := func(phone string) *mvmv1.ContactServiceCreateRequest {
return &mvmv1.ContactServiceCreateRequest{
Tenant: "tenant",
FirstName: "Jane",
LastName: "Doe",
Department: "IT",
Phone: phone,
Email: "jane.doe@example.com",
}
}

tests := validation.Prototests{
{
Name: "valid",
Msg: request("+4915123456789"),
WantErr: false,
},
{
Name: "missing phone",
Msg: request(""),
WantErr: true,
WantErrorMessage: "validation error: phone: " + msgPhone,
},
{
Name: "phone without country code",
Msg: request("015123456789"),
WantErr: true,
WantErrorMessage: "validation error: phone: " + msgPhone,
},
{
Name: "phone with whitespace",
Msg: request("+49 151 23456789"),
WantErr: true,
WantErrorMessage: "validation error: phone: " + msgPhone,
},
}

validation.ValidateProtos(t, tests)
}

func TestValidateContactServiceUpdateRequest(t *testing.T) {
request := func(phone string) *mvmv1.ContactServiceUpdateRequest {
return &mvmv1.ContactServiceUpdateRequest{
Tenant: "tenant",
Uuid: validation.TestUUID,
UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER},
FirstName: "Jane",
LastName: "Doe",
Department: "IT",
Phone: phone,
Email: "jane.doe@example.com",
}
}

tests := validation.Prototests{
{
Name: "valid",
Msg: request("+4915123456789"),
WantErr: false,
},
{
Name: "invalid phone",
Msg: request("0151-23456789"),
WantErr: true,
WantErrorMessage: "validation error: phone: " + msgPhone,
},
}

validation.ValidateProtos(t, tests)
}
Loading
Loading