-
Notifications
You must be signed in to change notification settings - Fork 62
/
ulid_test.go
344 lines (279 loc) · 8.12 KB
/
ulid_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package strfmt
import (
"bytes"
"database/sql/driver"
"encoding/gob"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
)
const testUlid = string("01EYXZVGBHG26MFTG4JWR4K558")
const testUlidAlt = string("01EYXZW663G7PYHVSQ8WTMDA67")
var testUlidOverrideMtx sync.Mutex
var testUlidOverrideValMtx sync.Mutex
func TestFormatULID_Text(t *testing.T) {
t.Parallel()
t.Run("positive", func(t *testing.T) {
t.Parallel()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
res, err := ulid.MarshalText()
require.NoError(t, err)
assert.Equal(t, testUlid, string(res))
ulid2, _ := ParseULID(testUlidAlt)
require.NoError(t, err)
what := []byte(testUlid)
err = ulid2.UnmarshalText(what)
require.NoError(t, err)
assert.Equal(t, testUlid, ulid2.String())
})
t.Run("negative", func(t *testing.T) {
t.Parallel()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
what := []byte("00000000-0000-0000-0000-000000000000")
err = ulid.UnmarshalText(what)
require.Error(t, err)
})
}
func TestFormatULID_BSON(t *testing.T) {
t.Parallel()
t.Run("positive", func(t *testing.T) {
t.Parallel()
ulid, _ := ParseULID(testUlid)
bsonData, err := bson.Marshal(&ulid)
require.NoError(t, err)
var ulidUnmarshaled ULID
err = bson.Unmarshal(bsonData, &ulidUnmarshaled)
require.NoError(t, err)
assert.Equal(t, ulid, ulidUnmarshaled)
// Check value marshaling explicitly
m := bson.M{"data": ulid}
bsonData, err = bson.Marshal(&m)
require.NoError(t, err)
var mUnmarshaled bson.M
err = bson.Unmarshal(bsonData, &mUnmarshaled)
require.NoError(t, err)
data, ok := m["data"].(ULID)
assert.True(t, ok)
assert.Equal(t, ulid, data)
})
t.Run("negative", func(t *testing.T) {
t.Parallel()
uuid := UUID("00000000-0000-0000-0000-000000000000")
bsonData, err := bson.Marshal(&uuid)
require.NoError(t, err)
var ulidUnmarshaled ULID
err = bson.Unmarshal(bsonData, &ulidUnmarshaled)
require.Error(t, err)
})
}
func TestFormatULID_JSON(t *testing.T) {
t.Parallel()
t.Run("positive", func(t *testing.T) {
t.Parallel()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
whatStr := fmt.Sprintf(`"%s"`, testUlidAlt)
what := []byte(whatStr)
err = ulid.UnmarshalJSON(what)
require.NoError(t, err)
assert.Equal(t, testUlidAlt, ulid.String())
data, err := ulid.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, whatStr, string(data))
})
t.Run("null", func(t *testing.T) {
t.Parallel()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
err = ulid.UnmarshalJSON([]byte("null"))
require.NoError(t, err)
})
t.Run("negative", func(t *testing.T) {
t.Parallel()
// Check UnmarshalJSON failure with no lexed items
ulid := NewULIDZero()
err := ulid.UnmarshalJSON([]byte("zorg emperor"))
require.Error(t, err)
// Check lexer failure
err = ulid.UnmarshalJSON([]byte(`"zorg emperor"`))
require.Error(t, err)
})
}
func TestFormatULID_Scan(t *testing.T) {
t.Parallel()
t.Run("db.Scan", func(t *testing.T) {
t.Parallel()
testUlidOverrideMtx.Lock()
defer testUlidOverrideMtx.Unlock()
srcUlid := testUlidAlt
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
err = ulid.Scan(srcUlid)
require.NoError(t, err)
assert.Equal(t, srcUlid, ulid.String())
ulid, _ = ParseULID(testUlid)
err = ulid.Scan([]byte(srcUlid))
require.NoError(t, err)
assert.Equal(t, srcUlid, ulid.String())
})
t.Run("db.Scan_Failed", func(t *testing.T) {
t.Parallel()
testUlidOverrideMtx.Lock()
defer testUlidOverrideMtx.Unlock()
ulid, err := ParseULID(testUlid)
zero := NewULIDZero()
require.NoError(t, err)
err = ulid.Scan(nil)
require.NoError(t, err)
assert.Equal(t, zero, ulid)
err = ulid.Scan("")
require.NoError(t, err)
assert.Equal(t, zero, ulid)
err = ulid.Scan(int64(0))
require.Error(t, err)
err = ulid.Scan(float64(0))
require.Error(t, err)
})
t.Run("db.Value", func(t *testing.T) {
t.Parallel()
testUlidOverrideValMtx.Lock()
defer testUlidOverrideValMtx.Unlock()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
val, err := ulid.Value()
require.NoError(t, err)
assert.EqualValues(t, testUlid, val)
})
t.Run("override.Scan", func(t *testing.T) {
t.Parallel()
testUlidOverrideMtx.Lock()
defer testUlidOverrideMtx.Unlock()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
ulid2, err := ParseULID(testUlidAlt)
require.NoError(t, err)
ULIDScanOverrideFunc = func(raw interface{}) (ULID, error) {
u := NewULIDZero()
switch x := raw.(type) {
case [16]byte:
return u, u.ULID.UnmarshalBinary(x[:])
case int: // just for linter
return u, fmt.Errorf("cannot sql.Scan() strfmt.ULID from: %#v", raw)
}
return u, fmt.Errorf("cannot sql.Scan() strfmt.ULID from: %#v", raw)
}
// get underlying binary implementation which is actually [16]byte
bytes := [16]byte(ulid.ULID)
err = ulid2.Scan(bytes)
require.NoError(t, err)
assert.Equal(t, ulid2, ulid)
assert.Equal(t, ulid2.String(), ulid.String())
// check other default cases became unreachable
err = ulid2.Scan(testUlid)
require.Error(t, err)
// return default Scan method
ULIDScanOverrideFunc = ULIDScanDefaultFunc
err = ulid2.Scan(testUlid)
require.NoError(t, err)
assert.Equal(t, testUlid, ulid2.String())
})
t.Run("override.Value", func(t *testing.T) {
t.Parallel()
testUlidOverrideValMtx.Lock()
defer testUlidOverrideValMtx.Unlock()
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
ulid2, err := ParseULID(testUlid)
require.NoError(t, err)
ULIDValueOverrideFunc = func(u ULID) (driver.Value, error) {
bytes := [16]byte(u.ULID)
return driver.Value(bytes), nil
}
exp := [16]byte(ulid2.ULID)
val, err := ulid.Value()
require.NoError(t, err)
assert.EqualValues(t, exp, val)
// return default Value method
ULIDValueOverrideFunc = ULIDValueDefaultFunc
val, err = ulid.Value()
require.NoError(t, err)
assert.EqualValues(t, testUlid, val)
})
}
func TestFormatULID_DeepCopy(t *testing.T) {
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
in := &ulid
out := new(ULID)
in.DeepCopyInto(out)
assert.Equal(t, in, out)
out2 := in.DeepCopy()
assert.Equal(t, in, out2)
var inNil *ULID
out3 := inNil.DeepCopy()
assert.Nil(t, out3)
}
func TestFormatULID_GobEncoding(t *testing.T) {
ulid, err := ParseULID(testUlid)
require.NoError(t, err)
b := bytes.Buffer{}
enc := gob.NewEncoder(&b)
err = enc.Encode(ulid)
require.NoError(t, err)
assert.NotEmpty(t, b.Bytes())
var result ULID
dec := gob.NewDecoder(&b)
err = dec.Decode(&result)
require.NoError(t, err)
assert.Equal(t, ulid, result)
assert.Equal(t, ulid.String(), result.String())
}
func TestFormatULID_NewULID_and_Equal(t *testing.T) {
t.Parallel()
ulid1, err := NewULID()
require.NoError(t, err)
ulid2, err := NewULID()
require.NoError(t, err)
//nolint:gocritic
assert.True(t, ulid1.Equal(ulid1), "ULID instances should be equal")
assert.False(t, ulid1.Equal(ulid2), "ULID instances should not be equal")
ulidZero := NewULIDZero()
ulidZero2 := NewULIDZero()
assert.True(t, ulidZero.Equal(ulidZero2), "ULID instances should be equal")
}
func TestIsULID(t *testing.T) {
t.Parallel()
tcases := []struct {
ulid string
expect bool
}{
{ulid: "01EYXZVGBHG26MFTG4JWR4K558", expect: true},
{ulid: "01EYXZW663G7PYHVSQ8WTMDA67", expect: true},
{ulid: "7ZZZZZZZZZ0000000000000000", expect: true},
{ulid: "00000000000000000000000000", expect: true},
{ulid: "7ZZZZZZZZZZZZZZZZZZZZZZZZZ", expect: true},
{ulid: "not-a-ulid", expect: false},
{ulid: "8000000000FJ2MMFJ3ATV3XB2C", expect: false},
{ulid: "81EYY0NEYJZZZZZZZZZZZZZZZZ", expect: false},
{ulid: "7ZZZZZZZZZ000000000000000U", expect: false},
{ulid: "7ZZZZZZZZZ000000000000000L", expect: false},
{ulid: "7ZZZZZZZZZ000000000000000O", expect: false},
{ulid: "7ZZZZZZZZZ000000000000000I", expect: false},
}
for _, tcase := range tcases {
tc := tcase
t.Run(fmt.Sprintf("%s:%t", tc.ulid, tc.expect), func(t *testing.T) {
t.Parallel()
if tc.expect {
assert.True(t, IsULID(tc.ulid))
} else {
assert.False(t, IsULID(tc.ulid))
}
})
}
}