vitess.io/vitess@v0.16.2/go/protoutil/duration_test.go (about) 1 /* 2 Copyright 2021 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 protoutil 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 25 "vitess.io/vitess/go/vt/proto/vttime" 26 ) 27 28 func TestDurationFromProto(t *testing.T) { 29 t.Parallel() 30 31 tests := []struct { 32 name string 33 in *vttime.Duration 34 expected time.Duration 35 isOk bool 36 shouldErr bool 37 }{ 38 { 39 name: "success", 40 in: &vttime.Duration{Seconds: 1000}, 41 expected: time.Second * 1000, 42 isOk: true, 43 shouldErr: false, 44 }, 45 { 46 name: "nil value", 47 in: nil, 48 expected: 0, 49 isOk: false, 50 shouldErr: false, 51 }, 52 { 53 name: "error", 54 in: &vttime.Duration{ 55 // This is the max allowed seconds for a durationpb, plus 1. 56 Seconds: int64(10000*365.25*24*60*60) + 1, 57 }, 58 expected: 0, 59 isOk: true, 60 shouldErr: true, 61 }, 62 } 63 64 for _, tt := range tests { 65 tt := tt 66 67 t.Run(tt.name, func(t *testing.T) { 68 t.Parallel() 69 70 actual, ok, err := DurationFromProto(tt.in) 71 if tt.shouldErr { 72 assert.Error(t, err) 73 assert.Equal(t, tt.isOk, ok, "expected (_, ok, _) = DurationFromProto; to be ok = %v", tt.isOk) 74 return 75 } 76 77 assert.NoError(t, err) 78 assert.Equal(t, tt.expected, actual) 79 assert.Equal(t, tt.isOk, ok, "expected (_, ok, _) = DurationFromProto; to be ok = %v", tt.isOk) 80 }) 81 } 82 }