github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datatype/custom_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 TestCustomType(t *testing.T) { 29 customType := NewCustom("foo.bar.qix") 30 assert.Equal(t, primitive.DataTypeCodeCustom, customType.Code()) 31 assert.Equal(t, "foo.bar.qix", customType.ClassName) 32 } 33 34 func TestWriteCustomType(t *testing.T) { 35 for _, version := range primitive.SupportedProtocolVersions() { 36 t.Run(version.String(), func(t *testing.T) { 37 tests := []struct { 38 name string 39 input DataType 40 expected []byte 41 err error 42 }{ 43 {"simple custom", NewCustom("hello"), []byte{0, 5, byte('h'), byte('e'), byte('l'), byte('l'), byte('o')}, nil}, 44 {"nil custom", nil, nil, errors.New("expected *Custom, got <nil>")}, 45 } 46 for _, test := range tests { 47 t.Run(test.name, func(t *testing.T) { 48 var dest = &bytes.Buffer{} 49 var err error 50 err = writeCustomType(test.input, dest, version) 51 actual := dest.Bytes() 52 assert.Equal(t, test.expected, actual) 53 assert.Equal(t, test.err, err) 54 }) 55 } 56 }) 57 } 58 } 59 60 func TestLengthOfCustomType(t *testing.T) { 61 for _, version := range primitive.SupportedProtocolVersions() { 62 t.Run(version.String(), func(t *testing.T) { 63 tests := []struct { 64 name string 65 input DataType 66 expected int 67 err error 68 }{ 69 {"simple custom", NewCustom("hello"), primitive.LengthOfString("hello"), nil}, 70 {"nil custom", nil, -1, errors.New("expected *Custom, got <nil>")}, 71 } 72 for _, test := range tests { 73 t.Run(test.name, func(t *testing.T) { 74 var actual int 75 var err error 76 actual, err = lengthOfCustomType(test.input, version) 77 assert.Equal(t, test.expected, actual) 78 assert.Equal(t, test.err, err) 79 }) 80 } 81 }) 82 } 83 } 84 85 func TestReadCustomType(t *testing.T) { 86 for _, version := range primitive.SupportedProtocolVersions() { 87 t.Run(version.String(), func(t *testing.T) { 88 tests := []struct { 89 name string 90 input []byte 91 expected DataType 92 err error 93 }{ 94 {"simple custom", []byte{0, 5, byte('h'), byte('e'), byte('l'), byte('l'), byte('o')}, NewCustom("hello"), nil}, 95 { 96 "cannot read custom", 97 []byte{}, 98 nil, 99 fmt.Errorf("cannot read custom type class name: %w", 100 fmt.Errorf("cannot read [string] length: %w", 101 fmt.Errorf("cannot read [short]: %w", 102 errors.New("EOF")))), 103 }, 104 } 105 for _, test := range tests { 106 t.Run(test.name, func(t *testing.T) { 107 var source = bytes.NewBuffer(test.input) 108 var actual DataType 109 var err error 110 actual, err = readCustomType(source, version) 111 assert.Equal(t, test.expected, actual) 112 assert.Equal(t, test.err, err) 113 }) 114 } 115 }) 116 } 117 } 118 119 func TestCustomTypeDeepCopy(t *testing.T) { 120 ct := NewCustom("foo.bar.qix") 121 clonedCustomType := ct.DeepCopy() 122 assert.Equal(t, ct, clonedCustomType) 123 clonedCustomType.ClassName = "123" 124 assert.Equal(t, "123", clonedCustomType.ClassName) 125 assert.Equal(t, "foo.bar.qix", ct.ClassName) 126 }