github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/common/message_test.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 common
    15  
    16  import (
    17  	"testing"
    18  
    19  	timodel "github.com/pingcap/tidb/pkg/parser/model"
    20  	"github.com/pingcap/tidb/pkg/parser/mysql"
    21  	"github.com/pingcap/tidb/pkg/parser/types"
    22  	"github.com/pingcap/tiflow/cdc/model"
    23  	"github.com/pingcap/tiflow/pkg/config"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestCreate(t *testing.T) {
    28  	t.Parallel()
    29  	tableInfo := model.BuildTableInfo("test", "t1", []*model.Column{
    30  		{
    31  			Name: "a",
    32  			Flag: model.HandleKeyFlag | model.PrimaryKeyFlag,
    33  		}, {
    34  			Name: "b",
    35  			Flag: 0,
    36  		},
    37  	}, [][]int{{0}})
    38  	rowEvent := &model.RowChangedEvent{
    39  		TableInfo: tableInfo,
    40  		PreColumns: model.Columns2ColumnDatas([]*model.Column{
    41  			{
    42  				Name:  "a",
    43  				Value: 1,
    44  			}, {
    45  				Name:  "b",
    46  				Value: 2,
    47  			},
    48  		}, tableInfo),
    49  		StartTs:  1234,
    50  		CommitTs: 5678,
    51  	}
    52  
    53  	msg := NewMsg(config.ProtocolOpen,
    54  		[]byte("key1"),
    55  		[]byte("value1"),
    56  		rowEvent.CommitTs,
    57  		model.MessageTypeRow,
    58  		&rowEvent.TableInfo.TableName.Schema,
    59  		&rowEvent.TableInfo.TableName.Table)
    60  
    61  	require.Equal(t, []byte("key1"), msg.Key)
    62  	require.Equal(t, []byte("value1"), msg.Value)
    63  	require.Equal(t, rowEvent.CommitTs, msg.Ts)
    64  	require.Equal(t, model.MessageTypeRow, msg.Type)
    65  	require.Equal(t, rowEvent.TableInfo.GetSchemaName(), *msg.Schema)
    66  	require.Equal(t, rowEvent.TableInfo.GetTableName(), *msg.Table)
    67  	require.Equal(t, config.ProtocolOpen, msg.Protocol)
    68  
    69  	ft := types.NewFieldType(0)
    70  	ft.SetFlag(mysql.PriKeyFlag)
    71  	job := &timodel.Job{
    72  		ID:         1071,
    73  		TableID:    49,
    74  		SchemaName: "test",
    75  		Type:       timodel.ActionAddColumn,
    76  		StartTS:    420536581131337731,
    77  		Query:      "alter table t1 add column a int",
    78  		BinlogInfo: &timodel.HistoryInfo{
    79  			TableInfo: &timodel.TableInfo{
    80  				ID:   49,
    81  				Name: timodel.CIStr{O: "t1"},
    82  				Columns: []*timodel.ColumnInfo{
    83  					{ID: 1, Name: timodel.CIStr{O: "id"}, FieldType: *ft, State: timodel.StatePublic},
    84  					{ID: 2, Name: timodel.CIStr{O: "a"}, FieldType: types.FieldType{}, State: timodel.StatePublic},
    85  				},
    86  			},
    87  			FinishedTS: 420536581196873729,
    88  		},
    89  	}
    90  	preTableInfo := &model.TableInfo{
    91  		TableName: model.TableName{
    92  			Schema:  "test",
    93  			Table:   "t1",
    94  			TableID: 49,
    95  		},
    96  		TableInfo: &timodel.TableInfo{
    97  			ID:   49,
    98  			Name: timodel.CIStr{O: "t1"},
    99  			Columns: []*timodel.ColumnInfo{
   100  				{ID: 1, Name: timodel.CIStr{O: "id"}, FieldType: *ft, State: timodel.StatePublic},
   101  			},
   102  		},
   103  	}
   104  	tableInfo2 := model.WrapTableInfo(job.SchemaID, job.SchemaName, job.BinlogInfo.FinishedTS, job.BinlogInfo.TableInfo)
   105  	ddlEvent := &model.DDLEvent{}
   106  	ddlEvent.FromJob(job, preTableInfo, tableInfo2)
   107  
   108  	msg = NewDDLMsg(config.ProtocolMaxwell, nil, []byte("value1"), ddlEvent)
   109  	require.Nil(t, msg.Key)
   110  	require.Equal(t, []byte("value1"), msg.Value)
   111  	require.Equal(t, ddlEvent.CommitTs, msg.Ts)
   112  	require.Equal(t, model.MessageTypeDDL, msg.Type)
   113  	require.Equal(t, ddlEvent.TableInfo.TableName.Schema, *msg.Schema)
   114  	require.Equal(t, ddlEvent.TableInfo.TableName.Table, *msg.Table)
   115  	require.Equal(t, config.ProtocolMaxwell, msg.Protocol)
   116  
   117  	msg = NewResolvedMsg(config.ProtocolCanal, []byte("key1"), nil, 1234)
   118  	require.Equal(t, []byte("key1"), msg.Key)
   119  	require.Nil(t, msg.Value)
   120  	require.Equal(t, uint64(1234), msg.Ts)
   121  	require.Equal(t, model.MessageTypeResolved, msg.Type)
   122  	require.Nil(t, msg.Schema)
   123  	require.Nil(t, msg.Table)
   124  	require.Equal(t, config.ProtocolCanal, msg.Protocol)
   125  }