github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datacodec/codec_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  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/datastax/go-cassandra-native-protocol/datatype"
    24  )
    25  
    26  func TestNewCodec(t *testing.T) {
    27  	customType := datatype.NewCustom("com.example.Type")
    28  	listType := datatype.NewList(datatype.Int)
    29  	listCodec, _ := NewList(listType)
    30  	setType := datatype.NewSet(datatype.Int)
    31  	setCodec, _ := NewSet(setType)
    32  	mapType := datatype.NewMap(datatype.Int, datatype.Varchar)
    33  	mapCodec, _ := NewMap(mapType)
    34  	tupleType := datatype.NewTuple(datatype.Int)
    35  	tupleCodec, _ := NewTuple(tupleType)
    36  	userDefinedType, _ := datatype.NewUserDefined("ks1", "table1", []string{"f1"}, []datatype.DataType{datatype.Int})
    37  	userDefinedCodec, _ := NewUserDefined(userDefinedType)
    38  	tests := []struct {
    39  		name      string
    40  		dt        datatype.DataType
    41  		wantCodec Codec
    42  		wantErr   string
    43  	}{
    44  		{"Ascii", datatype.Ascii, Ascii, ""},
    45  		{"Bigint", datatype.Bigint, Bigint, ""},
    46  		{"Blob", datatype.Blob, Blob, ""},
    47  		{"Boolean", datatype.Boolean, Boolean, ""},
    48  		{"Counter", datatype.Counter, Counter, ""},
    49  		{"Custom", customType, NewCustom(customType), ""},
    50  		{"Date", datatype.Date, Date, ""},
    51  		{"Decimal", datatype.Decimal, Decimal, ""},
    52  		{"Double", datatype.Double, Double, ""},
    53  		{"Duration", datatype.Duration, Duration, ""},
    54  		{"Float", datatype.Float, Float, ""},
    55  		{"Inet", datatype.Inet, Inet, ""},
    56  		{"Int", datatype.Int, Int, ""},
    57  		{"Smallint", datatype.Smallint, Smallint, ""},
    58  		{"Time", datatype.Time, Time, ""},
    59  		{"Timestamp", datatype.Timestamp, Timestamp, ""},
    60  		{"Timeuuid", datatype.Timeuuid, Timeuuid, ""},
    61  		{"Tinyint", datatype.Tinyint, Tinyint, ""},
    62  		{"Uuid", datatype.Uuid, Uuid, ""},
    63  		{"Varchar", datatype.Varchar, Varchar, ""},
    64  		{"Varint", datatype.Varint, Varint, ""},
    65  		{"List", listType, listCodec, ""},
    66  		{"Set", setType, setCodec, ""},
    67  		{"Map", mapType, mapCodec, ""},
    68  		{"Tuple", tupleType, tupleCodec, ""},
    69  		{"UserDefined", userDefinedType, userDefinedCodec, ""},
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			gotCodec, gotErr := NewCodec(tt.dt)
    74  			assert.Equal(t, tt.wantCodec, gotCodec)
    75  			assertErrorMessage(t, tt.wantErr, gotErr)
    76  		})
    77  	}
    78  }
    79  
    80  func TestPreferredGoType(t *testing.T) {
    81  	customType := datatype.NewCustom("com.example.Type")
    82  	listType := datatype.NewList(datatype.Int)
    83  	setType := datatype.NewSet(datatype.Int)
    84  	mapType := datatype.NewMap(datatype.Int, datatype.Varchar)
    85  	tupleType := datatype.NewTuple(datatype.Int)
    86  	userDefinedType, _ := datatype.NewUserDefined("ks1", "table1", []string{"f1"}, []datatype.DataType{datatype.Int})
    87  	tests := []struct {
    88  		name     string
    89  		dt       datatype.DataType
    90  		wantType reflect.Type
    91  		wantErr  string
    92  	}{
    93  		{"Ascii", datatype.Ascii, typeOfString, ""},
    94  		{"Bigint", datatype.Bigint, typeOfInt64, ""},
    95  		{"Blob", datatype.Blob, typeOfByteSlice, ""},
    96  		{"Boolean", datatype.Boolean, typeOfBoolean, ""},
    97  		{"Counter", datatype.Counter, typeOfInt64, ""},
    98  		{"Custom", customType, typeOfByteSlice, ""},
    99  		{"Date", datatype.Date, typeOfTime, ""},
   100  		{"Decimal", datatype.Decimal, typeOfCqlDecimal, ""},
   101  		{"Double", datatype.Double, typeOfFloat64, ""},
   102  		{"Duration", datatype.Duration, typeOfCqlDuration, ""},
   103  		{"Float", datatype.Float, typeOfFloat32, ""},
   104  		{"Inet", datatype.Inet, typeOfNetIP, ""},
   105  		{"Int", datatype.Int, typeOfInt32, ""},
   106  		{"Smallint", datatype.Smallint, typeOfInt16, ""},
   107  		{"Time", datatype.Time, typeOfDuration, ""},
   108  		{"Timestamp", datatype.Timestamp, typeOfTime, ""},
   109  		{"Timeuuid", datatype.Timeuuid, typeOfUUID, ""},
   110  		{"Tinyint", datatype.Tinyint, typeOfInt8, ""},
   111  		{"Uuid", datatype.Uuid, typeOfUUID, ""},
   112  		{"Varchar", datatype.Varchar, typeOfString, ""},
   113  		{"Varint", datatype.Varint, typeOfBigIntPointer, ""},
   114  		{"List", listType, reflect.TypeOf([]*int32{}), ""},
   115  		{"Set", setType, reflect.TypeOf([]*int32{}), ""},
   116  		{"Map", mapType, reflect.TypeOf(map[*int32]*string{}), ""},
   117  		{"Tuple", tupleType, reflect.TypeOf([]interface{}{}), ""},
   118  		{"UserDefined", userDefinedType, reflect.TypeOf(map[string]interface{}{}), ""},
   119  		{"List wrong", datatype.NewList(wrongDataType{}), nil, "could not find any suitable Go type for CQL type 666"},
   120  		{"Set wrong", datatype.NewSet(wrongDataType{}), nil, "could not find any suitable Go type for CQL type 666"},
   121  		{"Map wrong key", datatype.NewMap(wrongDataType{}, datatype.Int), nil, "could not find any suitable Go type for CQL type 666"},
   122  		{"Map wrong value", datatype.NewMap(datatype.Int, wrongDataType{}), nil, "could not find any suitable Go type for CQL type 666"},
   123  		{"wrong", wrongDataType{}, nil, "could not find any suitable Go type for CQL type 666"},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			gotType, gotErr := PreferredGoType(tt.dt)
   128  			assert.Equal(t, tt.wantType, gotType, "expected %s, got %s", tt.wantType, gotType)
   129  			assertErrorMessage(t, tt.wantErr, gotErr)
   130  		})
   131  	}
   132  }