vitess.io/vitess@v0.16.2/go/vt/vterrors/aggregate_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 "errors" 21 "fmt" 22 "testing" 23 24 vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" 25 ) 26 27 var errGeneric = "generic error" 28 29 func errFromCode(c vtrpcpb.Code) error { 30 return New(c, errGeneric) 31 } 32 33 func TestAggregateVtGateErrorCodes(t *testing.T) { 34 var testcases = []struct { 35 input []error 36 expected vtrpcpb.Code 37 }{ 38 { 39 // aggregation of no errors is a success code 40 input: nil, 41 expected: vtrpcpb.Code_OK, 42 }, 43 { 44 // single error code gets returned directly 45 input: []error{errFromCode(vtrpcpb.Code_INVALID_ARGUMENT)}, 46 expected: vtrpcpb.Code_INVALID_ARGUMENT, 47 }, 48 { 49 // aggregate two codes to the highest priority 50 input: []error{ 51 errFromCode(vtrpcpb.Code_UNAVAILABLE), 52 errFromCode(vtrpcpb.Code_INVALID_ARGUMENT), 53 }, 54 expected: vtrpcpb.Code_INVALID_ARGUMENT, 55 }, 56 { 57 // unknown errors map to the unknown code 58 input: []error{ 59 fmt.Errorf("unknown error"), 60 }, 61 expected: vtrpcpb.Code_UNKNOWN, 62 }, 63 } 64 for _, tc := range testcases { 65 out := aggregateCodes(tc.input) 66 if out != tc.expected { 67 t.Errorf("AggregateVtGateErrorCodes(%v) = %v \nwant: %v", 68 tc.input, out, tc.expected) 69 } 70 } 71 } 72 73 func TestAggregateVtGateErrors(t *testing.T) { 74 var testcases = []struct { 75 input []error 76 expected error 77 }{ 78 { 79 input: nil, 80 expected: nil, 81 }, 82 { 83 input: []error{ 84 errFromCode(vtrpcpb.Code_UNAVAILABLE), 85 errFromCode(vtrpcpb.Code_INVALID_ARGUMENT), 86 }, 87 expected: New( 88 vtrpcpb.Code_INVALID_ARGUMENT, 89 aggregateErrors([]error{ 90 errors.New(errGeneric), 91 errors.New(errGeneric), 92 }), 93 ), 94 }, 95 } 96 for _, tc := range testcases { 97 out := Aggregate(tc.input) 98 if !Equals(out, tc.expected) { 99 t.Errorf("AggregateVtGateErrors(%+v) = %+v \nwant: %+v", 100 tc.input, out, tc.expected) 101 } 102 } 103 }