github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sqlmodel/reduce_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 sqlmodel
    15  
    16  import (
    17  	"testing"
    18  
    19  	cdcmodel "github.com/pingcap/tiflow/cdc/model"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestIdentity(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
    27  	sourceTI1 := mockTableInfo(t, "CREATE TABLE tb1 (c INT PRIMARY KEY, c2 INT)")
    28  
    29  	change := NewRowChange(source, nil, []interface{}{1, 2}, nil, sourceTI1, nil, nil)
    30  	pre, post := change.IdentityValues()
    31  	require.Equal(t, []interface{}{1}, pre)
    32  	require.Len(t, post, 0)
    33  
    34  	change = NewRowChange(source, nil, []interface{}{1, 2}, []interface{}{1, 4}, sourceTI1, nil, nil)
    35  	pre, post = change.IdentityValues()
    36  	require.Equal(t, []interface{}{1}, pre)
    37  	require.Equal(t, []interface{}{1}, post)
    38  	require.False(t, change.IsIdentityUpdated())
    39  
    40  	sourceTI2 := mockTableInfo(t, "CREATE TABLE tb1 (c INT, c2 INT)")
    41  	change = NewRowChange(source, nil, nil, []interface{}{5, 6}, sourceTI2, nil, nil)
    42  	pre, post = change.IdentityValues()
    43  	require.Len(t, pre, 0)
    44  	require.Equal(t, []interface{}{5, 6}, post)
    45  }
    46  
    47  func TestSplit(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
    51  	sourceTI1 := mockTableInfo(t, "CREATE TABLE tb1 (c INT PRIMARY KEY, c2 INT)")
    52  
    53  	change := NewRowChange(source, nil, []interface{}{1, 2}, []interface{}{3, 4}, sourceTI1, nil, nil)
    54  	require.True(t, change.IsIdentityUpdated())
    55  	del, ins := change.SplitUpdate()
    56  	delIDKey := del.IdentityKey()
    57  	require.NotZero(t, delIDKey)
    58  	insIDKey := ins.IdentityKey()
    59  	require.NotZero(t, insIDKey)
    60  	require.NotEqual(t, delIDKey, insIDKey)
    61  }
    62  
    63  func (s *dpanicSuite) TestReduce() {
    64  	source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
    65  	sourceTI := mockTableInfo(s.T(), "CREATE TABLE tb1 (c INT PRIMARY KEY, c2 INT)")
    66  
    67  	cases := []struct {
    68  		pre1      []interface{}
    69  		post1     []interface{}
    70  		pre2      []interface{}
    71  		post2     []interface{}
    72  		preAfter  []interface{}
    73  		postAfter []interface{}
    74  	}{
    75  		// INSERT + UPDATE
    76  		{
    77  			nil,
    78  			[]interface{}{1, 2},
    79  			[]interface{}{1, 2},
    80  			[]interface{}{3, 4},
    81  			nil,
    82  			[]interface{}{3, 4},
    83  		},
    84  		// INSERT + DELETE
    85  		{
    86  			nil,
    87  			[]interface{}{1, 2},
    88  			[]interface{}{1, 2},
    89  			nil,
    90  			[]interface{}{1, 2},
    91  			nil,
    92  		},
    93  		// UPDATE + UPDATE
    94  		{
    95  			[]interface{}{1, 2},
    96  			[]interface{}{1, 3},
    97  			[]interface{}{1, 3},
    98  			[]interface{}{1, 4},
    99  			[]interface{}{1, 2},
   100  			[]interface{}{1, 4},
   101  		},
   102  		// UPDATE + DELETE
   103  		{
   104  			[]interface{}{1, 2},
   105  			[]interface{}{1, 3},
   106  			[]interface{}{1, 3},
   107  			nil,
   108  			[]interface{}{1, 2},
   109  			nil,
   110  		},
   111  	}
   112  
   113  	for _, c := range cases {
   114  		change1 := NewRowChange(source, nil, c.pre1, c.post1, sourceTI, nil, nil)
   115  		change2 := NewRowChange(source, nil, c.pre2, c.post2, sourceTI, nil, nil)
   116  		changeAfter := NewRowChange(source, nil, c.preAfter, c.postAfter, sourceTI, nil, nil)
   117  		changeAfter.lazyInitWhereHandle()
   118  
   119  		change2.Reduce(change1)
   120  		s.Equal(changeAfter, change2)
   121  	}
   122  
   123  	// test reduce on IdentityUpdated will DPanic
   124  	change1 := NewRowChange(source, nil, []interface{}{1, 2}, []interface{}{3, 4}, sourceTI, nil, nil)
   125  	change2 := NewRowChange(source, nil, []interface{}{3, 4}, []interface{}{5, 6}, sourceTI, nil, nil)
   126  	s.Panics(func() {
   127  		change2.Reduce(change1)
   128  	})
   129  }