Repository navigation
Expand file tree
/
Copy pathv7_test.go
More file actions
88 lines (69 loc) · 2.03 KB
/
Copy pathv7_test.go
File metadata and controls
88 lines (69 loc) · 2.03 KB
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
package uuid_test
import (
"testing"
"time"
"github.com/DaanV2/go-uuid"
"github.com/stretchr/testify/require"
)
func Test_V7_Version(t *testing.T) {
require.Equal(t, uuid.Version7, uuid.V7.Version())
}
func Test_V7_Variant(t *testing.T) {
require.Equal(t, uuid.Variant1, uuid.V7.Variant())
}
func Test_V7_New(t *testing.T) {
u := uuid.V7.New()
Expect_Valid_UUID(t, u, uuid.V7.Version(), uuid.V7.Variant())
}
func Test_V7_NewHex(t *testing.T) {
u := uuid.V7.NewHex()
require.Len(t, u, 32)
require.NotRegexp(t, "[^a-zA-Z0-9]", u)
}
func Test_V7_NewString(t *testing.T) {
u := uuid.V7.NewString()
require.Len(t, u, 36)
require.NotRegexp(t, "[^a-zA-Z0-9-]", u)
err := uuid.ValidateStringUUID(u)
require.NoError(t, err)
}
func Test_V7_NewBatch(t *testing.T) {
sizes := []int{1, 2, 3, 10, 100}
for _, size := range sizes {
t.Run("NewBatch", func(t *testing.T) {
batch := uuid.V7.NewBatch(size)
require.Len(t, batch, size)
uni := make(map[string]bool)
for _, u := range batch {
Expect_Valid_UUID(t, u, uuid.V7.Version(), uuid.V7.Variant())
if has, ok := uni[u.String()]; ok {
require.False(t, has, "UUIDs should be unique")
} else {
uni[u.String()] = true
}
}
})
}
}
func Test_V7_Time_Ordering(t *testing.T) {
// Create UUIDs with known timestamps
now := time.Now().UTC()
u1 := uuid.V7.From(now)
u2 := uuid.V7.From(now.Add(time.Second))
// The string representation should be ordered
require.Less(t, u1.String(), u2.String(), "Later timestamp should produce greater UUID")
}
func Test_V7_Multiple_UUIDs_Are_Ordered(t *testing.T) {
// Generate multiple UUIDs with small delays
uuids := make([]uuid.UUID, 10)
for i := range 10 {
uuids[i] = uuid.V7.New()
time.Sleep(time.Millisecond) // Ensure different timestamps
}
// Verify UUIDs are ordered (later UUIDs should be greater than or equal to earlier ones)
for i := 1; i < len(uuids); i++ {
current := uuids[i].String()
previous := uuids[i-1].String()
require.GreaterOrEqual(t, current, previous, "UUIDs should be time-ordered")
}
}