Skip to content

Commit f0ca5b7

Browse files
committed
fix: update TestToBinaryDateTime
1 parent c0cd0ab commit f0ca5b7

File tree

2 files changed

+45
-74
lines changed

2 files changed

+45
-74
lines changed

mysql/resultset_helper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mysql
33
import (
44
"bytes"
55
"encoding/binary"
6+
"fmt"
67
"math"
78
"strconv"
89
"time"
@@ -43,7 +44,7 @@ func FormatTextValue(value interface{}) ([]byte, error) {
4344
case string:
4445
return utils.StringToByteSlice(v), nil
4546
case time.Time:
46-
return hack.Slice(v.Format(time.DateTime)), nil
47+
return utils.StringToByteSlice(v.Format(time.DateTime)), nil
4748
case nil:
4849
return nil, nil
4950
default:
@@ -55,7 +56,7 @@ func toBinaryDateTime(t time.Time) ([]byte, error) {
5556
var buf bytes.Buffer
5657

5758
if t.IsZero() {
58-
return nil, nil
59+
return nil, fmt.Errorf("zero time")
5960
}
6061

6162
year, month, day := t.Year(), t.Month(), t.Day()

mysql/util_test.go

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package mysql
22

33
import (
4-
"encoding/binary"
5-
"fmt"
6-
"strings"
74
"testing"
85
"time"
96

@@ -56,92 +53,65 @@ func TestFormatBinaryTime(t *testing.T) {
5653
}
5754
}
5855

59-
// mysql driver parse binary datetime
60-
func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (time.Time, error) {
61-
switch num {
62-
case 0:
63-
return time.Time{}, nil
64-
case 4:
65-
return time.Date(
66-
int(binary.LittleEndian.Uint16(data[:2])), // year
67-
time.Month(data[2]), // month
68-
int(data[3]), // day
69-
0, 0, 0, 0,
70-
loc,
71-
), nil
72-
case 7:
73-
return time.Date(
74-
int(binary.LittleEndian.Uint16(data[:2])), // year
75-
time.Month(data[2]), // month
76-
int(data[3]), // day
77-
int(data[4]), // hour
78-
int(data[5]), // minutes
79-
int(data[6]), // seconds
80-
0,
81-
loc,
82-
), nil
83-
case 11:
84-
return time.Date(
85-
int(binary.LittleEndian.Uint16(data[:2])), // year
86-
time.Month(data[2]), // month
87-
int(data[3]), // day
88-
int(data[4]), // hour
89-
int(data[5]), // minutes
90-
int(data[6]), // seconds
91-
int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
92-
loc,
93-
), nil
94-
}
95-
return time.Time{}, fmt.Errorf("invalid DATETIME packet length %d", num)
96-
}
97-
9856
func TestToBinaryDateTime(t *testing.T) {
57+
var (
58+
DateTimeNano = "2006-01-02 15:04:05.000000"
59+
formatBinaryDateTime = func(n int, data []byte) string {
60+
date, err := FormatBinaryDateTime(n, data)
61+
if err != nil {
62+
return ""
63+
}
64+
return string(date)
65+
}
66+
)
67+
9968
tests := []struct {
100-
name string
101-
input time.Time
102-
expected string
69+
Name string
70+
Data time.Time
71+
Expect func(n int, data []byte) string
72+
Error bool
10373
}{
10474
{
105-
name: "Zero time",
106-
input: time.Time{},
107-
expected: "",
75+
Name: "Zero time",
76+
Data: time.Time{},
77+
Expect: nil,
78+
Error: true,
10879
},
10980
{
110-
name: "Date with nanoseconds",
111-
input: time.Date(2023, 10, 10, 10, 10, 10, 123456000, time.UTC),
112-
expected: "2023-10-10 10:10:10.123456 +0000 UTC",
81+
Name: "Date with nanoseconds",
82+
Data: time.Date(2023, 10, 10, 10, 10, 10, 123456000, time.UTC),
83+
Expect: formatBinaryDateTime,
11384
},
11485
{
115-
name: "Date with time",
116-
input: time.Date(2023, 10, 10, 10, 10, 10, 0, time.UTC),
117-
expected: "2023-10-10 10:10:10 +0000 UTC",
86+
Name: "Date with time",
87+
Data: time.Date(2023, 10, 10, 10, 10, 10, 0, time.UTC),
88+
Expect: formatBinaryDateTime,
11889
},
11990
{
120-
name: "Date only",
121-
input: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC),
122-
expected: "2023-10-10 00:00:00 +0000 UTC",
91+
Name: "Date only",
92+
Data: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC),
93+
Expect: formatBinaryDateTime,
12394
},
12495
}
12596

126-
for _, tt := range tests {
127-
t.Run(tt.name, func(t *testing.T) {
128-
result, err := toBinaryDateTime(tt.input)
129-
if err != nil {
130-
t.Fatalf("unexpected error: %v", err)
97+
for _, test := range tests {
98+
t.Run(test.Name, func(t *testing.T) {
99+
got, err := toBinaryDateTime(test.Data)
100+
if test.Error {
101+
require.Error(t, err)
102+
} else {
103+
require.NoError(t, err)
131104
}
132-
if len(result) == 0 {
105+
if len(got) == 0 {
133106
return
134107
}
135-
num := uint64(result[0])
136-
data := result[1:]
137-
date, err := parseBinaryDateTime(num, data, time.UTC)
138-
if err != nil {
139-
t.Fatalf("unexpected error: %v", err)
140-
}
141-
142-
if !strings.EqualFold(date.String(), tt.expected) {
143-
t.Errorf("expected %v, got %v", tt.expected, result)
108+
tmp := test.Expect(int(got[0]), got[1:])
109+
if int(got[0]) < 11 {
110+
require.Equal(t, tmp, test.Data.Format(time.DateTime), "test case %v", test.Data.String())
111+
} else {
112+
require.Equal(t, tmp, test.Data.Format(DateTimeNano), "test case %v", test.Data.String())
144113
}
145114
})
146115
}
116+
147117
}

0 commit comments

Comments
 (0)