forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
147 lines (136 loc) · 3.73 KB
/
util_test.go
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
package mysql
import (
"encoding/binary"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCompareServerVersions(t *testing.T) {
tests := []struct {
A string
B string
Expect int
}{
{A: "1.2.3", B: "1.2.3", Expect: 0},
{A: "5.6-999", B: "8.0", Expect: -1},
{A: "8.0.32-0ubuntu0.20.04.2", B: "8.0.28", Expect: 1},
}
for _, test := range tests {
got, err := CompareServerVersions(test.A, test.B)
require.NoError(t, err)
require.Equal(t, test.Expect, got)
}
}
func TestFormatBinaryTime(t *testing.T) {
tests := []struct {
Data []byte
Expect string
Error bool
}{
{Data: []byte{}, Expect: "00:00:00"},
{Data: []byte{0, 0, 0, 0, 0, 0, 0, 10}, Expect: "00:00:10"},
{Data: []byte{0, 0, 0, 0, 0, 0, 1, 40}, Expect: "00:01:40"},
{Data: []byte{1, 0, 0, 0, 0, 0, 1, 40}, Expect: "-00:01:40"},
{Data: []byte{1, 1, 0, 0, 0, 1, 1, 40}, Expect: "-25:01:40"},
{Data: []byte{1, 1, 0, 0, 0, 1, 1, 40, 1, 2, 3, 0}, Expect: "-25:01:40.197121"},
{Data: []byte{0}, Error: true},
}
for _, test := range tests {
n := len(test.Data)
got, err := FormatBinaryTime(n, test.Data)
if test.Error {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, test.Expect, string(got), "test case %v", test.Data)
}
}
// mysql driver parse binary datetime
func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (time.Time, error) {
switch num {
case 0:
return time.Time{}, nil
case 4:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
0, 0, 0, 0,
loc,
), nil
case 7:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
int(data[4]), // hour
int(data[5]), // minutes
int(data[6]), // seconds
0,
loc,
), nil
case 11:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
int(data[4]), // hour
int(data[5]), // minutes
int(data[6]), // seconds
int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
loc,
), nil
}
return time.Time{}, fmt.Errorf("invalid DATETIME packet length %d", num)
}
func TestToBinaryDateTime(t *testing.T) {
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "Zero time",
input: time.Time{},
expected: "",
},
{
name: "Date with nanoseconds",
input: time.Date(2023, 10, 10, 10, 10, 10, 123456000, time.UTC),
expected: "2023-10-10 10:10:10.123456 +0000 UTC",
},
{
name: "Date with time",
input: time.Date(2023, 10, 10, 10, 10, 10, 0, time.UTC),
expected: "2023-10-10 10:10:10 +0000 UTC",
},
{
name: "Date only",
input: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC),
expected: "2023-10-10 00:00:00 +0000 UTC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toBinaryDateTime(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
return
}
num := uint64(result[0])
data := result[1:]
date, err := parseBinaryDateTime(num, data, time.UTC)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.EqualFold(date.String(), tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}