-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeid_test.go
210 lines (173 loc) · 4.96 KB
/
typeid_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
package typeid
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"github.com/gofrs/uuid/v5"
)
const (
userIDPrefix = "user"
accountIDPrefix = "account"
emptyID = "00000000000000000000000000" // empty is suffix is 26 zeros
)
type userPrefix struct{}
func (userPrefix) Prefix() string {
return userIDPrefix
}
type accountPrefix struct{}
func (accountPrefix) Prefix() string {
return accountIDPrefix
}
type nilPrefix struct{}
func (nilPrefix) Prefix() string {
return ""
}
type UserID = Random[userPrefix]
type AccountID = Sortable[accountPrefix]
type NilID = Random[nilPrefix]
func TestTypeID_New(t *testing.T) {
t.Parallel()
userID, err := New[UserID]()
if err != nil {
t.Fatalf("create UserID: unexpected error:\n%+v", err)
}
if uuid.V4 != userID.UUID().Version() {
t.Errorf("expected UUIDv4, got version byte: %x", userID.UUID().Version())
}
accountID, err := New[AccountID]()
if err != nil {
t.Fatalf("create AserID: unexpected error:\n%+v", err)
}
if uuid.V7 != accountID.UUID().Version() {
t.Errorf("expected UUIDv7, got version byte: %x", accountID.UUID().Version())
}
}
func TestTypeID_Nil(t *testing.T) {
t.Parallel()
nilUserID := Nil[UserID]()
if "user_"+emptyID != nilUserID.String() {
t.Errorf("expected nil user id, got: %s", nilUserID.String())
}
if nilUserID != Nil[UserID]() {
t.Errorf("two nil id's are equal\nGot: %v\nExpected: %v", nilUserID, Nil[UserID]())
}
nilAccountID := Nil[AccountID]()
if "account_"+emptyID != nilAccountID.String() {
t.Errorf("expected nil account id, got: %s", nilAccountID.String())
}
nilID := Nil[NilID]()
if emptyID != nilID.String() {
t.Errorf("two nil id's are equal\nGot: %v\nExpected: %v", emptyID, nilID.String())
}
}
func TestTypeID_ToFrom(t *testing.T) {
t.Parallel()
type RandomID = UserID
t.Run("typeid.Random", runToFromQuickTests[RandomID])
type SortableID = AccountID
t.Run("typeid.Sortable", runToFromQuickTests[SortableID])
}
func runToFromQuickTests[T idImplementation[P], P Prefix](t *testing.T) {
t.Helper()
t.Parallel()
t.Run("from string", func(t *testing.T) {
t.Parallel()
if err := quick.Check(fromStringTester[T](t), nil); err != nil {
t.Error(err)
}
})
t.Run("from UUID", func(t *testing.T) {
t.Parallel()
if err := quick.Check(fromUUIDTester[T](t), nil); err != nil {
t.Error(err)
}
})
t.Run("from UUID string", func(t *testing.T) {
t.Parallel()
if err := quick.Check(fromUUIDStringTester[T](t), nil); err != nil {
t.Error(err)
}
})
t.Run("from UUID bytes", func(t *testing.T) {
t.Parallel()
if err := quick.Check(fromUUIDBytesTester[T](t), nil); err != nil {
t.Error(err)
}
})
}
func fromStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromString[T](wid.ID().String())
if err != nil {
t.Fatalf("parse type id from string: unexpected error:\n%+v", err)
}
return wid.ID() == parsedID
}
}
func fromUUIDTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUID[T](wid.ID().UUID())
if err != nil {
t.Fatalf("parse type id from UUID: unexpected error:\n%+v", err)
}
return wid.ID() == parsedID
}
}
func fromUUIDStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUIDStr[T](wid.ID().UUID().String())
if err != nil {
t.Fatalf("parse type id from UUID string: unexpected error:\n%+v", err)
}
return wid.ID() == parsedID
}
}
func fromUUIDBytesTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUIDBytes[T](wid.id.UUID().Bytes())
if err != nil {
t.Fatalf("parse type id from UUID bytes: unexpected error:\n%+v", err)
}
return wid.ID() == parsedID
}
}
// wrappedID is a helper struct that implements [quick.Generator] to facilitate simple property-based tests.
// We require this helper because we cannot directly implement interfaces on type aliases.
type wrappedID[T instance[P], P Prefix] struct {
id T
}
func (w wrappedID[T, P]) ID() T {
return w.id
}
func (w wrappedID[T, P]) Generate(rnd *rand.Rand, _ int) reflect.Value {
// gen the processor to determine the UUID version to use
procGenUUID, err := (T{}).processor().generateUUID()
if err != nil {
panic(err)
}
version := procGenUUID.Version()
uuidGen := uuid.NewGenWithOptions(uuid.WithRandomReader(rnd))
var uid uuid.UUID
switch version {
case uuid.V4:
if uid, err = uuidGen.NewV4(); err != nil {
panic("failed to generate uuid v4")
}
case uuid.V7:
if uid, err = uuidGen.NewV7(); err != nil {
panic("failed to generate uuid v7")
}
default:
panic("unknown uuid version")
}
tid, err := FromUUID[T](uid)
if err != nil {
panic(err)
}
return reflect.ValueOf(wrappedID[T, P]{tid})
}