forked from google/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
null_test.go
214 lines (198 loc) · 4.92 KB
/
null_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2021 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package uuid
import (
"bytes"
"encoding/json"
"testing"
)
func TestNullUUIDScan(t *testing.T) {
var u UUID
var nu NullUUID
uNilErr := u.Scan(nil)
nuNilErr := nu.Scan(nil)
if uNilErr != nil &&
nuNilErr != nil &&
uNilErr.Error() != nuNilErr.Error() {
t.Errorf("expected errors to be equal, got %s, %s", uNilErr, nuNilErr)
}
uInvalidStringErr := u.Scan("test")
nuInvalidStringErr := nu.Scan("test")
if uInvalidStringErr != nil &&
nuInvalidStringErr != nil &&
uInvalidStringErr.Error() != nuInvalidStringErr.Error() {
t.Errorf("expected errors to be equal, got %s, %s", uInvalidStringErr, nuInvalidStringErr)
}
valid := "12345678-abcd-1234-abcd-0123456789ab"
uValidErr := u.Scan(valid)
nuValidErr := nu.Scan(valid)
if uValidErr != nuValidErr {
t.Errorf("expected errors to be equal, got %s, %s", uValidErr, nuValidErr)
}
}
func TestNullUUIDValue(t *testing.T) {
var u UUID
var nu NullUUID
nuValue, nuErr := nu.Value()
if nuErr != nil {
t.Errorf("expected nil err, got err %s", nuErr)
}
if nuValue != nil {
t.Errorf("expected nil value, got non-nil %s", nuValue)
}
u = MustParse("12345678-abcd-1234-abcd-0123456789ab")
nu = NullUUID{
UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"),
Valid: true,
}
uValue, uErr := u.Value()
nuValue, nuErr = nu.Value()
if uErr != nil {
t.Errorf("expected nil err, got err %s", uErr)
}
if nuErr != nil {
t.Errorf("expected nil err, got err %s", nuErr)
}
if uValue != nuValue {
t.Errorf("expected uuid %s and nulluuid %s to be equal ", uValue, nuValue)
}
}
func TestNullUUIDMarshalText(t *testing.T) {
tests := []struct {
nullUUID NullUUID
}{
{
nullUUID: NullUUID{},
},
{
nullUUID: NullUUID{
UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"),
Valid: true,
},
},
}
for _, test := range tests {
var uText []byte
var uErr error
nuText, nuErr := test.nullUUID.MarshalText()
if test.nullUUID.Valid {
uText, uErr = test.nullUUID.UUID.MarshalText()
} else {
uText = []byte("null")
}
if nuErr != uErr {
t.Errorf("expected error %e, got %e", nuErr, uErr)
}
if !bytes.Equal(nuText, uText) {
t.Errorf("expected text data %s, got %s", string(nuText), string(uText))
}
}
}
func TestNullUUIDUnmarshalText(t *testing.T) {
tests := []struct {
nullUUID NullUUID
}{
{
nullUUID: NullUUID{},
},
{
nullUUID: NullUUID{
UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"),
Valid: true,
},
},
}
for _, test := range tests {
var uText []byte
var uErr error
nuText, nuErr := test.nullUUID.MarshalText()
if test.nullUUID.Valid {
uText, uErr = test.nullUUID.UUID.MarshalText()
} else {
uText = []byte("null")
}
if nuErr != uErr {
t.Errorf("expected error %e, got %e", nuErr, uErr)
}
if !bytes.Equal(nuText, uText) {
t.Errorf("expected text data %s, got %s", string(nuText), string(uText))
}
}
}
func TestNullUUIDMarshalBinary(t *testing.T) {
tests := []struct {
nullUUID NullUUID
}{
{
nullUUID: NullUUID{},
},
{
nullUUID: NullUUID{
UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"),
Valid: true,
},
},
}
for _, test := range tests {
var uBinary []byte
var uErr error
nuBinary, nuErr := test.nullUUID.MarshalBinary()
if test.nullUUID.Valid {
uBinary, uErr = test.nullUUID.UUID.MarshalBinary()
} else {
uBinary = []byte(nil)
}
if nuErr != uErr {
t.Errorf("expected error %e, got %e", nuErr, uErr)
}
if !bytes.Equal(nuBinary, uBinary) {
t.Errorf("expected binary data %s, got %s", string(nuBinary), string(uBinary))
}
}
}
func TestNullUUIDMarshalJSON(t *testing.T) {
jsonNull, _ := json.Marshal(nil)
jsonUUID, _ := json.Marshal(MustParse("12345678-abcd-1234-abcd-0123456789ab"))
tests := []struct {
nullUUID NullUUID
expected []byte
expectedErr error
}{
{
nullUUID: NullUUID{},
expected: jsonNull,
expectedErr: nil,
},
{
nullUUID: NullUUID{
UUID: MustParse(string(jsonUUID)),
Valid: true,
},
expected: []byte(`"12345678-abcd-1234-abcd-0123456789ab"`),
expectedErr: nil,
},
}
for _, test := range tests {
data, err := json.Marshal(&test.nullUUID)
if err != test.expectedErr {
t.Errorf("expected error %e, got %e", test.expectedErr, err)
}
if !bytes.Equal(data, test.expected) {
t.Errorf("expected json data %s, got %s", string(test.expected), string(data))
}
}
}
func TestNullUUIDUnmarshalJSON(t *testing.T) {
jsonNull, _ := json.Marshal(nil)
jsonUUID, _ := json.Marshal(MustParse("12345678-abcd-1234-abcd-0123456789ab"))
var nu NullUUID
err := json.Unmarshal(jsonNull, &nu)
if err != nil || nu.Valid {
t.Errorf("expected nil when unmarshalling null, got %s", err)
}
err = json.Unmarshal(jsonUUID, &nu)
if err != nil || !nu.Valid {
t.Errorf("expected nil when unmarshalling null, got %s", err)
}
}