github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datatype/map_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 datatype 16 17 import ( 18 "bytes" 19 "errors" 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 "github.com/datastax/go-cassandra-native-protocol/primitive" 26 ) 27 28 func TestMapType(t *testing.T) { 29 mapType := NewMap(Varchar, Int) 30 assert.Equal(t, primitive.DataTypeCodeMap, mapType.Code()) 31 assert.Equal(t, Varchar, mapType.KeyType) 32 assert.Equal(t, Int, mapType.ValueType) 33 } 34 35 func TestMapTypeDeepCopy(t *testing.T) { 36 mt := NewMap(Varchar, Int) 37 cloned := mt.DeepCopy() 38 assert.Equal(t, mt, cloned) 39 cloned.KeyType = Inet 40 cloned.ValueType = Uuid 41 assert.Equal(t, primitive.DataTypeCodeMap, mt.Code()) 42 assert.Equal(t, Varchar, mt.KeyType) 43 assert.Equal(t, Int, mt.ValueType) 44 assert.Equal(t, primitive.DataTypeCodeMap, cloned.Code()) 45 assert.Equal(t, Inet, cloned.KeyType) 46 assert.Equal(t, Uuid, cloned.ValueType) 47 } 48 49 func TestWriteMapType(t *testing.T) { 50 for _, version := range primitive.SupportedProtocolVersions() { 51 t.Run(version.String(), func(t *testing.T) { 52 tests := []struct { 53 name string 54 input DataType 55 expected []byte 56 err error 57 }{ 58 { 59 "simple map", 60 NewMap(Varchar, Int), 61 []byte{ 62 0, byte(primitive.DataTypeCodeVarchar & 0xFF), 63 0, byte(primitive.DataTypeCodeInt & 0xFF), 64 }, 65 nil, 66 }, 67 { 68 "complex map", 69 NewMap(NewMap(Varchar, Int), NewMap(Boolean, Float)), 70 []byte{ 71 0, byte(primitive.DataTypeCodeMap & 0xFF), 72 0, byte(primitive.DataTypeCodeVarchar & 0xFF), 73 0, byte(primitive.DataTypeCodeInt & 0xFF), 74 0, byte(primitive.DataTypeCodeMap & 0xFF), 75 0, byte(primitive.DataTypeCodeBoolean & 0xFF), 76 0, byte(primitive.DataTypeCodeFloat & 0xFF), 77 }, 78 nil, 79 }, 80 {"nil map", nil, nil, errors.New("expected *Map, got <nil>")}, 81 } 82 for _, test := range tests { 83 t.Run(test.name, func(t *testing.T) { 84 var dest = &bytes.Buffer{} 85 var err error 86 err = writeMapType(test.input, dest, version) 87 actual := dest.Bytes() 88 assert.Equal(t, test.expected, actual) 89 assert.Equal(t, test.err, err) 90 }) 91 } 92 }) 93 } 94 } 95 96 func TestLengthOfMapType(t *testing.T) { 97 for _, version := range primitive.SupportedProtocolVersions() { 98 t.Run(version.String(), func(t *testing.T) { 99 tests := []struct { 100 name string 101 input DataType 102 expected int 103 err error 104 }{ 105 { 106 "simple map", 107 NewMap(Varchar, Int), 108 primitive.LengthOfShort * 2, 109 nil, 110 }, 111 { 112 "complex map", 113 NewMap(NewMap(Varchar, Int), NewMap(Boolean, Float)), 114 primitive.LengthOfShort * 6, 115 nil, 116 }, 117 {"nil map", nil, -1, errors.New("expected *Map, got <nil>")}, 118 } 119 for _, test := range tests { 120 t.Run(test.name, func(t *testing.T) { 121 var actual int 122 var err error 123 actual, err = lengthOfMapType(test.input, version) 124 assert.Equal(t, test.expected, actual) 125 assert.Equal(t, test.err, err) 126 }) 127 } 128 }) 129 } 130 } 131 132 func TestReadMapType(t *testing.T) { 133 for _, version := range primitive.SupportedProtocolVersions() { 134 t.Run(version.String(), func(t *testing.T) { 135 tests := []struct { 136 name string 137 input []byte 138 expected DataType 139 err error 140 }{ 141 { 142 "simple map", 143 []byte{ 144 0, byte(primitive.DataTypeCodeVarchar & 0xFF), 145 0, byte(primitive.DataTypeCodeInt & 0xFF), 146 }, 147 NewMap(Varchar, Int), 148 nil, 149 }, 150 { 151 "complex map", 152 []byte{ 153 0, byte(primitive.DataTypeCodeMap & 0xFF), 154 0, byte(primitive.DataTypeCodeVarchar & 0xFF), 155 0, byte(primitive.DataTypeCodeInt & 0xFF), 156 0, byte(primitive.DataTypeCodeMap & 0xFF), 157 0, byte(primitive.DataTypeCodeBoolean & 0xFF), 158 0, byte(primitive.DataTypeCodeFloat & 0xFF), 159 }, 160 NewMap(NewMap(Varchar, Int), NewMap(Boolean, Float)), 161 nil, 162 }, 163 { 164 "cannot read map", 165 []byte{}, 166 nil, 167 fmt.Errorf("cannot read map key type: %w", 168 fmt.Errorf("cannot read data type code: %w", 169 fmt.Errorf("cannot read [short]: %w", 170 errors.New("EOF")))), 171 }, 172 } 173 for _, test := range tests { 174 t.Run(test.name, func(t *testing.T) { 175 var source = bytes.NewBuffer(test.input) 176 var actual DataType 177 var err error 178 actual, err = readMapType(source, version) 179 assert.Equal(t, test.expected, actual) 180 assert.Equal(t, test.err, err) 181 }) 182 } 183 }) 184 } 185 }