github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/duration/duration_test.go (about) 1 // Copyright 2023 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package duration 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestParseDuration(t *testing.T) { 24 cases := []struct { 25 str string 26 duration time.Duration 27 }{ 28 { 29 "1h", 30 time.Hour, 31 }, 32 { 33 "1h100m", 34 time.Hour + 100*time.Minute, 35 }, 36 { 37 "1d10000m", 38 24*time.Hour + 10000*time.Minute, 39 }, 40 { 41 "1d100h", 42 24*time.Hour + 100*time.Hour, 43 }, 44 { 45 "1.5d", 46 36 * time.Hour, 47 }, 48 { 49 "1d1.5h", 50 24*time.Hour + time.Hour + 30*time.Minute, 51 }, 52 { 53 "1d3.555h", 54 24*time.Hour + time.Duration(3.555*float64(time.Hour)), 55 }, 56 } 57 58 for _, c := range cases { 59 t.Run(c.str, func(t *testing.T) { 60 d, err := ParseDuration(c.str) 61 require.NoError(t, err) 62 require.Equal(t, c.duration, d) 63 }) 64 } 65 }