github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/result_other_test.go (about)

     1  // Copyright 2020 DataStax
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package message
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    24  )
    25  
    26  func TestSetKeyspaceResult_DeepCopy(t *testing.T) {
    27  	msg := &SetKeyspaceResult{
    28  		Keyspace: "ks1",
    29  	}
    30  
    31  	cloned := msg.DeepCopy()
    32  	assert.Equal(t, msg, cloned)
    33  
    34  	cloned.Keyspace = "ks2"
    35  
    36  	assert.NotEqual(t, msg, cloned)
    37  
    38  	assert.Equal(t, "ks1", msg.Keyspace)
    39  	assert.Equal(t, "ks2", cloned.Keyspace)
    40  }
    41  
    42  func TestResultCodec_Encode_Other(test *testing.T) {
    43  	codec := &resultCodec{}
    44  	for _, version := range primitive.SupportedProtocolVersions() {
    45  		test.Run(version.String(), func(test *testing.T) {
    46  			tests := []encodeTestCase{
    47  				{
    48  					"void result",
    49  					&VoidResult{},
    50  					[]byte{
    51  						0, 0, 0, 1, // result type
    52  					},
    53  					nil,
    54  				},
    55  				{
    56  					"set keyspace result",
    57  					&SetKeyspaceResult{Keyspace: "ks1"},
    58  					[]byte{
    59  						0, 0, 0, 3, // result type
    60  						0, 3, k, s, _1,
    61  					},
    62  					nil,
    63  				},
    64  			}
    65  			for _, tt := range tests {
    66  				test.Run(tt.name, func(t *testing.T) {
    67  					dest := &bytes.Buffer{}
    68  					err := codec.Encode(tt.input, dest, version)
    69  					assert.Equal(t, tt.expected, dest.Bytes())
    70  					assert.Equal(t, tt.err, err)
    71  				})
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestResultCodec_EncodedLength_Other(test *testing.T) {
    78  	codec := &resultCodec{}
    79  	for _, version := range primitive.SupportedProtocolVersions() {
    80  		test.Run(version.String(), func(test *testing.T) {
    81  			tests := []encodedLengthTestCase{
    82  				{
    83  					"void result",
    84  					&VoidResult{},
    85  					primitive.LengthOfInt,
    86  					nil,
    87  				},
    88  				{
    89  					"set keyspace result",
    90  					&SetKeyspaceResult{Keyspace: "ks1"},
    91  					primitive.LengthOfInt + primitive.LengthOfString("ks1"),
    92  					nil,
    93  				},
    94  			}
    95  			for _, tt := range tests {
    96  				test.Run(tt.name, func(t *testing.T) {
    97  					actual, err := codec.EncodedLength(tt.input, version)
    98  					assert.Equal(t, tt.expected, actual)
    99  					assert.Equal(t, tt.err, err)
   100  				})
   101  			}
   102  		})
   103  	}
   104  }
   105  
   106  func TestResultCodec_Decode_Other(test *testing.T) {
   107  	codec := &resultCodec{}
   108  	for _, version := range primitive.SupportedProtocolVersions() {
   109  		test.Run(version.String(), func(test *testing.T) {
   110  			tests := []decodeTestCase{
   111  				{
   112  					"void result",
   113  					[]byte{
   114  						0, 0, 0, 1, // result type
   115  					},
   116  					&VoidResult{},
   117  					nil,
   118  				},
   119  				{
   120  					"set keyspace result",
   121  					[]byte{
   122  						0, 0, 0, 3, // result type
   123  						0, 3, k, s, _1,
   124  					},
   125  					&SetKeyspaceResult{Keyspace: "ks1"},
   126  					nil,
   127  				},
   128  			}
   129  			for _, tt := range tests {
   130  				test.Run(tt.name, func(t *testing.T) {
   131  					source := bytes.NewBuffer(tt.input)
   132  					actual, err := codec.Decode(source, version)
   133  					assert.Equal(t, tt.expected, actual)
   134  					assert.Equal(t, tt.err, err)
   135  				})
   136  			}
   137  		})
   138  	}
   139  }