github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/mq/dispatcher/partition/columns_test.go (about)

     1  // Copyright 2023 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 partition
    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/types"
    22  	"github.com/pingcap/tiflow/cdc/model"
    23  	"github.com/pingcap/tiflow/pkg/errors"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestColumnsDispatcher(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	tidbTableInfo := &timodel.TableInfo{
    31  		ID:   100,
    32  		Name: timodel.NewCIStr("t1"),
    33  		Columns: []*timodel.ColumnInfo{
    34  			{ID: 1, Name: timodel.NewCIStr("col2"), Offset: 1, FieldType: *types.NewFieldType(mysql.TypeLong)},
    35  			{ID: 2, Name: timodel.NewCIStr("col1"), Offset: 0, FieldType: *types.NewFieldType(mysql.TypeLong)},
    36  			{ID: 3, Name: timodel.NewCIStr("col3"), Offset: 2, FieldType: *types.NewFieldType(mysql.TypeLong)},
    37  		},
    38  	}
    39  	tableInfo := model.WrapTableInfo(100, "test", 33, tidbTableInfo)
    40  	event := &model.RowChangedEvent{
    41  		TableInfo: tableInfo,
    42  		Columns: []*model.ColumnData{
    43  			{ColumnID: 1, Value: 11},
    44  			{ColumnID: 2, Value: 22},
    45  			{ColumnID: 3, Value: 33},
    46  		},
    47  	}
    48  
    49  	p := NewColumnsDispatcher([]string{"col-2", "col-not-found"})
    50  	_, _, err := p.DispatchRowChangedEvent(event, 16)
    51  	require.ErrorIs(t, err, errors.ErrDispatcherFailed)
    52  
    53  	p = NewColumnsDispatcher([]string{"col2", "col1"})
    54  	index, _, err := p.DispatchRowChangedEvent(event, 16)
    55  	require.NoError(t, err)
    56  	require.Equal(t, int32(15), index)
    57  }