github.com/XiaoMi/Gaea@v1.2.5/mysql/field_test.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mysql
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  var errTimeTpl = "invalid TypeDuration %s"
    23  
    24  func Test_StringToMysqlTime(t *testing.T) {
    25  	tests := []struct {
    26  		timestr string
    27  		value   TimeValue
    28  		err     error
    29  	}{
    30  		{"00:00", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00")},
    31  		{"00:00:00", TimeValue{}, nil},
    32  		{"00:00:00.", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00:00.")},
    33  		{"00:00:00.00", TimeValue{}, nil},
    34  		{"00:00:00.000001", TimeValue{Microsecond: 1}, nil},
    35  		{"00:00:00.001", TimeValue{Microsecond: 1000}, nil},
    36  		{"00:00:00.999999", TimeValue{Microsecond: 999999}, nil},
    37  		{"00:00:00.9999991", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00:00.9999991")},
    38  		{"00:00:00.-01", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00:00.-01")},
    39  		{"00:00:01", TimeValue{Second: 1}, nil},
    40  		{"00:00:59", TimeValue{Second: 59}, nil},
    41  		{"00:00:60", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00:60")},
    42  		{"00:00:-01", TimeValue{}, fmt.Errorf(errTimeTpl, "00:00:-01")},
    43  		{"00:01:00", TimeValue{Minute: 1}, nil},
    44  		{"00:59:00", TimeValue{Minute: 59}, nil},
    45  		{"00:60:00", TimeValue{}, fmt.Errorf(errTimeTpl, "00:60:00")},
    46  		{"00:-01:00", TimeValue{}, fmt.Errorf(errTimeTpl, "00:-01:00")},
    47  		{"00:1.5:00", TimeValue{}, fmt.Errorf(errTimeTpl, "00:1.5:00")},
    48  		{"01:00:00", TimeValue{Hour: 1}, nil},
    49  		{"23:00:00", TimeValue{Hour: 23}, nil},
    50  		{"24:00:00", TimeValue{Day: 1, Hour: 0}, nil},
    51  		{"25:00:00", TimeValue{Day: 1, Hour: 1}, nil},
    52  		{"48:00:00", TimeValue{Day: 2, Hour: 0}, nil},
    53  		{"1.1:00:00", TimeValue{}, fmt.Errorf(errTimeTpl, "1.1:00:00")},
    54  
    55  		// negative time
    56  		{"-00:00:00.00", TimeValue{IsNegative: true}, nil}, // mysql doesn't return this value
    57  		{"-00:00:00.000001", TimeValue{IsNegative: true, Microsecond: 1}, nil},
    58  		{"-00:00:00.001", TimeValue{IsNegative: true, Microsecond: 1000}, nil},
    59  		{"-00:00:00.999999", TimeValue{IsNegative: true, Microsecond: 999999}, nil},
    60  		{"-00:00:00.9999991", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:00:00.9999991")},
    61  		{"-00:00:00.-01", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:00:00.-01")},
    62  		{"-00:00:01", TimeValue{IsNegative: true, Second: 1}, nil},
    63  		{"-00:00:59", TimeValue{IsNegative: true, Second: 59}, nil},
    64  		{"-00:00:60", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:00:60")},
    65  		{"-00:00:-01", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:00:-01")},
    66  		{"-00:01:00", TimeValue{IsNegative: true, Minute: 1}, nil},
    67  		{"-00:59:00", TimeValue{IsNegative: true, Minute: 59}, nil},
    68  		{"-00:60:00", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:60:00")},
    69  		{"-00:-01:00", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:-01:00")},
    70  		{"-00:1.5:00", TimeValue{}, fmt.Errorf(errTimeTpl, "-00:1.5:00")},
    71  		{"-01:00:00", TimeValue{IsNegative: true, Hour: 1}, nil},
    72  		{"-23:00:00", TimeValue{IsNegative: true, Hour: 23}, nil},
    73  		{"-24:00:00", TimeValue{IsNegative: true, Day: 1, Hour: 0}, nil},
    74  		{"-25:00:00", TimeValue{IsNegative: true, Day: 1, Hour: 1}, nil},
    75  		{"-48:00:00", TimeValue{IsNegative: true, Day: 2, Hour: 0}, nil},
    76  		{"-1.1:00:00", TimeValue{}, fmt.Errorf(errTimeTpl, "-1.1:00:00")},
    77  	}
    78  	for _, test := range tests {
    79  		t.Run(test.timestr, func(t *testing.T) {
    80  			v, err := stringToMysqlTime(test.timestr)
    81  			if err != nil {
    82  				if test.err == nil {
    83  					t.Errorf("expect no error, actual err: %v", err)
    84  					t.FailNow()
    85  				}
    86  				if err.Error() != test.err.Error() {
    87  					t.Errorf("error not equal, expect: %v, actual: %v", test.err, err)
    88  					t.FailNow()
    89  				}
    90  			} else {
    91  				if test.err != nil {
    92  					t.Errorf("expect error: %v, actual no error, value: %v", test.err, v)
    93  					t.FailNow()
    94  				}
    95  
    96  				if v != test.value {
    97  					t.Errorf("result not equal, str: %s, expect: %v, actual: %v", test.timestr, test.value, v)
    98  					t.FailNow()
    99  				}
   100  			}
   101  		})
   102  	}
   103  }