git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/uuid/sql_test.go (about) 1 // Copyright 2016 Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uuid 6 7 import ( 8 "bytes" 9 "strings" 10 "testing" 11 ) 12 13 func TestScan(t *testing.T) { 14 stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479" 15 badTypeTest := 6 16 invalidTest := "f47ac10b-58cc-0372-8567-0e02b2c3d4" 17 18 byteTest := make([]byte, 16) 19 byteTestUUID := Must(Parse(stringTest)) 20 copy(byteTest, byteTestUUID[:]) 21 22 // sunny day tests 23 24 var uuid UUID 25 err := (&uuid).Scan(stringTest) 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 err = (&uuid).Scan([]byte(stringTest)) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 err = (&uuid).Scan(byteTest) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 // bad type tests 41 42 err = (&uuid).Scan(badTypeTest) 43 if err == nil { 44 t.Error("int correctly parsed and shouldn't have") 45 } 46 if !strings.Contains(err.Error(), "unable to scan type") { 47 t.Error("attempting to parse an int returned an incorrect error message") 48 } 49 50 // invalid/incomplete uuids 51 52 err = (&uuid).Scan(invalidTest) 53 if err == nil { 54 t.Error("invalid uuid was parsed without error") 55 } 56 if !strings.Contains(err.Error(), "invalid UUID") { 57 t.Error("attempting to parse an invalid UUID returned an incorrect error message") 58 } 59 60 err = (&uuid).Scan(byteTest[:len(byteTest)-2]) 61 if err == nil { 62 t.Error("invalid byte uuid was parsed without error") 63 } 64 if !strings.Contains(err.Error(), "invalid UUID") { 65 t.Error("attempting to parse an invalid byte UUID returned an incorrect error message") 66 } 67 68 // empty tests 69 70 uuid = UUID{} 71 var emptySlice []byte 72 err = (&uuid).Scan(emptySlice) 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 for _, v := range uuid { 78 if v != 0 { 79 t.Error("UUID was not nil after scanning empty byte slice") 80 } 81 } 82 83 uuid = UUID{} 84 var emptyString string 85 err = (&uuid).Scan(emptyString) 86 if err != nil { 87 t.Fatal(err) 88 } 89 for _, v := range uuid { 90 if v != 0 { 91 t.Error("UUID was not nil after scanning empty byte slice") 92 } 93 } 94 95 uuid = UUID{} 96 err = (&uuid).Scan(nil) 97 if err != nil { 98 t.Fatal(err) 99 } 100 for _, v := range uuid { 101 if v != 0 { 102 t.Error("UUID was not nil after scanning nil") 103 } 104 } 105 } 106 107 func TestValue(t *testing.T) { 108 stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479" 109 uuid := Must(Parse(stringTest)) 110 val, _ := uuid.Value() 111 if !bytes.Equal(uuid[:], val.([]byte)) { 112 t.Error("Value() did not return expected string") 113 } 114 }