github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/options_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  	"errors"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    25  )
    26  
    27  func TestOptionsCodec_Encode(t *testing.T) {
    28  	codec := &optionsCodec{}
    29  	for _, version := range primitive.SupportedProtocolVersions() {
    30  		t.Run(version.String(), func(t *testing.T) {
    31  			tests := []encodeTestCase{
    32  				{
    33  					"options simple",
    34  					&Options{},
    35  					nil,
    36  					nil,
    37  				},
    38  				{
    39  					"not an options",
    40  					&Ready{},
    41  					nil,
    42  					errors.New("expected *message.Options, got *message.Ready"),
    43  				},
    44  			}
    45  			for _, tt := range tests {
    46  				t.Run(tt.name, func(t *testing.T) {
    47  					dest := &bytes.Buffer{}
    48  					err := codec.Encode(tt.input, dest, version)
    49  					assert.Equal(t, tt.expected, dest.Bytes())
    50  					assert.Equal(t, tt.err, err)
    51  				})
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestOptionsCodec_EncodedLength(t *testing.T) {
    58  	codec := &optionsCodec{}
    59  	for _, version := range primitive.SupportedProtocolVersions() {
    60  		t.Run(version.String(), func(t *testing.T) {
    61  			tests := []encodedLengthTestCase{
    62  				{
    63  					"options simple",
    64  					&Options{},
    65  					0,
    66  					nil,
    67  				},
    68  				{
    69  					"not an options",
    70  					&Ready{},
    71  					-1,
    72  					errors.New("expected *message.Options, got *message.Ready"),
    73  				},
    74  			}
    75  			for _, tt := range tests {
    76  				t.Run(tt.name, func(t *testing.T) {
    77  					actual, err := codec.EncodedLength(tt.input, version)
    78  					assert.Equal(t, tt.expected, actual)
    79  					assert.Equal(t, tt.err, err)
    80  				})
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func TestOptionsCodec_Decode(t *testing.T) {
    87  	codec := &optionsCodec{}
    88  	for _, version := range primitive.SupportedProtocolVersions() {
    89  		t.Run(version.String(), func(t *testing.T) {
    90  			tests := []decodeTestCase{
    91  				{
    92  					"options simple",
    93  					[]byte{},
    94  					&Options{},
    95  					nil,
    96  				},
    97  			}
    98  			for _, tt := range tests {
    99  				t.Run(tt.name, func(t *testing.T) {
   100  					source := bytes.NewBuffer(tt.input)
   101  					actual, err := codec.Decode(source, version)
   102  					assert.Equal(t, tt.expected, actual)
   103  					assert.Equal(t, tt.err, err)
   104  				})
   105  			}
   106  		})
   107  	}
   108  }