github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/utils/test_utils.go (about)

     1  // Copyright 2022 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 utils
    15  
    16  import (
    17  	"database/sql/driver"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/cdc/entry"
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  	"github.com/pingcap/tiflow/pkg/config"
    23  )
    24  
    25  // NewLargeEvent4Test creates large events for test
    26  func NewLargeEvent4Test(t *testing.T, replicaConfig *config.ReplicaConfig) (*model.DDLEvent, *model.RowChangedEvent, *model.RowChangedEvent, *model.RowChangedEvent) {
    27  	helper := entry.NewSchemaTestHelperWithReplicaConfig(t, replicaConfig)
    28  	defer helper.Close()
    29  
    30  	sql := `create table test.t(
    31      	t tinyint primary key,
    32  		tu1 tinyint unsigned default 1,
    33  		tu2 tinyint unsigned default 2,
    34  		tu3 tinyint unsigned default 3,
    35  		tu4 tinyint unsigned default 4,
    36  		s smallint default 5,
    37  		su1 smallint unsigned default 6,
    38  		su2 smallint unsigned default 7,
    39  		su3 smallint unsigned default 8,
    40  		su4 smallint unsigned default 9,
    41  		m mediumint default 10,
    42  		mu1 mediumint unsigned default 11,
    43  		mu2 mediumint unsigned default 12,
    44  		mu3 mediumint unsigned default 13,
    45  		mu4 mediumint unsigned default 14,
    46  		i int default 15,
    47  		iu1 int unsigned default 16,
    48  		iu2 int unsigned default 17,
    49  		iu3 int unsigned default 18,
    50  		iu4 int unsigned default 19,
    51  		bi bigint default 20,
    52  		biu1 bigint unsigned default 21,
    53  		biu2 bigint unsigned default 22,
    54  		biu3 bigint unsigned default 23,
    55  		biu4 bigint unsigned default 24,
    56  		floatT float default 3.14,
    57  		doubleT double default 2.7182818284,
    58  	 	decimalT decimal(12, 6) default 179394.2333,
    59  	 	floatTu float unsigned default 3.14,
    60  		doubleTu double unsigned default 2.7182818284,
    61  	 	decimalTu decimal(12, 6) unsigned default 179394.2333,
    62  	 	decimalTu2 decimal(5, 4) unsigned default 3.1415,
    63  	 	varcharT varchar(255) default '测试Varchar default',
    64  	 	charT char(255) default '测试Char default',
    65  	 	binaryT binary(255) default '测试Binary default',
    66  	 	varbinaryT varbinary(255) default '测试varbinary default',
    67  	 	tinytextT tinytext,
    68  	 	textT text,
    69  	 	mediumtextT mediumtext,
    70  	 	longtextT longtext,
    71  	 	tinyblobT tinyblob,
    72  	 	blobT blob,
    73  	 	mediumblobT mediumblob,
    74  	 	longblobT longblob,
    75  	 	dateT date default '2023-12-27',
    76  	 	datetimeT datetime default '2023-12-27 12:27:23',
    77  	 	timestampT timestamp default now(),
    78  	 	timestampT2 timestamp(6) default '2024-03-11 08:51:01.461270',
    79  	 	timeT time default '12:27:23',
    80  	 	yearT year default 2023,
    81  	 	enumT enum('a', 'b', 'c') default 'b',
    82  	 	setT set('a', 'b', 'c') default 'c',
    83  	 	bitT bit(10) default b'1010101010',
    84  	 	jsonT json)`
    85  	ddlEvent := helper.DDL2Event(sql)
    86  
    87  	sql = `insert into test.t values(
    88  		127,
    89  		127,
    90  		128,
    91  		0,
    92  		null,
    93  		32767,
    94  		32767,
    95  		32768,
    96  		0,
    97  		null,
    98  		8388607,
    99  		8388607,
   100  		8388608,
   101  		0,
   102  		null,
   103  		2147483647,
   104  		2147483647,
   105  		2147483648,
   106  		0,
   107  		null,
   108  		9223372036854775807,
   109  		9223372036854775807,
   110  		9223372036854775808,
   111  		0,
   112  		null,
   113  		3.14,
   114  		2.71,
   115  		2333.654321,
   116  		3.14,
   117  		2.71,
   118  		2333.123456,
   119          1.7371,
   120  		'测试Varchar',
   121  		'测试String',
   122  		'测试Binary',
   123  		'测试varbinary',
   124  		'测试Tinytext',
   125  		'测试text',
   126  		'测试mediumtext',
   127  		'测试longtext',
   128  		'测试tinyblob',
   129  		'测试blob',
   130  		'测试mediumblob',
   131  		'测试longblob',
   132  		'2020-02-20',
   133  		'2020-02-20 02:20:20',
   134  		'2020-02-20 10:20:20',
   135  	    '2024-03-11 08:51:01.461270',
   136  		'02:20:20',
   137  		2020,
   138  		'a',
   139  		'b',
   140  		65,
   141  		'{"key1": "value1"}')`
   142  	insert := helper.DML2Event(sql, "test", "t")
   143  
   144  	update := *insert
   145  	update.PreColumns = update.Columns
   146  
   147  	deleteE := *insert
   148  	deleteE.PreColumns = deleteE.Columns
   149  	deleteE.Columns = nil
   150  
   151  	return ddlEvent, insert, &update, &deleteE
   152  }
   153  
   154  // LargeColumnKeyValues returns the key values of large columns
   155  func LargeColumnKeyValues() ([]string, []driver.Value) {
   156  	names := make([]string, 0, len(LargeTableColumns))
   157  	values := make([]driver.Value, 0, len(LargeTableColumns))
   158  	for key, value := range LargeTableColumns {
   159  		names = append(names, key)
   160  		values = append(values, driver.Value(value))
   161  	}
   162  	return names, values
   163  }
   164  
   165  // LargeTableColumns is the columns of large table
   166  var LargeTableColumns = map[string]interface{}{
   167  	"t":           []uint8("127"),
   168  	"tu1":         []uint8("127"),
   169  	"tu2":         []uint8("128"),
   170  	"tu3":         []uint8("0"),
   171  	"tu4":         nil,
   172  	"s":           []uint8("32767"),
   173  	"su1":         []uint8("32767"),
   174  	"su2":         []uint8("32768"),
   175  	"su3":         []uint8("0"),
   176  	"su4":         nil,
   177  	"m":           []uint8("8388607"),
   178  	"mu1":         []uint8("8388607"),
   179  	"mu2":         []uint8("8388608"),
   180  	"mu3":         []uint8("0"),
   181  	"mu4":         nil,
   182  	"i":           []uint8("2147483647"),
   183  	"iu1":         []uint8("2147483647"),
   184  	"iu2":         []uint8("2147483648"),
   185  	"iu3":         []uint8("0"),
   186  	"iu4":         nil,
   187  	"bi":          []uint8("9223372036854775807"),
   188  	"biu1":        []uint8("9223372036854775807"),
   189  	"biu2":        []uint8("9223372036854775808"),
   190  	"biu3":        []uint8("0"),
   191  	"biu4":        nil,
   192  	"floatT":      []uint8("3.14"),
   193  	"doubleT":     []uint8("2.71"),
   194  	"decimalT":    []uint8("2333.654321"),
   195  	"floatTu":     []uint8("3.14"),
   196  	"doubleTu":    []uint8("2.71"),
   197  	"decimalTu":   []uint8("2333.123456"),
   198  	"decimalTu2":  []uint8("1.7371"),
   199  	"varcharT":    []uint8("测试Varchar"),
   200  	"charT":       []uint8("测试String"),
   201  	"binaryT":     []uint8("测试Binary"),
   202  	"varbinaryT":  []uint8("测试varbinary"),
   203  	"tinytextT":   []uint8("测试Tinytext"),
   204  	"textT":       []uint8("测试text"),
   205  	"mediumtextT": []uint8("测试mediumtext"),
   206  	"longtextT":   []uint8("测试longtext"),
   207  	"tinyblobT":   []uint8("测试tinyblob"),
   208  	"blobT":       []uint8("测试blob"),
   209  	"mediumblobT": []uint8("测试mediumblob"),
   210  	"longblobT":   []uint8("测试longblob"),
   211  	"dateT":       []uint8("2020-02-20"),
   212  	"datetimeT":   []uint8("2020-02-20 02:20:20"),
   213  	"timestampT":  []uint8("2020-02-20 10:20:20"),
   214  	"timestampT2": []uint8("2024-03-11 08:51:01.461270"),
   215  	"timeT":       []uint8("02:20:20"),
   216  	"yearT":       []uint8("2020"),
   217  	"enumT":       []uint8("a"),
   218  	"setT":        []uint8("b"),
   219  	"bitT":        []uint8{65},
   220  	"jsonT":       []uint8("{\"key1\": \"value1\"}"),
   221  }