github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/tests/case_many_types.go (about)

     1  // Copyright 2020 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  	"math"
    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  // ManyTypesCase is base impl of test case for different types data
    28  type ManyTypesCase struct {
    29  	framework.Task
    30  }
    31  
    32  // NewManyTypesCase create a test case which has many types
    33  func NewManyTypesCase(task framework.Task) *ManyTypesCase {
    34  	return &ManyTypesCase{
    35  		Task: task,
    36  	}
    37  }
    38  
    39  // Name impl framework.Task interface
    40  func (s *ManyTypesCase) Name() string {
    41  	return "Many Types"
    42  }
    43  
    44  // Run impl framework.Task interface
    45  func (s *ManyTypesCase) 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_boolean   BOOLEAN,
    52  						t_bigint    BIGINT,
    53  						t_double    DOUBLE,
    54  						t_float     FLOAT,
    55  						t_decimal   DECIMAL(38, 19),
    56  						t_bit       BIT(64),
    57  						t_date      DATE,
    58  						t_datetime  DATETIME,
    59  						t_timestamp TIMESTAMP NULL,
    60  						t_time      TIME,
    61  						t_year      YEAR,
    62  						t_char      CHAR,
    63  						t_varchar   VARCHAR(10),
    64  						t_blob      BLOB,
    65  						t_text      TEXT,
    66  						t_enum      ENUM ('enum1', 'enum2', 'enum3'),
    67  						t_set       SET ('a', 'b', 'c'),
    68  						t_json      JSON,
    69  						PRIMARY KEY (id)
    70  					)`
    71  	case *canal.SingleTableTask:
    72  		createDBQuery = `create table test (
    73  						id          INT,
    74  						t_boolean   BOOLEAN,
    75  						t_bigint    BIGINT,
    76  						t_double    DOUBLE,
    77  						t_float     FLOAT,
    78  						t_decimal   DECIMAL(38, 19),
    79  						t_date      DATE,
    80  						t_datetime  DATETIME,
    81  						t_timestamp TIMESTAMP NULL,
    82  						t_time      TIME,
    83  						t_char      CHAR,
    84  						t_varchar   VARCHAR(10),
    85  						t_blob      BLOB,
    86  						t_text      TEXT,
    87  						t_enum      ENUM ('enum1', 'enum2', 'enum3'),
    88  						t_set       SET ('a', 'b', 'c'),
    89  						t_json      JSON,
    90  						PRIMARY KEY (id)
    91  					)`
    92  	case *mysql.SingleTableTask:
    93  		createDBQuery = `create table test (
    94  						id          INT,
    95  						t_boolean   BOOLEAN,
    96  						t_bigint    BIGINT,
    97  						t_double    DOUBLE,
    98  						t_float     FLOAT,
    99  						t_decimal   DECIMAL(38, 19),
   100  						t_bit       BIT(64),
   101  						t_date      DATE,
   102  						t_datetime  DATETIME,
   103  						t_timestamp TIMESTAMP NULL,
   104  						t_time      TIME,
   105  						t_year      YEAR,
   106  						t_char      CHAR,
   107  						t_varchar   VARCHAR(10),
   108  						t_blob      BLOB,
   109  						t_text      TEXT,
   110  						t_enum      ENUM ('enum1', 'enum2', 'enum3'),
   111  						t_set       SET ('a', 'b', 'c'),
   112  						t_json      JSON,
   113  						PRIMARY KEY (id)
   114  					)`
   115  	default:
   116  		return errors.New("unknown test case type")
   117  	}
   118  
   119  	_, err := ctx.Upstream.ExecContext(ctx.Ctx, createDBQuery)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	if _, ok := s.Task.(*avro.SingleTableTask); ok {
   124  		_, err = ctx.Downstream.ExecContext(ctx.Ctx, "drop table if exists test")
   125  		if err != nil {
   126  			return err
   127  		}
   128  
   129  		_, err = ctx.Downstream.ExecContext(ctx.Ctx, createDBQuery)
   130  		if err != nil {
   131  			return err
   132  		}
   133  	}
   134  
   135  	// Get a handle of an existing table
   136  	table := ctx.SQLHelper().GetTable("test")
   137  	data := map[string]interface{}{
   138  		"id":          0,
   139  		"t_boolean":   true,
   140  		"t_bigint":    math.MaxInt64,
   141  		"t_double":    1.01234,
   142  		"t_float":     2.45678,
   143  		"t_decimal":   "12345.6789",
   144  		"t_date":      time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
   145  		"t_datetime":  time.Now(),
   146  		"t_timestamp": time.Now(),
   147  		"t_time":      "23:59:59",
   148  		"t_char":      "a",
   149  		"t_varchar":   "测试varchar",
   150  		"t_blob":      []byte{0x1, 0x2, 0x0, 0x3, 0x4},
   151  		"t_text":      "测试text",
   152  		"t_enum":      "enum2",
   153  		"t_set":       "a,b",
   154  		"t_json":      nil,
   155  	}
   156  	_, ok := s.Task.(*avro.SingleTableTask)
   157  	if ok {
   158  		data["t_year"] = 2019
   159  		data["t_bit"] = 0b1001001
   160  	}
   161  	return table.Insert(data).Send().Wait().Check()
   162  }