This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerange_test.go
More file actions
129 lines (113 loc) · 2.74 KB
/
timerange_test.go
File metadata and controls
129 lines (113 loc) · 2.74 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
package typeid_test
import (
"testing"
"time"
"github.com/go-chi/typeid"
)
func TestUUID_GetTime(t *testing.T) {
id, err := typeid.NewUUID[userPrefix]()
if err != nil {
t.Fatal(err)
}
got := id.GetTime()
if got.IsZero() {
t.Fatal("GetTime() returned zero")
}
if time.Since(got) > time.Second {
t.Fatalf("GetTime() = %v, too far from now", got)
}
}
func TestInt64_GetTime(t *testing.T) {
id, err := typeid.NewInt64[orgPrefix]()
if err != nil {
t.Fatal(err)
}
got := id.GetTime()
if got.IsZero() {
t.Fatal("GetTime() returned zero")
}
if time.Since(got) > time.Second {
t.Fatalf("GetTime() = %v, too far from now", got)
}
}
func TestTimeRange_ToSql_BothSet(t *testing.T) {
since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC)
r := typeid.UUIDRange[userPrefix]("id", &since, &until)
sql, args, err := r.ToSql()
if err != nil {
t.Fatal(err)
}
if sql != "id BETWEEN ? AND ?" {
t.Fatalf("sql = %q", sql)
}
if len(args) != 2 {
t.Fatalf("args len = %d, want 2", len(args))
}
}
func TestTimeRange_ToSql_SinceOnly(t *testing.T) {
since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
r := typeid.UUIDRange[userPrefix]("id", &since, nil)
sql, args, err := r.ToSql()
if err != nil {
t.Fatal(err)
}
if sql != "id >= ?" {
t.Fatalf("sql = %q", sql)
}
if len(args) != 1 {
t.Fatalf("args len = %d, want 1", len(args))
}
}
func TestTimeRange_ToSql_UntilOnly(t *testing.T) {
until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC)
r := typeid.UUIDRange[userPrefix]("id", nil, &until)
sql, args, err := r.ToSql()
if err != nil {
t.Fatal(err)
}
if sql != "id <= ?" {
t.Fatalf("sql = %q", sql)
}
if len(args) != 1 {
t.Fatalf("args len = %d, want 1", len(args))
}
}
func TestTimeRange_ToSql_Neither(t *testing.T) {
r := typeid.UUIDRange[userPrefix]("id", nil, nil)
sql, args, err := r.ToSql()
if err != nil {
t.Fatal(err)
}
if sql != "1=1" {
t.Fatalf("sql = %q", sql)
}
if len(args) != 0 {
t.Fatalf("args len = %d, want 0", len(args))
}
}
func TestTimeRange_ToSql_Int64(t *testing.T) {
since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
until := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC)
r := typeid.Int64Range[orgPrefix]("id", &since, &until)
sql, args, err := r.ToSql()
if err != nil {
t.Fatal(err)
}
if sql != "id BETWEEN ? AND ?" {
t.Fatalf("sql = %q", sql)
}
if len(args) != 2 {
t.Fatalf("args len = %d, want 2", len(args))
}
}
func TestTimeRange_FloorCeil_Accessors(t *testing.T) {
since := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
r := typeid.UUIDRange[userPrefix]("id", &since, nil)
if _, ok := r.Floor(); !ok {
t.Fatal("Floor() returned false")
}
if _, ok := r.Ceil(); ok {
t.Fatal("Ceil() returned true for nil until")
}
}