|
1 | 1 | package mysql |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/binary" |
5 | | - "fmt" |
6 | | - "strings" |
7 | 4 | "testing" |
8 | 5 | "time" |
9 | 6 |
|
@@ -56,92 +53,65 @@ func TestFormatBinaryTime(t *testing.T) { |
56 | 53 | } |
57 | 54 | } |
58 | 55 |
|
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 | | - |
98 | 56 | 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 | + |
99 | 68 | 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 |
103 | 73 | }{ |
104 | 74 | { |
105 | | - name: "Zero time", |
106 | | - input: time.Time{}, |
107 | | - expected: "", |
| 75 | + Name: "Zero time", |
| 76 | + Data: time.Time{}, |
| 77 | + Expect: nil, |
| 78 | + Error: true, |
108 | 79 | }, |
109 | 80 | { |
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, |
113 | 84 | }, |
114 | 85 | { |
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, |
118 | 89 | }, |
119 | 90 | { |
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, |
123 | 94 | }, |
124 | 95 | } |
125 | 96 |
|
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) |
131 | 104 | } |
132 | | - if len(result) == 0 { |
| 105 | + if len(got) == 0 { |
133 | 106 | return |
134 | 107 | } |
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()) |
144 | 113 | } |
145 | 114 | }) |
146 | 115 | } |
| 116 | + |
147 | 117 | } |
0 commit comments