github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/codec/interface_test.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 codec
    15  
    16  import (
    17  	"github.com/pingcap/check"
    18  	timodel "github.com/pingcap/parser/model"
    19  	"github.com/pingcap/parser/mysql"
    20  	"github.com/pingcap/parser/types"
    21  	"github.com/pingcap/ticdc/cdc/model"
    22  	"github.com/pingcap/ticdc/pkg/util/testleak"
    23  )
    24  
    25  type codecInterfaceSuite struct {
    26  }
    27  
    28  var _ = check.Suite(&codecInterfaceSuite{})
    29  
    30  func (s *codecInterfaceSuite) SetUpSuite(c *check.C) {
    31  }
    32  
    33  func (s *codecInterfaceSuite) TearDownSuite(c *check.C) {
    34  }
    35  
    36  func (s *codecInterfaceSuite) TestCreate(c *check.C) {
    37  	defer testleak.AfterTest(c)()
    38  	rowEvent := &model.RowChangedEvent{
    39  		Table: &model.TableName{
    40  			Schema: "test",
    41  			Table:  "t1",
    42  		},
    43  		PreColumns: []*model.Column{
    44  			{
    45  				Name:  "a",
    46  				Value: 1,
    47  				Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
    48  			}, {
    49  				Name:  "b",
    50  				Value: 2,
    51  				Flag:  0,
    52  			},
    53  		},
    54  		StartTs:  1234,
    55  		CommitTs: 5678,
    56  	}
    57  
    58  	msg := NewMQMessage(ProtocolDefault, []byte("key1"), []byte("value1"), rowEvent.CommitTs, model.MqMessageTypeRow, &rowEvent.Table.Schema, &rowEvent.Table.Table)
    59  
    60  	c.Assert(msg.Key, check.BytesEquals, []byte("key1"))
    61  	c.Assert(msg.Value, check.BytesEquals, []byte("value1"))
    62  	c.Assert(msg.Ts, check.Equals, rowEvent.CommitTs)
    63  	c.Assert(msg.Type, check.Equals, model.MqMessageTypeRow)
    64  	c.Assert(*msg.Schema, check.Equals, rowEvent.Table.Schema)
    65  	c.Assert(*msg.Table, check.Equals, rowEvent.Table.Table)
    66  	c.Assert(msg.Protocol, check.Equals, ProtocolDefault)
    67  
    68  	job := &timodel.Job{
    69  		ID:         1071,
    70  		TableID:    49,
    71  		SchemaName: "test",
    72  		Type:       timodel.ActionAddColumn,
    73  		StartTS:    420536581131337731,
    74  		Query:      "alter table t1 add column a int",
    75  		BinlogInfo: &timodel.HistoryInfo{
    76  			TableInfo: &timodel.TableInfo{
    77  				ID:   49,
    78  				Name: timodel.CIStr{O: "t1"},
    79  				Columns: []*timodel.ColumnInfo{
    80  					{ID: 1, Name: timodel.CIStr{O: "id"}, FieldType: types.FieldType{Flag: mysql.PriKeyFlag}, State: timodel.StatePublic},
    81  					{ID: 2, Name: timodel.CIStr{O: "a"}, FieldType: types.FieldType{}, State: timodel.StatePublic},
    82  				},
    83  			},
    84  			FinishedTS: 420536581196873729,
    85  		},
    86  	}
    87  	preTableInfo := &model.TableInfo{
    88  		TableName: model.TableName{
    89  			Schema:  "test",
    90  			Table:   "t1",
    91  			TableID: 49,
    92  		},
    93  		TableInfo: &timodel.TableInfo{
    94  			ID:   49,
    95  			Name: timodel.CIStr{O: "t1"},
    96  			Columns: []*timodel.ColumnInfo{
    97  				{ID: 1, Name: timodel.CIStr{O: "id"}, FieldType: types.FieldType{Flag: mysql.PriKeyFlag}, State: timodel.StatePublic},
    98  			},
    99  		},
   100  	}
   101  	ddlEvent := &model.DDLEvent{}
   102  	ddlEvent.FromJob(job, preTableInfo)
   103  
   104  	msg = newDDLMQMessage(ProtocolMaxwell, nil, []byte("value1"), ddlEvent)
   105  	c.Assert(msg.Key, check.IsNil)
   106  	c.Assert(msg.Value, check.BytesEquals, []byte("value1"))
   107  	c.Assert(msg.Ts, check.Equals, ddlEvent.CommitTs)
   108  	c.Assert(msg.Type, check.Equals, model.MqMessageTypeDDL)
   109  	c.Assert(*msg.Schema, check.Equals, ddlEvent.TableInfo.Schema)
   110  	c.Assert(*msg.Table, check.Equals, ddlEvent.TableInfo.Table)
   111  	c.Assert(msg.Protocol, check.Equals, ProtocolMaxwell)
   112  
   113  	msg = newResolvedMQMessage(ProtocolCanal, []byte("key1"), nil, 1234)
   114  	c.Assert(msg.Key, check.BytesEquals, []byte("key1"))
   115  	c.Assert(msg.Value, check.IsNil)
   116  	c.Assert(msg.Ts, check.Equals, uint64(1234))
   117  	c.Assert(msg.Type, check.Equals, model.MqMessageTypeResolved)
   118  	c.Assert(msg.Schema, check.IsNil)
   119  	c.Assert(msg.Table, check.IsNil)
   120  	c.Assert(msg.Protocol, check.Equals, ProtocolCanal)
   121  }