-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtypes_test.go
More file actions
168 lines (138 loc) · 4.93 KB
/
types_test.go
File metadata and controls
168 lines (138 loc) · 4.93 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
161
162
163
164
165
166
167
168
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package sql
import (
"testing"
"github.com/openark/golib/log"
"github.com/stretchr/testify/require"
)
func init() {
log.SetLevel(log.ERROR)
}
func TestParseColumnList(t *testing.T) {
names := "id,category,max_len"
columnList := ParseColumnList(names)
require.Equal(t, 3, columnList.Len())
require.Equal(t, []string{"id", "category", "max_len"}, columnList.Names())
require.Equal(t, 0, columnList.Ordinals["id"])
require.Equal(t, 1, columnList.Ordinals["category"])
require.Equal(t, 2, columnList.Ordinals["max_len"])
}
func TestGetColumn(t *testing.T) {
names := "id,category,max_len"
columnList := ParseColumnList(names)
{
column := columnList.GetColumn("category")
require.NotNil(t, column)
require.Equal(t, column.Name, "category")
}
{
column := columnList.GetColumn("no_such_column")
require.Nil(t, column)
}
}
func TestBinaryToString(t *testing.T) {
id := []uint8{0x1b, 0x99}
col := make([]interface{}, 1)
col[0] = id
cv := ToColumnValues(col)
require.Equal(t, "1b99", cv.StringColumn(0))
}
func TestConvertArgCharsetDecoding(t *testing.T) {
latin1Bytes := []uint8{0x47, 0x61, 0x72, 0xe7, 0x6f, 0x6e, 0x20, 0x21}
col := Column{
Charset: "latin1",
charsetConversion: &CharacterSetConversion{
FromCharset: "latin1",
ToCharset: "utf8mb4",
},
}
// Should decode []uint8
str := col.convertArg(latin1Bytes)
require.Equal(t, "Garçon !", str)
}
func TestConvertArgBinaryColumnPadding(t *testing.T) {
// Test that binary columns are padded with trailing zeros to their declared length.
// This is needed because MySQL's binlog strips trailing 0x00 bytes from binary values.
// See https://github.com/github/gh-ost/issues/909
// Simulates a binary(20) column where binlog delivered only 18 bytes
// (trailing zeros were stripped)
truncatedValue := []uint8{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, // 18 bytes, missing 2 trailing zeros
}
col := Column{
Name: "bin_col",
Type: BinaryColumnType,
BinaryOctetLength: 20,
}
result := col.convertArg(truncatedValue)
resultBytes := result.([]byte)
require.Equal(t, 20, len(resultBytes), "binary column should be padded to declared length")
// First 18 bytes should be unchanged
require.Equal(t, truncatedValue, resultBytes[:18])
// Last 2 bytes should be zeros
require.Equal(t, []byte{0x00, 0x00}, resultBytes[18:])
}
func TestConvertArgVarbinaryStringWithInvalidUTF8Bytes(t *testing.T) {
// go-mysql returns varbinary binlog row values as Go `string` (not `[]uint8`).
// When convertArg receives a string for a column with no Charset (varbinary),
// it must return []byte — not the original string. The Go MySQL driver sends
// string args as MYSQL_TYPE_VAR_STRING with utf8mb4 charset metadata, which
// causes MySQL to validate the bytes and emit Warning 1300 for invalid sequences.
// gh-ost's panic-on-warnings then turns that warning into a fatal migration error.
// See: uuid varbinary(16) rows whose binary UUID bytes happen to be invalid utf8mb4.
rawBytes := []byte{0x91, 0xC3, 0xCD, 0x00, 0x01, 0x02}
col := Column{
Name: "uuid",
Charset: "", // varbinary has no character set
MySQLType: "varbinary(16)", // set by inspect.go from information_schema COLUMN_TYPE
}
result := col.convertArg(string(rawBytes))
require.IsType(t, []byte{}, result,
"varbinary value from binlog (Go string) must be returned as []byte, not string, "+
"to prevent MySQL driver from sending it with the connection's charset metadata")
require.Equal(t, rawBytes, result.([]byte))
}
func TestConvertArgVarbinaryBytesWithInvalidUTF8Bytes(t *testing.T) {
// When go-mysql returns varbinary values as []uint8 (rather than string),
// convertArg should also return []byte consistently.
rawBytes := []uint8{0x91, 0xC3, 0xCD, 0x00, 0x01, 0x02}
col := Column{
Name: "uuid",
Charset: "",
MySQLType: "varbinary(16)", // set by inspect.go from information_schema COLUMN_TYPE
}
result := col.convertArg(rawBytes)
require.IsType(t, []byte{}, result)
require.Equal(t, rawBytes, result.([]byte))
}
func TestConvertArgBinaryColumnNoPaddingWhenFull(t *testing.T) {
// When binary value is already at full length, no padding should occur
fullValue := []uint8{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, // 20 bytes
}
col := Column{
Name: "bin_col",
Type: BinaryColumnType,
BinaryOctetLength: 20,
}
result := col.convertArg(fullValue)
resultBytes := result.([]byte)
require.Equal(t, 20, len(resultBytes))
require.Equal(t, fullValue, resultBytes)
}
func TestConvertArgBitColumn(t *testing.T) {
b := []uint8{0x00, 0x00, 0xa3}
col := Column{
Name: "bit_col",
Type: BitColumnType,
}
result := col.convertArg(b)
require.Equal(t, uint64(163), result)
}