github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/metadata/ddl_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 metadata
    15  
    16  import (
    17  	"context"
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/pingcap/tiflow/engine/pkg/adapter"
    22  	"github.com/pingcap/tiflow/engine/pkg/meta/mock"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestMarshal(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	st := SourceTable{Source: "source", Schema: "schema", Table: "table"}
    30  	bytes, err := json.Marshal(st)
    31  	require.NoError(t, err)
    32  	var st2 SourceTable
    33  	require.NoError(t, json.Unmarshal(bytes, &st2))
    34  	require.Equal(t, st, st2)
    35  
    36  	tt := TargetTable{Schema: "schema", Table: "table"}
    37  	bytes, err = json.Marshal(tt)
    38  	require.NoError(t, err)
    39  	var tt2 TargetTable
    40  	require.NoError(t, json.Unmarshal(bytes, &tt2))
    41  	require.Equal(t, tt, tt2)
    42  }
    43  
    44  func TestDroppedColumnsStore(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	var (
    48  		targetTable         = TargetTable{Schema: "schema", Table: "table"}
    49  		sourceTable1        = SourceTable{Source: "source", Schema: "schema", Table: "table1"}
    50  		sourceTable2        = SourceTable{Source: "source", Schema: "schema", Table: "table2"}
    51  		droppedColumnsStore = NewDroppedColumnsStore(mock.NewMetaMock(), targetTable)
    52  	)
    53  	key := droppedColumnsStore.key()
    54  	keys, err := adapter.DMDroppedColumnsKeyAdapter.Decode(key)
    55  	require.NoError(t, err)
    56  	require.Len(t, keys, 1)
    57  	var tt TargetTable
    58  	require.NoError(t, json.Unmarshal([]byte(keys[0]), &tt))
    59  	require.Equal(t, tt, targetTable)
    60  
    61  	state := droppedColumnsStore.createState()
    62  	require.IsType(t, &DroppedColumns{}, state)
    63  
    64  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable1))
    65  	require.NoError(t, droppedColumnsStore.DelDroppedColumn(context.Background(), "col"))
    66  	require.NoError(t, droppedColumnsStore.DelDroppedColumnForTable(context.Background(), sourceTable1))
    67  
    68  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1"}, sourceTable1))
    69  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable1))
    70  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col2", sourceTable1))
    71  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    72  
    73  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1", "col2"}, sourceTable2))
    74  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable1))
    75  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col2", sourceTable1))
    76  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    77  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col2", sourceTable2))
    78  
    79  	require.NoError(t, droppedColumnsStore.DelDroppedColumn(context.Background(), "col1"))
    80  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    81  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col2", sourceTable2))
    82  	require.NoError(t, droppedColumnsStore.DelDroppedColumn(context.Background(), "col2"))
    83  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col2", sourceTable2))
    84  
    85  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1", "col2"}, sourceTable1))
    86  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1", "col2"}, sourceTable2))
    87  	require.NoError(t, droppedColumnsStore.DelDroppedColumnForTable(context.Background(), sourceTable1))
    88  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable1))
    89  	require.True(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    90  	require.NoError(t, droppedColumnsStore.DelDroppedColumnForTable(context.Background(), sourceTable2))
    91  	require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    92  
    93  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1"}, sourceTable1))
    94  	require.NoError(t, droppedColumnsStore.AddDroppedColumns(context.Background(), []string{"col1"}, sourceTable2))
    95  	require.NoError(t, DelAllDroppedColumns(context.Background(), droppedColumnsStore.kvClient))
    96  	// mock kv client doesn't support WithPrefix
    97  	// require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable1))
    98  	// require.False(t, droppedColumnsStore.HasDroppedColumn(context.Background(), "col1", sourceTable2))
    99  }