-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathid_test.go
86 lines (77 loc) · 1.52 KB
/
id_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
80
81
82
83
84
85
86
package xidb
import (
"reflect"
"testing"
"github.com/rs/xid"
)
func TestIDValue(t *testing.T) {
i, _ := xid.FromString("9m4e2mr0ui3e8a215n4g")
tests := []struct {
name string
id ID
expectedVal interface{}
}{
{
name: "non nil id",
id: ID{ID: i},
expectedVal: i.Bytes(),
},
{
name: "nil id",
id: ID{ID: xid.NilID()},
expectedVal: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := tt.id.Value()
if !reflect.DeepEqual(got, tt.expectedVal) {
t.Errorf("wanted %v, got %v", tt.expectedVal, got)
}
})
}
}
func TestIDScan(t *testing.T) {
i, _ := xid.FromString("9m4e2mr0ui3e8a215n4g")
tests := []struct {
name string
val interface{}
expectedID ID
expectedErr bool
}{
{
name: "bytes id",
val: i.Bytes(),
expectedID: ID{ID: i},
},
{
name: "nil id",
val: nil,
expectedID: ID{ID: xid.NilID()},
},
{
name: "wrong bytes",
val: []byte{0x01},
expectedErr: true,
},
{
name: "unknown type",
val: 1,
expectedErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id := &ID{}
err := id.Scan(tt.val)
if (err != nil) != tt.expectedErr {
t.Errorf("error expected: %t, got %t", tt.expectedErr, (err != nil))
}
if err == nil {
if !reflect.DeepEqual(id.ID, tt.expectedID.ID) {
t.Errorf("wanted %v, got %v", tt.expectedID, id)
}
}
})
}
}