vitess.io/vitess@v0.16.2/go/vt/vterrors/proto3_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vterrors
    18  
    19  import (
    20  	"testing"
    21  
    22  	"google.golang.org/protobuf/proto"
    23  
    24  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    25  )
    26  
    27  func TestFromVtRPCError(t *testing.T) {
    28  	testcases := []struct {
    29  		in   *vtrpcpb.RPCError
    30  		want error
    31  	}{{
    32  		in:   nil,
    33  		want: nil,
    34  	}, {
    35  		in: &vtrpcpb.RPCError{
    36  			Code:    vtrpcpb.Code_INVALID_ARGUMENT,
    37  			Message: "bad input",
    38  		},
    39  		want: New(vtrpcpb.Code_INVALID_ARGUMENT, "bad input"),
    40  	}, {
    41  		in: &vtrpcpb.RPCError{
    42  			Message: "bad input",
    43  			Code:    vtrpcpb.Code_INVALID_ARGUMENT,
    44  		},
    45  		want: New(vtrpcpb.Code_INVALID_ARGUMENT, "bad input"),
    46  	}, {
    47  		in: &vtrpcpb.RPCError{
    48  			Message: "bad input",
    49  			Code:    vtrpcpb.Code_INVALID_ARGUMENT,
    50  		},
    51  		want: New(vtrpcpb.Code_INVALID_ARGUMENT, "bad input"),
    52  	}}
    53  	for _, tcase := range testcases {
    54  		got := FromVTRPC(tcase.in)
    55  		if !Equals(got, tcase.want) {
    56  			t.Errorf("FromVtRPCError(%v): [%v], want [%v]", tcase.in, got, tcase.want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestVtRPCErrorFromVtError(t *testing.T) {
    62  	testcases := []struct {
    63  		in   error
    64  		want *vtrpcpb.RPCError
    65  	}{{
    66  		in:   nil,
    67  		want: nil,
    68  	}, {
    69  		in: New(vtrpcpb.Code_INVALID_ARGUMENT, "bad input"),
    70  		want: &vtrpcpb.RPCError{
    71  			Message: "bad input",
    72  			Code:    vtrpcpb.Code_INVALID_ARGUMENT,
    73  		},
    74  	}}
    75  	for _, tcase := range testcases {
    76  		got := ToVTRPC(tcase.in)
    77  		if !proto.Equal(got, tcase.want) {
    78  			t.Errorf("VtRPCErrorFromVtError(%v): %v, want %v", tcase.in, got, tcase.want)
    79  		}
    80  	}
    81  }