-
Notifications
You must be signed in to change notification settings - Fork 504
/
uniqueidentifier_test.go
79 lines (68 loc) · 2.08 KB
/
uniqueidentifier_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mssql
import (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"testing"
)
func TestUniqueIdentifier(t *testing.T) {
dbUUID := UniqueIdentifier{0x67, 0x45, 0x23, 0x01,
0xAB, 0x89,
0xEF, 0xCD,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
}
uuid := UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
t.Run("Scan", func(t *testing.T) {
t.Run("[]byte", func(t *testing.T) {
var sut UniqueIdentifier
if err := sut.Scan(dbUUID[:]); err != nil {
t.Fatal(err)
}
if sut != uuid {
t.Errorf("bytes not swapped correctly: got %q; want %q", sut, uuid)
}
})
t.Run("string", func(t *testing.T) {
var sut UniqueIdentifier
if err := sut.Scan(uuid.String()); err != nil {
t.Fatal(err)
}
if sut != uuid {
t.Errorf("string not scanned correctly: got %q; want %q", sut, uuid)
}
})
})
t.Run("Value", func(t *testing.T) {
sut := uuid
v, err := sut.Value()
if err != nil {
t.Fatal(err)
}
b, ok := v.([]byte)
if !ok {
t.Fatalf("(%T) is not []byte", v)
}
if !bytes.Equal(b, dbUUID[:]) {
t.Errorf("got %q; want %q", b, dbUUID)
}
})
}
func TestUniqueIdentifierString(t *testing.T) {
sut := UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
expected := "01234567-89AB-CDEF-0123-456789ABCDEF"
if actual := sut.String(); actual != expected {
t.Errorf("sut.String() = %s; want %s", sut, expected)
}
}
func TestUniqueIdentifierMarshalText(t *testing.T) {
sut := UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
expected := []byte{48, 49, 50, 51, 52, 53, 54, 55, 45, 56, 57, 65, 66, 45, 67, 68, 69, 70, 45, 48, 49, 50, 51, 45, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70}
if actual := sut.MarshalText(); !reflect.DeepEqual(actual, expected) {
t.Errorf("sut.MarshalText() = %v; want %v", actual, expected)
}
}
var _ fmt.Stringer = UniqueIdentifier{}
var _ sql.Scanner = &UniqueIdentifier{}
var _ driver.Valuer = UniqueIdentifier{}