-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_test.go
More file actions
152 lines (140 loc) · 3.44 KB
/
generator_test.go
File metadata and controls
152 lines (140 loc) · 3.44 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
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
// Copyright Chrono Technologies LLC
// SPDX-License-Identifier: MIT
package main
import (
"testing"
"time"
"github.com/google/uuid"
)
func TestGenerateUUIDV7WithCustomTime(t *testing.T) {
tests := []struct {
name string
input string
wantTime time.Time
wantErr bool
}{
{
name: "with invalid string",
input: "not-a-time",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 date string",
input: "1969-12-31",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 rfc3339 format",
input: "1969-12-31T23:59:59Z",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 iso8601 with offset",
input: "1969-12-31T18:59:59-05:00",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with just before unix epoch",
input: "1969-12-31T23:59:59.999Z",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with negative unix timestamp (seconds)",
input: "-1687689000",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with negative unix timestamp (milliseconds)",
input: "-1687689000123",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with unix epoch boundary",
input: "1970-01-01T00:00:00Z",
wantTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with only date",
input: "2023-06-15",
wantTime: time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with timezone-less datetime",
input: "2023-06-15 10:30:00",
wantTime: time.Date(2023, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with seconds-less datetime",
input: "2023-06-15 10:30",
wantTime: time.Date(2023, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with rfc3339 format",
input: "2025-06-15T10:30:00.123Z",
wantTime: time.Date(2025, 6, 15, 10, 30, 0, 123000000, time.UTC),
wantErr: false,
},
{
name: "with iso8601 format including timezone",
input: "2025-06-15T10:30:00Z",
wantTime: time.Date(2025, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with iso8601 format including offset",
input: "2023-06-15T10:30:00-07:00",
wantTime: time.Date(2023, 6, 15, 17, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with unix timestamp (seconds)",
input: "1687689000",
wantTime: time.Unix(1687689000, 0),
wantErr: false,
},
{
name: "with unix timestamp (milliseconds)",
input: "1687689000123",
wantTime: time.UnixMilli(1687689000123),
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := generateUUIDV7(test.input)
if test.wantErr {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if result.Version() != uuidV7 {
t.Fatalf("unexpected uuid version, got: %d", result.Version())
}
extractedTime := extractTimeFromUUIDV7(result)
if !extractedTime.Equal(test.wantTime) {
t.Errorf("Time mismatch: got %v, want %v", extractedTime, test.wantTime)
}
})
}
}
func extractTimeFromUUIDV7(u uuid.UUID) time.Time {
msec := int64(u[0])<<40 | int64(u[1])<<32 | int64(u[2])<<24 |
int64(u[3])<<16 | int64(u[4])<<8 | int64(u[5])
return time.UnixMilli(msec)
}