-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
99 lines (91 loc) · 1.9 KB
/
uuid_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
87
88
89
90
91
92
93
94
95
96
97
98
99
package uuid
import (
"bytes"
"encoding/gob"
"encoding/json"
"testing"
"github.com/globalsign/mgo/bson"
)
func TestUUID(t *testing.T) {
uuid := New()
println("UUID: ", uuid.String())
data, err := json.Marshal(uuid)
if err != nil {
t.Error(err)
}
println("JSON: ", string(data))
var newUUID UUID
if err := json.Unmarshal(data, &newUUID); err != nil {
t.Error(err)
}
println("RESTORE:", newUUID.String())
if !uuid.Equal(newUUID) {
t.Error("bad restore")
}
if newUUID.Version() != 4 {
t.Error("bad version", newUUID.Version())
}
data, err = bson.Marshal(uuid)
if err != nil {
t.Fatal(err)
}
if err := bson.Unmarshal(data, &newUUID); err != nil {
t.Fatal(err)
}
println("RESTORE:", newUUID.String())
if !uuid.Equal(newUUID) {
t.Error("bad restore")
}
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(uuid)
if err != nil {
t.Error(err)
}
err = gob.NewDecoder(&buf).Decode(&newUUID)
if err != nil {
t.Error(err)
}
println("RESTORE:", newUUID.String())
data, err = bson.Marshal(bson.Binary{
Kind: 0x05,
Data: uuid.Bytes(),
})
if err != nil {
t.Error(err)
}
err = bson.Unmarshal(data, &newUUID)
if err == nil {
t.Error("bad SetBSON")
}
data, err = bson.Marshal(bson.Binary{
Kind: 0x04,
Data: uuid.Bytes()[1:],
})
if err != nil {
t.Error(err)
}
err = bson.Unmarshal(data, &newUUID)
if err == nil {
t.Error("bad SetBSON")
}
}
func TestUUIDUnmarshal(t *testing.T) {
for _, uuidStr := range []string{
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"6ba7b8109dad11d180b400c04fd430c8",
} {
_, err := Parse(uuidStr)
if err != nil {
t.Error(err)
}
}
var uuid UUID
if uuid.UnmarshalText([]byte("12345678")) == nil {
t.Error("bad unmarshal")
}
if uuid.UnmarshalText([]byte("6ba7b8109dad11d180b400c04fd430cw")) == nil {
t.Error("bad unmarshal")
}
}