-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsubtype_test.go
100 lines (87 loc) · 2.76 KB
/
subtype_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
package typeid_test
import (
"fmt"
"testing"
"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"go.jetify.com/typeid"
)
func ExampleNew() {
tid := typeid.Must(typeid.New[AccountID]())
fmt.Println("Prefix:", tid.Prefix())
// Output:
// Prefix: account
}
func ExampleFromSuffix() {
tid := typeid.Must(typeid.FromSuffix[UserID]("00041061050r3gg28a1c60t3gf"))
fmt.Printf("Prefix: %s\nSuffix: %s\n", tid.Prefix(), tid.Suffix())
// Output:
// Prefix: user
// Suffix: 00041061050r3gg28a1c60t3gf
}
func TestSubtypeConstructors(t *testing.T) {
// These constructors should work for a subtype:
_, err := typeid.New[AccountID]()
assert.NoError(t, err)
_, err = typeid.FromSuffix[AccountID]("00041061050r3gg28a1c60t3gf")
assert.NoError(t, err)
// But error on TypeID[typeid.Any]:
_, err = typeid.New[typeid.AnyID]()
assert.Error(t, err)
_, err = typeid.FromSuffix[typeid.AnyID]("00041061050r3gg28a1c60t3gf")
assert.Error(t, err)
}
func TestSubtypeNil(t *testing.T) {
var emptyUser UserID
nilUser, err := typeid.Parse[UserID]("user_00000000000000000000000000")
assert.NoError(t, err)
assert.Equal(t, nilUser, emptyUser)
assert.Equal(t, nilUser.String(), emptyUser.String())
assert.Equal(t, nilUser.Prefix(), emptyUser.Prefix())
assert.Equal(t, nilUser.UUID(), emptyUser.UUID())
assert.Equal(t, nilUser.UUIDBytes(), emptyUser.UUIDBytes())
assert.Equal(t, "user_00000000000000000000000000", nilUser.String())
assert.Equal(t, "user", nilUser.Prefix())
parsed, err := typeid.FromString("user_00000000000000000000000000")
assert.NoError(t, err)
assert.Equal(t, "user_00000000000000000000000000", parsed.String())
assert.Equal(t, "user", parsed.Prefix())
assert.Equal(t, "00000000000000000000000000", parsed.Suffix())
}
func TestParse(t *testing.T) {
// Generate a bunch of random UserIDs. We should be able to parse them
// using the correct type, but not an incorrect one.
for i := 0; i < 1000; i++ {
tid := typeid.Must(typeid.New[UserID]())
// They parse as UserID
parsed, err := typeid.Parse[UserID](tid.String())
if err != nil {
t.Error(err)
}
if tid != parsed {
t.Errorf("Expected %s, got %s", tid, parsed)
}
// They also parse as a generic TypeID
_, err = typeid.FromString(tid.String())
if err != nil {
t.Error(err)
}
// But not as an AccountID
_, err = typeid.Parse[AccountID](tid.String())
assert.Error(t, err)
}
}
func TestFromUUID(t *testing.T) {
uid, err := uuid.NewV7()
assert.NoError(t, err)
id, err := typeid.FromUUID[UserID](uid.String())
assert.NoError(t, err)
assert.Equal(t, uid.String(), id.UUID())
}
func TestFromUUIDBytes(t *testing.T) {
uid, err := uuid.NewV7()
assert.NoError(t, err)
id, err := typeid.FromUUIDBytes[UserID](uid.Bytes())
assert.NoError(t, err)
assert.Equal(t, uid.Bytes(), id.UUIDBytes())
}