github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/cases/case_handle_key.go (about)

     1  // Copyright 2020 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 cases
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/tiflow/tests/mq_protocol_tests/framework"
    19  )
    20  
    21  // HandleKeyCase is base impl of test case for non primary handle keys
    22  type HandleKeyCase struct {
    23  	framework.Task
    24  }
    25  
    26  // NewHandleKeyCase create a test case which have non primary handle keys
    27  func NewHandleKeyCase(task framework.Task) *HandleKeyCase {
    28  	return &HandleKeyCase{
    29  		Task: task,
    30  	}
    31  }
    32  
    33  // Name impl framework.Task interface
    34  func (s *HandleKeyCase) Name() string {
    35  	return "Handle Key"
    36  }
    37  
    38  // Run impl framework.Task interface
    39  func (s *HandleKeyCase) Run(ctx *framework.TaskContext) error {
    40  	_, err := ctx.Upstream.ExecContext(ctx.Ctx, "create table test (id int not null unique, value int)")
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// Get a handle of an existing table
    46  	table := ctx.SQLHelper().GetTable("test")
    47  	// Create an SQL request, send it to the upstream, wait for completion and check the correctness of replication
    48  	err = table.Insert(map[string]interface{}{
    49  		"id":    0,
    50  		"value": 0,
    51  	}).Send().Wait().Check()
    52  	if err != nil {
    53  		return errors.AddStack(err)
    54  	}
    55  
    56  	err = table.Upsert(map[string]interface{}{
    57  		"id":    0,
    58  		"value": 1,
    59  	}).Send().Wait().Check()
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	err = table.Delete(map[string]interface{}{
    65  		"id": 0,
    66  	}).Send().Wait().Check()
    67  	return err
    68  }