vitess.io/vitess@v0.16.2/go/vt/vitessdriver/time_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 vitessdriver 18 19 import ( 20 "reflect" 21 "testing" 22 "time" 23 24 "vitess.io/vitess/go/sqltypes" 25 ) 26 27 var randomLocation = time.FixedZone("Nowhere", 3*60*60) 28 29 func DatetimeValue(str string) sqltypes.Value { 30 return sqltypes.TestValue(sqltypes.Datetime, str) 31 } 32 33 func DateValue(str string) sqltypes.Value { 34 return sqltypes.TestValue(sqltypes.Date, str) 35 } 36 37 func TestDatetimeToNative(t *testing.T) { 38 39 tcases := []struct { 40 val sqltypes.Value 41 loc *time.Location 42 out time.Time 43 err bool 44 }{{ 45 val: DatetimeValue("1899-08-24 17:20:00"), 46 out: time.Date(1899, 8, 24, 17, 20, 0, 0, time.UTC), 47 }, { 48 val: DatetimeValue("1952-03-11 01:02:03"), 49 loc: time.Local, 50 out: time.Date(1952, 3, 11, 1, 2, 3, 0, time.Local), 51 }, { 52 val: DatetimeValue("1952-03-11 01:02:03"), 53 loc: randomLocation, 54 out: time.Date(1952, 3, 11, 1, 2, 3, 0, randomLocation), 55 }, { 56 val: DatetimeValue("1952-03-11 01:02:03"), 57 loc: time.UTC, 58 out: time.Date(1952, 3, 11, 1, 2, 3, 0, time.UTC), 59 }, { 60 val: DatetimeValue("1899-08-24 17:20:00.000000"), 61 out: time.Date(1899, 8, 24, 17, 20, 0, 0, time.UTC), 62 }, { 63 val: DatetimeValue("1899-08-24 17:20:00.000001"), 64 out: time.Date(1899, 8, 24, 17, 20, 0, int(1*time.Microsecond), time.UTC), 65 }, { 66 val: DatetimeValue("1899-08-24 17:20:00.123456"), 67 out: time.Date(1899, 8, 24, 17, 20, 0, int(123456*time.Microsecond), time.UTC), 68 }, { 69 val: DatetimeValue("1899-08-24 17:20:00.222"), 70 out: time.Date(1899, 8, 24, 17, 20, 0, int(222*time.Millisecond), time.UTC), 71 }, { 72 val: DatetimeValue("1899-08-24 17:20:00.1234567"), 73 err: true, 74 }, { 75 val: DatetimeValue("1899-08-24 17:20:00.1"), 76 out: time.Date(1899, 8, 24, 17, 20, 0, int(100*time.Millisecond), time.UTC), 77 }, { 78 val: DatetimeValue("0000-00-00 00:00:00"), 79 out: time.Time{}, 80 }, { 81 val: DatetimeValue("0000-00-00 00:00:00.0"), 82 out: time.Time{}, 83 }, { 84 val: DatetimeValue("0000-00-00 00:00:00.000"), 85 out: time.Time{}, 86 }, { 87 val: DatetimeValue("0000-00-00 00:00:00.000000"), 88 out: time.Time{}, 89 }, { 90 val: DatetimeValue("0000-00-00 00:00:00.0000000"), 91 err: true, 92 }, { 93 val: DatetimeValue("1899-08-24T17:20:00.000000"), 94 err: true, 95 }, { 96 val: DatetimeValue("1899-02-31 17:20:00.000000"), 97 err: true, 98 }, { 99 val: DatetimeValue("1899-08-24 17:20:00."), 100 out: time.Date(1899, 8, 24, 17, 20, 0, 0, time.UTC), 101 }, { 102 val: DatetimeValue("0000-00-00 00:00:00.000001"), 103 err: true, 104 }, { 105 val: DatetimeValue("1899-08-24 17:20:00 +02:00"), 106 err: true, 107 }, { 108 val: DatetimeValue("1899-08-24"), 109 err: true, 110 }, { 111 val: DatetimeValue("This is not a valid timestamp"), 112 err: true, 113 }} 114 115 for _, tcase := range tcases { 116 got, err := DatetimeToNative(tcase.val, tcase.loc) 117 if tcase.err && err == nil { 118 t.Errorf("DatetimeToNative(%v, %#v) succeeded; expected error", tcase.val, tcase.loc) 119 } 120 if !tcase.err && err != nil { 121 t.Errorf("DatetimeToNative(%v, %#v) failed: %v", tcase.val, tcase.loc, err) 122 } 123 if !reflect.DeepEqual(got, tcase.out) { 124 t.Errorf("DatetimeToNative(%v, %#v): %v, want %v", tcase.val, tcase.loc, got, tcase.out) 125 } 126 } 127 } 128 129 func TestDateToNative(t *testing.T) { 130 tcases := []struct { 131 val sqltypes.Value 132 loc *time.Location 133 out time.Time 134 err bool 135 }{{ 136 val: DateValue("1899-08-24"), 137 out: time.Date(1899, 8, 24, 0, 0, 0, 0, time.UTC), 138 }, { 139 val: DateValue("1952-03-11"), 140 loc: time.Local, 141 out: time.Date(1952, 3, 11, 0, 0, 0, 0, time.Local), 142 }, { 143 val: DateValue("1952-03-11"), 144 loc: randomLocation, 145 out: time.Date(1952, 3, 11, 0, 0, 0, 0, randomLocation), 146 }, { 147 val: DateValue("0000-00-00"), 148 out: time.Time{}, 149 }, { 150 val: DateValue("1899-02-31"), 151 err: true, 152 }, { 153 val: DateValue("1899-08-24 17:20:00"), 154 err: true, 155 }, { 156 val: DateValue("0000-00-00 00:00:00"), 157 err: true, 158 }, { 159 val: DateValue("This is not a valid timestamp"), 160 err: true, 161 }} 162 163 for _, tcase := range tcases { 164 got, err := DateToNative(tcase.val, tcase.loc) 165 if tcase.err && err == nil { 166 t.Errorf("DateToNative(%v, %#v) succeeded; expected error", tcase.val, tcase.loc) 167 } 168 if !tcase.err && err != nil { 169 t.Errorf("DateToNative(%v, %#v) failed: %v", tcase.val, tcase.loc, err) 170 } 171 if !reflect.DeepEqual(got, tcase.out) { 172 t.Errorf("DateToNative(%v, %#v): %v, want %v", tcase.val, tcase.loc, got, tcase.out) 173 } 174 } 175 }