github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/uuid_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 primitive 16 17 import ( 18 "bytes" 19 "errors" 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 var uuid = UUID{0xC0, 0xD1, 0xD2, 0x1E, 0xBB, 0x01, 0x41, 0x96, 0x86, 0xDB, 0xBC, 0x31, 0x7B, 0xC1, 0x79, 0x6A} 27 var uuidBytes = [16]byte{0xC0, 0xD1, 0xD2, 0x1E, 0xBB, 0x01, 0x41, 0x96, 0x86, 0xDB, 0xBC, 0x31, 0x7B, 0xC1, 0x79, 0x6A} 28 29 func TestReadUuid(t *testing.T) { 30 tests := []struct { 31 name string 32 source []byte 33 expected *UUID 34 remaining []byte 35 err error 36 }{ 37 {"simple UUID", uuidBytes[:], &uuid, []byte{}, nil}, 38 {"UUID with remaining", append(uuidBytes[:], 1, 2, 3, 4), &uuid, []byte{1, 2, 3, 4}, nil}, 39 { 40 "cannot read UUID", 41 uuidBytes[:15], 42 nil, 43 []byte{}, 44 fmt.Errorf("cannot read [uuid] content: %w", errors.New("unexpected EOF")), 45 }, 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 buf := bytes.NewBuffer(tt.source) 50 actual, err := ReadUuid(buf) 51 assert.Equal(t, tt.expected, actual) 52 assert.Equal(t, tt.remaining, buf.Bytes()) 53 assert.Equal(t, tt.err, err) 54 }) 55 } 56 } 57 58 func TestWriteUuid(t *testing.T) { 59 tests := []struct { 60 name string 61 input *UUID 62 expected []byte 63 err error 64 }{ 65 { 66 "simple UUID", 67 &uuid, 68 uuidBytes[:], 69 nil, 70 }, 71 { 72 "UUID with remaining", 73 &uuid, 74 uuidBytes[:], 75 nil, 76 }, 77 { 78 "nil UUID", 79 nil, 80 nil, 81 errors.New("cannot write nil [uuid]"), 82 }, 83 } 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 buf := &bytes.Buffer{} 87 err := WriteUuid(tt.input, buf) 88 assert.Equal(t, tt.expected, buf.Bytes()) 89 assert.Equal(t, tt.err, err) 90 }) 91 } 92 } 93 94 func TestUUID_DeepCopy(t *testing.T) { 95 u := &UUID{0, 1, 2, 3, 4, 5, 6} 96 cloned := u.DeepCopy() 97 98 assert.Equal(t, u, cloned) 99 100 cloned[1] = 9 101 assert.NotEqual(t, u, cloned) 102 }