github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/sink_protocol_test.go (about)

     1  // Copyright 2021 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 config
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestParseSinkProtocolFromString(t *testing.T) {
    23  	t.Parallel()
    24  	testCases := []struct {
    25  		protocol             string
    26  		expectedProtocolEnum Protocol
    27  		expectedErr          string
    28  	}{
    29  		{
    30  			protocol:    "random",
    31  			expectedErr: ".*unknown 'random' message protocol for sink.*",
    32  		},
    33  		{
    34  			protocol:             "default",
    35  			expectedProtocolEnum: ProtocolOpen,
    36  		},
    37  		{
    38  			protocol:             "canal",
    39  			expectedProtocolEnum: ProtocolCanal,
    40  		},
    41  		{
    42  			protocol:             "canal-json",
    43  			expectedProtocolEnum: ProtocolCanalJSON,
    44  		},
    45  		{
    46  			protocol:             "maxwell",
    47  			expectedProtocolEnum: ProtocolMaxwell,
    48  		},
    49  		{
    50  			protocol:             "avro",
    51  			expectedProtocolEnum: ProtocolAvro,
    52  		},
    53  		{
    54  			protocol:             "flat-avro",
    55  			expectedProtocolEnum: ProtocolAvro,
    56  		},
    57  		{
    58  			protocol:             "craft",
    59  			expectedProtocolEnum: ProtocolCraft,
    60  		},
    61  		{
    62  			protocol:             "open-protocol",
    63  			expectedProtocolEnum: ProtocolOpen,
    64  		},
    65  	}
    66  
    67  	for _, tc := range testCases {
    68  		protocol, err := ParseSinkProtocolFromString(tc.protocol)
    69  		if tc.expectedErr != "" {
    70  			require.Regexp(t, tc.expectedErr, err)
    71  		} else {
    72  			require.Equal(t, tc.expectedProtocolEnum, protocol)
    73  		}
    74  	}
    75  }
    76  
    77  func TestString(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	testCases := []struct {
    81  		protocolEnum     Protocol
    82  		expectedProtocol string
    83  	}{
    84  		{
    85  			protocolEnum:     ProtocolDefault,
    86  			expectedProtocol: "default",
    87  		},
    88  		{
    89  			protocolEnum:     ProtocolCanal,
    90  			expectedProtocol: "canal",
    91  		},
    92  		{
    93  			protocolEnum:     ProtocolCanalJSON,
    94  			expectedProtocol: "canal-json",
    95  		},
    96  		{
    97  			protocolEnum:     ProtocolMaxwell,
    98  			expectedProtocol: "maxwell",
    99  		},
   100  		{
   101  			protocolEnum:     ProtocolAvro,
   102  			expectedProtocol: "avro",
   103  		},
   104  		{
   105  			protocolEnum:     ProtocolCraft,
   106  			expectedProtocol: "craft",
   107  		},
   108  		{
   109  			protocolEnum:     ProtocolOpen,
   110  			expectedProtocol: "open-protocol",
   111  		},
   112  	}
   113  
   114  	for _, tc := range testCases {
   115  		require.Equal(t, tc.expectedProtocol, tc.protocolEnum.String())
   116  	}
   117  }
   118  
   119  func TestIsBatchEncoder(t *testing.T) {
   120  	t.Parallel()
   121  
   122  	testCases := []struct {
   123  		protocolEnum Protocol
   124  		expect       bool
   125  	}{
   126  		{
   127  			protocolEnum: ProtocolDefault,
   128  			expect:       false,
   129  		},
   130  		{
   131  			protocolEnum: ProtocolCanal,
   132  			expect:       true,
   133  		},
   134  		{
   135  			protocolEnum: ProtocolCanalJSON,
   136  			expect:       false,
   137  		},
   138  		{
   139  			protocolEnum: ProtocolMaxwell,
   140  			expect:       true,
   141  		},
   142  		{
   143  			protocolEnum: ProtocolAvro,
   144  			expect:       false,
   145  		},
   146  		{
   147  			protocolEnum: ProtocolCraft,
   148  			expect:       true,
   149  		},
   150  		{
   151  			protocolEnum: ProtocolOpen,
   152  			expect:       true,
   153  		},
   154  	}
   155  
   156  	for _, tc := range testCases {
   157  		require.Equal(t, tc.expect, tc.protocolEnum.IsBatchEncode())
   158  	}
   159  }