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

     1  // Copyright 2021 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 datacodec
    16  
    17  import (
    18  	"encoding/binary"
    19  	"math/big"
    20  	"net"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"github.com/datastax/go-cassandra-native-protocol/datatype"
    28  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    29  )
    30  
    31  const (
    32  	a = byte('a')
    33  	b = byte('b')
    34  	c = byte('c')
    35  	d = byte('d')
    36  	e = byte('e')
    37  	f = byte('f')
    38  	x = byte('x')
    39  	y = byte('y')
    40  )
    41  
    42  func boolPtr(v bool) *bool                    { return &v }
    43  func intPtr(v int) *int                       { return &v }
    44  func int64Ptr(v int64) *int64                 { return &v }
    45  func int32Ptr(v int32) *int32                 { return &v }
    46  func int16Ptr(v int16) *int16                 { return &v }
    47  func int8Ptr(v int8) *int8                    { return &v }
    48  func uintPtr(v uint) *uint                    { return &v }
    49  func uint64Ptr(v uint64) *uint64              { return &v }
    50  func uint32Ptr(v uint32) *uint32              { return &v }
    51  func uint16Ptr(v uint16) *uint16              { return &v }
    52  func uint8Ptr(v uint8) *uint8                 { return &v }
    53  func stringPtr(v string) *string              { return &v }
    54  func float64Ptr(v float64) *float64           { return &v }
    55  func float32Ptr(v float32) *float32           { return &v }
    56  func byteArrayPtr(v [16]byte) *[16]byte       { return &v }
    57  func interfacePtr(v interface{}) *interface{} { return &v }
    58  func boolNilPtr() *bool                       { return nil }
    59  func intNilPtr() *int                         { return nil }
    60  func int64NilPtr() *int64                     { return nil }
    61  func int32NilPtr() *int32                     { return nil }
    62  func int16NilPtr() *int16                     { return nil }
    63  func int8NilPtr() *int8                       { return nil }
    64  func uintNilPtr() *uint                       { return nil }
    65  func uint64NilPtr() *uint64                   { return nil }
    66  func uint32NilPtr() *uint32                   { return nil }
    67  func uint16NilPtr() *uint16                   { return nil }
    68  func uint8NilPtr() *uint8                     { return nil }
    69  func stringNilPtr() *string                   { return nil }
    70  func bigIntNilPtr() *big.Int                  { return nil }
    71  func bigFloatNilPtr() *big.Float              { return nil }
    72  func float64NilPtr() *float64                 { return nil }
    73  func float32NilPtr() *float32                 { return nil }
    74  func timeNilPtr() *time.Time                  { return nil }
    75  func durationNilPtr() *time.Duration          { return nil }
    76  func cqlDecimalNilPtr() *CqlDecimal           { return nil }
    77  func cqlDurationNilPtr() *CqlDuration         { return nil }
    78  func byteSliceNilPtr() *[]byte                { return nil }
    79  func byteArrayNilPtr() *[16]byte              { return nil }
    80  func runeSliceNilPtr() *[]rune                { return nil }
    81  func netIPNilPtr() *net.IP                    { return nil }
    82  func uuidNilPtr() *primitive.UUID             { return nil }
    83  func interfaceNilPtr() *interface{}           { return nil }
    84  
    85  func encodeUint64(v uint64) []byte {
    86  	bs := make([]byte, 8)
    87  	binary.BigEndian.PutUint64(bs, v)
    88  	return bs
    89  }
    90  
    91  func encodeUint32(v uint32) []byte {
    92  	bs := make([]byte, 4)
    93  	binary.BigEndian.PutUint32(bs, v)
    94  	return bs
    95  }
    96  
    97  func encodeUint16(v uint16) []byte {
    98  	bs := make([]byte, 2)
    99  	binary.BigEndian.PutUint16(bs, v)
   100  	return bs
   101  }
   102  
   103  // Assert that either the error is nil if the expected message is empty, or that the error message contains the expected
   104  // message.
   105  func assertErrorMessage(t *testing.T, expectedMessage string, actual error) {
   106  	if expectedMessage == "" {
   107  		assert.NoError(t, actual)
   108  	} else {
   109  		require.NotNil(t, actual)
   110  		assert.Contains(t, actual.Error(), expectedMessage)
   111  	}
   112  }
   113  
   114  type wrongDataType struct{}
   115  
   116  func (w wrongDataType) String() string                      { return "666" }
   117  func (w wrongDataType) AsCql() string                       { return "666" }
   118  func (w wrongDataType) Code() primitive.DataTypeCode        { return 666 }
   119  func (w wrongDataType) DeepCopyDataType() datatype.DataType { return &wrongDataType{} }