-
Notifications
You must be signed in to change notification settings - Fork 3
/
id_test.go
210 lines (186 loc) · 3.79 KB
/
id_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 idgen_test
import (
"math/rand"
"net/url"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/oklog/ulid"
"github.com/appointy/idgen"
)
func TestNew(t *testing.T) {
prefix := "cus"
testNew(t, idgen.New(prefix), prefix)
}
func TestULID(t *testing.T) {
id := idgen.New("cus")
if _, err := ulid.Parse(id[len("cus_"):]); err != nil {
t.Errorf("func New not generating ulids: %v", err)
}
}
func TestURLSafe(t *testing.T) {
for range [20]struct{}{} {
id := idgen.New("cus")
if url.PathEscape(id) != id {
t.Errorf("value generated by New is not url path safe, expected: %s, got: %s", url.PathEscape(id), id)
}
if url.QueryEscape(id) != id {
t.Errorf("value generated by New is not url query safe, expected: %s, got: %s", url.QueryEscape(id), id)
}
}
}
func TestUniqueness(t *testing.T) {
set := map[string]bool{}
for range [10000]struct{}{} {
id := idgen.New("cus")
if set[id] {
t.Errorf("generating repeated strings")
}
set[id] = true
}
}
func TestUniquenessParallel(t *testing.T) {
var wg sync.WaitGroup
ids := make(chan string, 1000)
for i := 0; i < 100; i++ {
wg.Add(1)
go func(ids chan<- string) {
defer wg.Done()
for i := 0; i < 100; i++ {
ids <- idgen.New("usr")
}
}(ids)
}
go func() {
wg.Wait()
close(ids)
}()
set := map[string]bool{}
for id := range ids {
if set[id] {
t.Errorf("generating repeated strings")
}
set[id] = true
}
}
func TestLexicalOrder(t *testing.T) {
var ii [1000]string
for k := range ii {
ii[k] = idgen.New("cus")
}
if !sort.StringsAreSorted(ii[:]) {
t.Errorf("should generate lexically sorted ids")
for _, v := range ii {
t.Log(v)
}
}
}
func TestGenerator_New(t *testing.T) {
const prefix = "cus"
pg := idgen.Generator{
Prefix: prefix,
}
testNew(t, pg.New(), prefix)
}
func TestPrefix(t *testing.T) {
tests := []struct {
name string
id string
want string
}{
{
name: "Empty",
id: "",
want: "",
},
{
name: "Good",
id: idgen.New("cus"),
want: "cus",
},
{
name: "ulid",
id: idgen.New("c")[2:],
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := idgen.Prefix(tt.id); got != tt.want {
t.Errorf("Prefix() = %v, want %v", got, tt.want)
}
})
}
}
func TestTime(t *testing.T) {
tests := []struct {
name string
id string
want uint64
wantErr error
}{
{
name: "Empty",
id: "",
want: 0,
wantErr: idgen.ErrMalformed,
},
{
name: "Valid",
id: "cus_" + ulid.MustNew(ulid.MaxTime(), rand.New(rand.NewSource(time.Now().UnixNano()))).String(),
want: ulid.MaxTime(),
wantErr: nil,
},
{
name: "Valid Without Prefix",
id: ulid.MustNew(ulid.MaxTime(), rand.New(rand.NewSource(time.Now().UnixNano()))).String(),
want: ulid.MaxTime(),
wantErr: nil,
},
{
name: "Malformed",
id: "cus_" + uuid.Must(uuid.NewUUID()).String(),
wantErr: idgen.ErrMalformed,
want: 0,
},
{
name: "Just Prefix",
id: "cus_",
wantErr: idgen.ErrMalformed,
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := idgen.Time(tt.id)
if err != tt.wantErr {
t.Errorf("Time() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Time() = %v, want %v", got, tt.want)
}
})
}
}
func testNew(t *testing.T, id, prefix string) {
t.Helper()
if !strings.HasPrefix(id, prefix+"_") {
t.Errorf("New should return a ulid with the given prefix, expected: cus, got: %s", id)
}
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = idgen.New("cus")
}
}
func BenchmarkNewParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = idgen.New("cus")
}
})
}