-
Notifications
You must be signed in to change notification settings - Fork 7
/
typeid_test.go
240 lines (214 loc) · 6.38 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
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
package typeid_test
import (
_ "embed"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"go.jetify.com/typeid"
"gopkg.in/yaml.v2"
)
func ExampleWithPrefix() {
tid := typeid.Must(typeid.WithPrefix("prefix"))
fmt.Printf("New typeid: %s\n", tid)
}
func ExampleWithPrefix_emptyPrefix() {
tid := typeid.Must(typeid.WithPrefix(""))
fmt.Printf("New typeid without prefix: %s\n", tid)
}
func ExampleFromString() {
tid := typeid.Must(typeid.FromString("prefix_00041061050r3gg28a1c60t3gf"))
fmt.Printf("Prefix: %s\nSuffix: %s\n", tid.Prefix(), tid.Suffix())
// Output:
// Prefix: prefix
// Suffix: 00041061050r3gg28a1c60t3gf
}
func TestNilIsEmpty(t *testing.T) {
var emptyID typeid.AnyID
nilID, err := typeid.FromString("00000000000000000000000000")
assert.NoError(t, err)
assert.Equal(t, nilID, emptyID)
assert.Equal(t, nilID.String(), emptyID.String())
assert.Equal(t, nilID.UUID(), emptyID.UUID())
assert.Equal(t, nilID.UUIDBytes(), emptyID.UUIDBytes())
}
func TestIsZero(t *testing.T) {
testdata := []struct {
input string
output bool
}{
// IsZero == true values
{"00000000000000000000000000", true},
{"prefix_00000000000000000000000000", true},
{"other_00000000000000000000000000", true},
// IsZero == false values
{"00000000000000000000000001", false},
{"prefix_00000000000000000000000001", false},
{"other_00000000000000000000000001", false},
}
for _, td := range testdata {
t.Run(td.input, func(t *testing.T) {
tid, err := typeid.FromString(td.input)
if err != nil {
t.Error(err)
}
if tid.IsZero() != td.output {
t.Errorf("TypeId.IsZero should be %v for id %s", td.output, td.input)
}
})
}
}
func TestInvalidPrefix(t *testing.T) {
testdata := []struct {
name string
input string
}{
{"caps", "PREFIX"}, // Would be valid in lowercase
{"numeric", "12323"},
{"symbols", "pre.fix"},
{"spaces", " "},
{"long", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"},
}
for _, td := range testdata {
t.Run(td.name, func(t *testing.T) {
_, err := typeid.WithPrefix(td.input)
if err == nil {
t.Errorf("Expected error for invalid prefix: %s", td.input)
}
})
}
}
func TestInvalidSuffix(t *testing.T) {
testdata := []struct {
name string
input string
}{
{"spaces", " "},
{"short", "01234"},
{"long", "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"},
{"caps", "00041061050R3GG28A1C60T3GF"}, // Would be valid in lowercase
{"hyphens", "00041061050-3gg28a1-60t3gf"},
{"crockford_ambiguous", "ooo41o61o5or3gg28a1c6ot3gi"}, // Would be valid if we followed Crocksford's substitution rules
{"symbols", "00041061050.3gg28a1_60t3gf"},
{"wrong_alphabet", "ooooooiiiiiiuuuuuuulllllll"},
}
for _, td := range testdata {
t.Run(td.name, func(t *testing.T) {
_, err := typeid.From("prefix", td.input)
if err == nil {
t.Errorf("Expected error for invalid suffix: %s", td.input)
}
})
}
}
//go:embed testdata/invalid.yml
var invalidYML []byte
type InvalidExample struct {
Name string `yaml:"name"`
Tid string `yaml:"typeid"`
}
func TestInvalidTestdata(t *testing.T) {
assert.Greater(t, len(invalidYML), 0)
var testdata []InvalidExample
err := yaml.Unmarshal(invalidYML, &testdata)
if err != nil {
t.Errorf("Failed to unmarshal testdata: %s", err)
}
assert.Greater(t, len(testdata), 0)
for _, td := range testdata {
t.Run(td.Name, func(t *testing.T) {
_, err := typeid.FromString(td.Tid)
if err == nil {
t.Errorf("Expected error for invalid typeid: %s", td.Tid)
}
})
}
}
func TestEncodeDecode(t *testing.T) {
// Generate a bunch of random typeids, encode and decode from a string
// and make sure the result is the same as the original.
for i := 0; i < 1000; i++ {
tid := typeid.Must(typeid.WithPrefix("prefix"))
decoded, err := typeid.FromString(tid.String())
if err != nil {
t.Error(err)
}
if tid != decoded {
t.Errorf("Expected %s, got %s", tid, decoded)
}
}
// Repeat with the empty prefix:
for i := 0; i < 1000; i++ {
tid := typeid.Must(typeid.WithPrefix(""))
decoded, err := typeid.FromString(tid.String())
if err != nil {
t.Error(err)
}
if tid != decoded {
t.Errorf("Expected %s, got %s", tid, decoded)
}
}
}
func TestSpecialValues(t *testing.T) {
testdata := []struct {
name string
tid string
uuid string
}{
{"nil", "00000000000000000000000000", "00000000-0000-0000-0000-000000000000"},
{"one", "00000000000000000000000001", "00000000-0000-0000-0000-000000000001"},
{"ten", "0000000000000000000000000a", "00000000-0000-0000-0000-00000000000a"},
{"sixteen", "0000000000000000000000000g", "00000000-0000-0000-0000-000000000010"},
{"thirty-two", "00000000000000000000000010", "00000000-0000-0000-0000-000000000020"},
}
for _, data := range testdata {
t.Run(data.name, func(t *testing.T) {
// Values should be equal if we start by parsing the typeid
tid := typeid.Must(typeid.FromString(data.tid))
if data.uuid != tid.UUID() {
t.Errorf("Expected %s, got %s", data.uuid, tid.UUID())
}
// Values should be equal if we start by parsing the uuid
tid = typeid.Must(typeid.FromUUIDWithPrefix("", data.uuid))
if data.tid != tid.String() {
t.Errorf("Expected %s, got %s", data.tid, tid.String())
}
})
}
}
//go:embed testdata/valid.yml
var validYML []byte
type ValidExample struct {
Name string `yaml:"name"`
Tid string `yaml:"typeid"`
Prefix string `yaml:"prefix"`
UUID string `yaml:"uuid"`
}
func TestValidTestdata(t *testing.T) {
assert.Greater(t, len(validYML), 0)
var testdata []ValidExample
err := yaml.Unmarshal(validYML, &testdata)
if err != nil {
t.Errorf("Failed to unmarshal testdata: %s", err)
}
assert.Greater(t, len(testdata), 0)
for _, td := range testdata {
t.Run(td.Name, func(t *testing.T) {
tid := typeid.Must(typeid.FromString(td.Tid))
if td.UUID != tid.UUID() {
t.Errorf("Expected %s, got %s", td.UUID, tid.UUID())
}
if td.Prefix != tid.Prefix() {
t.Errorf("Expected %s, got %s", td.Prefix, tid.Prefix())
}
})
}
}
func TestConstructorError(t *testing.T) {
_, err := typeid.FromUUID[typeid.AnyID]("00000000-0000-0000-0000-000000000000")
assert.Error(t, err)
assert.True(t, errors.Is(err, typeid.ErrConstructor))
_, err = typeid.FromUUIDBytes[typeid.AnyID]([]byte{0, 0, 0, 0, 0, 0, 0, 0})
assert.Error(t, err)
assert.True(t, errors.Is(err, typeid.ErrConstructor))
}