github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/model/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 model_test
    15  
    16  import (
    17  	"testing"
    18  	"unsafe"
    19  
    20  	"github.com/pingcap/tidb/parser/model"
    21  	"github.com/pingcap/tidb/parser/terror"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestJobClone(t *testing.T) {
    26  	job := &model.Job{
    27  		ID:              100,
    28  		Type:            model.ActionCreateTable,
    29  		SchemaID:        101,
    30  		TableID:         102,
    31  		SchemaName:      "test",
    32  		TableName:       "t",
    33  		State:           model.JobStateDone,
    34  		MultiSchemaInfo: nil,
    35  	}
    36  	clone := job.Clone()
    37  	require.Equal(t, job.ID, clone.ID)
    38  	require.Equal(t, job.Type, clone.Type)
    39  	require.Equal(t, job.SchemaID, clone.SchemaID)
    40  	require.Equal(t, job.TableID, clone.TableID)
    41  	require.Equal(t, job.SchemaName, clone.SchemaName)
    42  	require.Equal(t, job.TableName, clone.TableName)
    43  	require.Equal(t, job.State, clone.State)
    44  	require.Equal(t, job.MultiSchemaInfo, clone.MultiSchemaInfo)
    45  }
    46  
    47  func TestJobSize(t *testing.T) {
    48  	msg := `Please make sure that the following methods work as expected:
    49  - SubJob.FromProxyJob()
    50  - SubJob.ToProxyJob()
    51  `
    52  	job := model.Job{}
    53  	require.Equal(t, 336, int(unsafe.Sizeof(job)), msg)
    54  }
    55  
    56  func TestBackfillMetaCodec(t *testing.T) {
    57  	jm := &model.JobMeta{
    58  		SchemaID: 1,
    59  		TableID:  2,
    60  		Query:    "alter table t add index idx(a)",
    61  		Priority: 1,
    62  	}
    63  	bm := &model.BackfillMeta{
    64  		EndInclude: true,
    65  		Error:      terror.ErrResultUndetermined,
    66  		JobMeta:    jm,
    67  	}
    68  	bmBytes, err := bm.Encode()
    69  	require.NoError(t, err)
    70  	bmRet := &model.BackfillMeta{}
    71  	bmRet.Decode(bmBytes)
    72  	require.Equal(t, bm, bmRet)
    73  }
    74  
    75  func TestMayNeedReorg(t *testing.T) {
    76  	//TODO(bb7133): add more test cases for different ActionType.
    77  	reorgJobTypes := []model.ActionType{
    78  		model.ActionReorganizePartition,
    79  		model.ActionRemovePartitioning,
    80  		model.ActionAlterTablePartitioning,
    81  		model.ActionAddIndex,
    82  		model.ActionAddPrimaryKey,
    83  	}
    84  	generalJobTypes := []model.ActionType{
    85  		model.ActionCreateTable,
    86  		model.ActionDropTable,
    87  	}
    88  	job := &model.Job{
    89  		ID:              100,
    90  		Type:            model.ActionCreateTable,
    91  		SchemaID:        101,
    92  		TableID:         102,
    93  		SchemaName:      "test",
    94  		TableName:       "t",
    95  		State:           model.JobStateDone,
    96  		MultiSchemaInfo: nil,
    97  	}
    98  	for _, jobType := range reorgJobTypes {
    99  		job.Type = jobType
   100  		require.True(t, job.MayNeedReorg())
   101  	}
   102  	for _, jobType := range generalJobTypes {
   103  		job.Type = jobType
   104  		require.False(t, job.MayNeedReorg())
   105  	}
   106  }