github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/model/common_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
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestResponseOpUnion(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	get := ResponseOp{
    26  		Response: &ResponseOpResponseGet{
    27  			ResponseGet: &GetResponse{
    28  				Header: &ResponseHeader{
    29  					ClusterID: "1111",
    30  				},
    31  			},
    32  		},
    33  	}
    34  	require.IsType(t, &GetResponse{}, get.GetResponseGet())
    35  	require.Nil(t, get.GetResponsePut())
    36  
    37  	put := ResponseOp{
    38  		Response: &ResponseOpResponsePut{
    39  			ResponsePut: &PutResponse{
    40  				Header: &ResponseHeader{
    41  					ClusterID: "1111",
    42  				},
    43  			},
    44  		},
    45  	}
    46  	require.IsType(t, &PutResponse{}, put.GetResponsePut())
    47  	require.Nil(t, put.GetResponseDelete())
    48  
    49  	delet := ResponseOp{
    50  		Response: &ResponseOpResponseDelete{
    51  			ResponseDelete: &DeleteResponse{
    52  				Header: &ResponseHeader{
    53  					ClusterID: "1111",
    54  				},
    55  			},
    56  		},
    57  	}
    58  	require.IsType(t, &DeleteResponse{}, delet.GetResponseDelete())
    59  	require.Nil(t, delet.GetResponseTxn())
    60  
    61  	txn := ResponseOp{
    62  		Response: &ResponseOpResponseTxn{
    63  			ResponseTxn: &TxnResponse{
    64  				Header: &ResponseHeader{
    65  					ClusterID: "1111",
    66  				},
    67  			},
    68  		},
    69  	}
    70  	require.IsType(t, &TxnResponse{}, txn.GetResponseTxn())
    71  	require.Nil(t, txn.GetResponseGet())
    72  }
    73  
    74  func TestNestedTxnResponse(t *testing.T) {
    75  	txn := ResponseOp{
    76  		Response: &ResponseOpResponseTxn{
    77  			ResponseTxn: &TxnResponse{
    78  				Header: &ResponseHeader{
    79  					ClusterID: "1111",
    80  				},
    81  				Responses: []ResponseOp{
    82  					{
    83  						Response: &ResponseOpResponseGet{
    84  							ResponseGet: &GetResponse{
    85  								Header: &ResponseHeader{
    86  									ClusterID: "1111",
    87  								},
    88  							},
    89  						},
    90  					},
    91  					{
    92  						Response: &ResponseOpResponsePut{
    93  							ResponsePut: &PutResponse{
    94  								Header: &ResponseHeader{
    95  									ClusterID: "1111",
    96  								},
    97  							},
    98  						},
    99  					},
   100  					{
   101  						Response: &ResponseOpResponseTxn{
   102  							ResponseTxn: &TxnResponse{
   103  								Header: &ResponseHeader{
   104  									ClusterID: "1111",
   105  								},
   106  							},
   107  						},
   108  					},
   109  				},
   110  			},
   111  		},
   112  	}
   113  	require.IsType(t, &TxnResponse{}, txn.GetResponseTxn())
   114  	require.IsType(t, &GetResponse{}, txn.GetResponseTxn().Responses[0].GetResponseGet())
   115  	require.IsType(t, &PutResponse{}, txn.GetResponseTxn().Responses[1].GetResponsePut())
   116  	require.IsType(t, &TxnResponse{}, txn.GetResponseTxn().Responses[2].GetResponseTxn())
   117  }