github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/tests/case_date_time.go (about) 1 // Copyright 2021 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 tests 15 16 import ( 17 "errors" 18 "log" 19 "time" 20 21 "github.com/pingcap/ticdc/integration/framework" 22 "github.com/pingcap/ticdc/integration/framework/avro" 23 "github.com/pingcap/ticdc/integration/framework/canal" 24 "github.com/pingcap/ticdc/integration/framework/mysql" 25 ) 26 27 // DateTimeCase is base impl of test case for different types data 28 type DateTimeCase struct { 29 framework.Task 30 } 31 32 // NewDateTimeCase create a test case which has many types 33 func NewDateTimeCase(task framework.Task) *DateTimeCase { 34 return &DateTimeCase{ 35 Task: task, 36 } 37 } 38 39 // Name impl framework.Task interface 40 func (s *DateTimeCase) Name() string { 41 return "Date Time" 42 } 43 44 // Run impl framework.Task interface 45 func (s *DateTimeCase) Run(ctx *framework.TaskContext) error { 46 var createDBQuery string 47 switch s.Task.(type) { 48 case *avro.SingleTableTask: 49 createDBQuery = `create table test ( 50 id INT, 51 t_date DATE, 52 t_datetime DATETIME, 53 t_timestamp TIMESTAMP NULL, 54 PRIMARY KEY (id) 55 )` 56 case *canal.SingleTableTask, *mysql.SingleTableTask: 57 log.Panic("DateTimeCase does not support downstreams other than Avro") 58 default: 59 return errors.New("unknown test case type") 60 } 61 62 _, err := ctx.Upstream.ExecContext(ctx.Ctx, createDBQuery) 63 if err != nil { 64 return err 65 } 66 if _, ok := s.Task.(*avro.SingleTableTask); ok { 67 _, err = ctx.Downstream.ExecContext(ctx.Ctx, "drop table if exists test") 68 if err != nil { 69 return err 70 } 71 72 _, err = ctx.Downstream.ExecContext(ctx.Ctx, createDBQuery) 73 if err != nil { 74 return err 75 } 76 } 77 78 // Get a handle of an existing table 79 table := ctx.SQLHelper().GetTable("test") 80 81 // Zero value case 82 zeroValue := time.Unix(0, 0) 83 data := map[string]interface{}{ 84 "id": 0, 85 "t_date": zeroValue, 86 "t_datetime": zeroValue, 87 "t_timestamp": zeroValue.Add(time.Second), 88 } 89 err = table.Insert(data).Send().Wait().Check() 90 if err != nil { 91 return err 92 } 93 94 // Ancient date case. We DO NOT support it. 95 // TODO investigate why and find out a solution 96 /* ancientTime := time.Date(960, 1, 1, 15, 33, 0, 0, time.UTC) 97 data = map[string]interface{}{ 98 "id": 1, 99 "t_date": ancientTime, 100 "t_datetime": ancientTime, 101 "t_timestamp": zeroValue.Add(time.Second), // Timestamp does not support the Zero value of `time.Time`, so we test the Unix epoch instead 102 } 103 err = table.Insert(data).Send().Wait().Check() 104 if err != nil { 105 return err 106 } 107 */ 108 109 return nil 110 }