-
Notifications
You must be signed in to change notification settings - Fork 1
/
ksuid_suite_test.go
263 lines (211 loc) · 6.34 KB
/
ksuid_suite_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
package uksuid
import (
"log"
"testing"
"time"
"github.com/segmentio/ksuid"
"github.com/gocql/gocql"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestKSUID(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "KSUID Suite")
}
// ksuidType is a mock gocql-type for testing CQL marshalling/Unmarshalling
type ksuidType struct{}
func (k ksuidType) Type() gocql.Type {
return gocql.TypeCustom
}
func (k ksuidType) Version() byte {
return 1
}
func (k ksuidType) Custom() string {
return "custom"
}
func (k ksuidType) New() interface{} {
return ksuidType{}
}
var _ = Describe("KSUID", func() {
// Taken from: https://github.com/segmentio/ksuid/blob/master/ksuid.go
// KSUID's epoch starts more recently so that the 32-bit number space gives a
// significantly higher useful lifetime of around 136 years from March 2017.
// This number (14e8) was picked to be easy to remember.
var epochStamp uint32 = 1400000000
Context("new KSUID is requested", func() {
It("should return new KSUID", func() {
u, err := New()
Expect(err).ToNot(HaveOccurred())
uid := u.String()
Expect(uid).ToNot(BeEmpty())
_, err = ksuid.Parse(uid)
Expect(err).ToNot(HaveOccurred())
})
})
Context("new KSUID WithTime is requested", func() {
It("should return new KSUID", func() {
uidTime := time.Now().AddDate(0, -2, -5)
u, err := NewWithTime(uidTime)
Expect(err).ToNot(HaveOccurred())
log.Println(u.Timestamp())
uid := u.String()
Expect(uid).ToNot(BeEmpty())
kuid, err := ksuid.Parse(uid)
Expect(err).ToNot(HaveOccurred())
t := int64(kuid.Timestamp() + epochStamp)
Expect(t).To(Equal(uidTime.Unix()))
})
})
Describe("Marshalling", func() {
It("should marshal to CQL when time is not specified", func() {
uid, err := New()
Expect(err).ToNot(HaveOccurred())
marshalled, err := gocql.Marshal(ksuidType{}, uid)
Expect(err).ToNot(HaveOccurred())
newKSUID, err := ksuid.FromBytes(marshalled)
Expect(err).ToNot(HaveOccurred())
Expect(newKSUID.String()).To(Equal(uid.String()))
})
It("should marshal to CQL when time is specified", func() {
uidTime := time.Now().AddDate(0, -9, 4)
uid, err := NewWithTime(uidTime)
Expect(err).ToNot(HaveOccurred())
marshalled, err := gocql.Marshal(ksuidType{}, uid)
Expect(err).ToNot(HaveOccurred())
newKSUID, err := ksuid.FromBytes(marshalled)
Expect(err).ToNot(HaveOccurred())
Expect(newKSUID.String()).To(Equal(uid.String()))
t := int64(newKSUID.Timestamp() + epochStamp)
Expect(t).To(Equal(uidTime.Unix()))
})
})
Describe("Unmarshalling", func() {
It("should unmarshal to CQL when time is not specified", func() {
uid, err := New()
Expect(err).ToNot(HaveOccurred())
uidBytes := uid.Bytes()
unmarshal := KSUID{}
err = gocql.Unmarshal(ksuidType{}, uidBytes, &unmarshal)
Expect(err).ToNot(HaveOccurred())
Expect(unmarshal.String()).To(Equal(uid.String()))
})
It("should unmarshal to CQL when time not specified", func() {
uidTime := time.Now().AddDate(0, 10, 4)
uid, err := NewWithTime(uidTime)
Expect(err).ToNot(HaveOccurred())
uidBytes := uid.Bytes()
unmarshal := KSUID{}
err = gocql.Unmarshal(ksuidType{}, uidBytes, &unmarshal)
Expect(err).ToNot(HaveOccurred())
Expect(unmarshal.String()).To(Equal(uid.String()))
Expect(unmarshal.Time().UnixNano()).To(Equal(uid.Time().UnixNano()))
})
})
Describe("Timestamp", func() {
It("should return corrected timestamp", func() {
t := time.Now()
ks, err := NewWithTime(t)
Expect(err).ToNot(HaveOccurred())
kt := int64(ks.Timestamp())
Expect(kt).To(Equal(t.Unix()))
})
})
Describe("TimestampUncorrected", func() {
It("should return uncorrected timestamp", func() {
t := time.Now()
ks, err := NewWithTime(t)
Expect(err).ToNot(HaveOccurred())
kt := int64(ks.TimestampUncorrected())
ut := t.Unix() - int64(epochStamp)
Expect(kt).To(Equal(ut))
})
})
Context("Sort operation is called", func() {
It("should sort provided values", func() {
ks := []KSUID{}
for i := 0; i < 10; i++ {
k, err := New()
Expect(err).ToNot(HaveOccurred())
ks = append(ks, k)
}
Expect(IsSorted(ks)).To(BeFalse())
sorted := Sort(ks)
Expect(IsSorted(sorted)).To(BeTrue())
})
})
Context("parsing to KSUID", func() {
Describe("FromBytes", func() {
It("should parse bytes to KSUID", func() {
u, err := New()
Expect(err).ToNot(HaveOccurred())
bytes := u.Bytes()
uid, err := FromBytes(bytes)
Expect(err).ToNot(HaveOccurred())
_, err = ksuid.FromBytes(uid.Bytes())
Expect(err).ToNot(HaveOccurred())
})
It("should return any errors that occur", func() {
invalidKSUID := "invalid"
_, err := FromBytes([]byte(invalidKSUID))
Expect(err).To(HaveOccurred())
})
})
Describe("FromString", func() {
It("should parse string to KSUID", func() {
u, err := New()
Expect(err).ToNot(HaveOccurred())
str := u.String()
uid, err := FromString(str)
Expect(uid).ToNot(BeNil())
_, err = ksuid.Parse(uid.String())
Expect(err).ToNot(HaveOccurred())
})
It("should return any errors that occur", func() {
invalidKSUID := "invalid"
_, err := FromString(invalidKSUID)
Expect(err).To(HaveOccurred())
})
})
Describe("FromParts", func() {
It("should parse string to KSUID", func() {
uidTime := time.Now().AddDate(0, 10, 4)
// 16 bytes
payload := []byte{
229, 69, 95, 119, 215, 29, 196, 255, 186, 191, 89, 24, 30, 239, 132, 38,
}
uid, err := FromParts(uidTime, payload)
Expect(err).ToNot(HaveOccurred())
Expect(uid.Payload()).To(Equal(payload))
// Expect(uid.Time)
})
})
Describe("ToWrappedKSUID", func() {
It("should convert provided KSUID to ksuid.KSUID", func() {
ks := []KSUID{}
for i := 0; i < 10; i++ {
k, err := New()
Expect(err).ToNot(HaveOccurred())
ks = append(ks, k)
}
ksids := ToWrappedKSUID(ks...)
for i, ksid := range ksids {
Expect(ksid).To(Equal(ks[i].KSUID))
}
})
})
Describe("ToWrapperKSUID", func() {
It("should convert provided ksuid.KSUID to KSUID", func() {
ks := []ksuid.KSUID{}
for i := 0; i < 10; i++ {
k, err := ksuid.NewRandom()
Expect(err).ToNot(HaveOccurred())
ks = append(ks, k)
}
ids := ToWrapperKSUID(ks...)
for i, id := range ids {
Expect(id.KSUID).To(Equal(ks[i]))
}
})
})
})
})