...

Source file src/go.einride.tech/aip/validation/error_test.go

Documentation: go.einride.tech/aip/validation

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"google.golang.org/genproto/googleapis/rpc/errdetails"
     7  	"google.golang.org/grpc/codes"
     8  	"google.golang.org/grpc/status"
     9  	"google.golang.org/protobuf/testing/protocmp"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestError_NewError(t *testing.T) {
    15  	t.Parallel()
    16  	t.Run("panics on empty field violations", func(t *testing.T) {
    17  		t.Parallel()
    18  		assert.Assert(t, cmp.Panics(func() {
    19  			_ = NewError(nil)
    20  		}))
    21  	})
    22  }
    23  
    24  func TestError_Error(t *testing.T) {
    25  	t.Parallel()
    26  	err := NewError([]*errdetails.BadRequest_FieldViolation{
    27  		{Field: "foo.bar", Description: "test"},
    28  		{Field: "baz", Description: "test2"},
    29  	})
    30  	assert.Error(t, err, `field violation on multiple fields:
    31   | foo.bar: test
    32   | baz: test2`)
    33  }
    34  
    35  func TestError_GRPCStatus(t *testing.T) {
    36  	t.Parallel()
    37  	expected := &errdetails.BadRequest{
    38  		FieldViolations: []*errdetails.BadRequest_FieldViolation{
    39  			{Field: "foo.bar", Description: "test"},
    40  			{Field: "baz", Description: "test2"},
    41  		},
    42  	}
    43  	s := status.Convert(NewError(expected.GetFieldViolations()))
    44  	assert.Equal(t, codes.InvalidArgument, s.Code())
    45  	assert.Equal(t, "invalid fields: foo.bar, baz", s.Message())
    46  	details := s.Details()
    47  	assert.Assert(t, len(details) == 1)
    48  	actual, ok := details[0].(*errdetails.BadRequest)
    49  	assert.Assert(t, ok)
    50  	assert.DeepEqual(t, expected, actual, protocmp.Transform())
    51  }
    52  

View as plain text