-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_test.go
More file actions
160 lines (136 loc) · 3.33 KB
/
time_test.go
File metadata and controls
160 lines (136 loc) · 3.33 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
153
154
155
156
157
158
159
160
package nullish
import (
"testing"
"time"
"github.com/goccy/go-json"
)
func TestNullTime_Value(t *testing.T) {
now := time.Now()
nt := NewNullTime(now, true)
got, err := nt.Value()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != now {
t.Errorf("expected %v, got %v", now, got)
}
// Test invalid
nt = NewNullTime(time.Time{}, false)
got, err = nt.Value()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != nil {
t.Errorf("expected nil, got %v", got)
}
}
func TestNullTime_Scan(t *testing.T) {
now := time.Now()
var nt NullTime
// Scan time.Time
err := nt.Scan(now)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !nt.Time.Equal(now) || !nt.Valid {
t.Errorf("expected Time=%v Valid=true, got Time=%v Valid=%v", now, nt.Time, nt.Valid)
}
// Scan nil
err = nt.Scan(nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if nt.Valid {
t.Error("expected Valid=false for nil")
}
// Scan invalid type
err = nt.Scan("not a time")
if err == nil {
t.Error("expected error for invalid type")
}
}
func TestNullTime_MarshalJSON(t *testing.T) {
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
nt := NewNullTime(now, true)
data, err := nt.MarshalJSON()
if err != nil {
t.Fatalf("marshal error: %v", err)
}
expected := `"` + now.Format(time.RFC3339Nano) + `"`
if string(data) != expected {
t.Errorf("expected %s, got %s", expected, string(data))
}
// Test invalid
nt = NewNullTime(time.Time{}, false)
data, err = nt.MarshalJSON()
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != "null" {
t.Errorf("expected null, got %s", string(data))
}
}
func TestNullTime_UnmarshalJSON(t *testing.T) {
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
jsonData := `"` + now.Format(time.RFC3339Nano) + `"`
var nt NullTime
err := nt.UnmarshalJSON([]byte(jsonData))
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !nt.Time.Equal(now) || !nt.Valid {
t.Errorf("expected Time=%v Valid=true, got Time=%v Valid=%v", now, nt.Time, nt.Valid)
}
// Test null
err = nt.UnmarshalJSON([]byte("null"))
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if nt.Valid {
t.Error("expected Valid=false for null")
}
}
func TestNullTime_RoundTrip(t *testing.T) {
now := time.Now()
nt := NewNullTime(now, true)
data, err := json.Marshal(nt)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var decoded NullTime
err = json.Unmarshal(data, &decoded)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
// Use Equal for time comparison (handles location/monotonic)
if !nt.Time.Equal(decoded.Time) || nt.Valid != decoded.Valid {
t.Errorf("roundtrip failed: expected %+v, got %+v", nt, decoded)
}
}
func BenchmarkNullTime_Value(b *testing.B) {
nt := NewNullTime(time.Now(), true)
for i := 0; i < b.N; i++ {
_, _ = nt.Value()
}
}
func BenchmarkNullTime_Scan(b *testing.B) {
now := time.Now()
for i := 0; i < b.N; i++ {
var nt NullTime
_ = nt.Scan(now)
}
}
func BenchmarkNullTime_MarshalJSON(b *testing.B) {
nt := NewNullTime(time.Now(), true)
for i := 0; i < b.N; i++ {
_, _ = nt.MarshalJSON()
}
}
func BenchmarkNullTime_UnmarshalJSON(b *testing.B) {
now := time.Now()
data := []byte(`"` + now.Format(time.RFC3339Nano) + `"`)
for i := 0; i < b.N; i++ {
var nt NullTime
_ = nt.UnmarshalJSON(data)
}
}