-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathversion7_test.go
65 lines (58 loc) · 2.09 KB
/
version7_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
package uuid
import (
"testing"
"time"
)
func TestNewV7WithTime(t *testing.T) {
testCases := map[string]string{
"test with current date": time.Now().Format(time.RFC3339), // now
"test with past date": time.Now().Add(-1 * time.Hour * 24 * 365).Format(time.RFC3339), // 1 year ago
"test with future date": time.Now().Add(time.Hour * 24 * 365).Format(time.RFC3339), // 1 year from now
"test with different timezone": "2021-09-01T12:00:00+04:00",
"test with negative timezone": "2021-09-01T12:00:00-12:00",
"test with future date in different timezone": "2124-09-23T12:43:30+09:00",
}
for testName, inputTime := range testCases {
t.Run(testName, func(t *testing.T) {
customTime, err := time.Parse(time.RFC3339, inputTime)
if err != nil {
t.Errorf("time.Parse returned unexpected error %v", err)
}
id, err := NewV7WithTime(&customTime)
if err != nil {
t.Errorf("NewV7WithTime returned unexpected error %v", err)
}
if id.Version() != 7 {
t.Errorf("got %d, want version 7", id.Version())
}
unixTime := time.Unix(id.Time().UnixTime())
// Compare the times in UTC format, since the input time might have different timezone,
// and the result is always in system timezone
if customTime.UTC().Format(time.RFC3339) != unixTime.UTC().Format(time.RFC3339) {
t.Errorf("got %s, want %s", unixTime.Format(time.RFC3339), customTime.Format(time.RFC3339))
}
})
}
}
func TestNewV7FromTimeGeneratesUniqueUUIDs(t *testing.T) {
now := time.Now()
ids := make([]string, 0)
runs := 26000
for i := 0; i < runs; i++ {
id, err := NewV7WithTime(&now)
if err != nil {
t.Errorf("NewV7WithTime returned unexpected error %v", err)
}
if id.Version() != 7 {
t.Errorf("got %d, want version 7", id.Version())
}
// Make sure we add only unique values
if !contains(t, ids, id.String()) {
ids = append(ids, id.String())
}
}
// Check we added all the UIDs
if len(ids) != runs {
t.Errorf("got %d UUIDs, want %d", len(ids), runs)
}
}