github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/csv/csv_encoder_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 csv
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/tidb/pkg/parser/mysql"
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestCSVBatchCodec(t *testing.T) {
    26  	tableInfo := model.BuildTableInfo("test", "table1", []*model.Column{{
    27  		Name: "tiny",
    28  		Type: mysql.TypeTiny,
    29  	}}, nil)
    30  	testCases := []*model.SingleTableTxn{
    31  		{
    32  			Rows: []*model.RowChangedEvent{
    33  				{
    34  					CommitTs:  1,
    35  					TableInfo: tableInfo,
    36  					Columns: model.Columns2ColumnDatas([]*model.Column{{
    37  						Name:  "tiny",
    38  						Value: int64(1),
    39  					}}, tableInfo),
    40  				},
    41  				{
    42  					CommitTs:  2,
    43  					TableInfo: tableInfo,
    44  					Columns: model.Columns2ColumnDatas([]*model.Column{{
    45  						Name:  "tiny",
    46  						Value: int64(2),
    47  					}}, tableInfo),
    48  				},
    49  			},
    50  		},
    51  		{
    52  			TableInfo: tableInfo,
    53  			Rows:      nil,
    54  		},
    55  	}
    56  
    57  	for _, cs := range testCases {
    58  		encoder := newBatchEncoder(&common.Config{
    59  			Delimiter:       ",",
    60  			Quote:           "\"",
    61  			Terminator:      "\n",
    62  			NullString:      "\\N",
    63  			IncludeCommitTs: true,
    64  		})
    65  		err := encoder.AppendTxnEvent(cs, nil)
    66  		require.Nil(t, err)
    67  		messages := encoder.Build()
    68  		if len(cs.Rows) == 0 {
    69  			require.Nil(t, messages)
    70  			continue
    71  		}
    72  		require.Len(t, messages, 1)
    73  		require.Equal(t, len(cs.Rows), messages[0].GetRowsCount())
    74  	}
    75  }
    76  
    77  func TestCSVAppendRowChangedEventWithCallback(t *testing.T) {
    78  	encoder := newBatchEncoder(&common.Config{
    79  		Delimiter:       ",",
    80  		Quote:           "\"",
    81  		Terminator:      "\n",
    82  		NullString:      "\\N",
    83  		IncludeCommitTs: true,
    84  	})
    85  	require.NotNil(t, encoder)
    86  
    87  	count := 0
    88  	tableInfo := model.BuildTableInfo("test", "table1", []*model.Column{{
    89  		Name:  "tiny",
    90  		Value: int64(1), Type: mysql.TypeTiny,
    91  	}}, nil)
    92  	row := &model.RowChangedEvent{
    93  		CommitTs:  1,
    94  		TableInfo: tableInfo,
    95  
    96  		Columns: model.Columns2ColumnDatas([]*model.Column{{Name: "tiny", Value: int64(1)}}, tableInfo),
    97  	}
    98  
    99  	txn := &model.SingleTableTxn{
   100  		TableInfo: tableInfo,
   101  		Rows:      []*model.RowChangedEvent{row},
   102  	}
   103  	callback := func() {
   104  		count += 1
   105  	}
   106  
   107  	// Empty build makes sure that the callback build logic not broken.
   108  	msgs := encoder.Build()
   109  	require.Len(t, msgs, 0, "no message should be built and no panic")
   110  
   111  	// Append the event.
   112  	err := encoder.AppendTxnEvent(txn, callback)
   113  	require.Nil(t, err)
   114  	require.Equal(t, 0, count, "nothing should be called")
   115  
   116  	msgs = encoder.Build()
   117  	require.Len(t, msgs, 1, "expected one message")
   118  	msgs[0].Callback()
   119  	require.Equal(t, 1, count, "expected all callbacks to be called")
   120  }