forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
179 lines (159 loc) · 5.69 KB
/
parser_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dump
import (
"strings"
"testing"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/stretchr/testify/require"
)
// This tests the binlogExp regexp that matches the line that mysqldump adds when called with --master-data or --source-data
func TestBinlogExp(t *testing.T) {
stmts := []struct {
input string
file string
pos string
}{
{
// MySQL 9.1.0
`CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE='binlog.000002', SOURCE_LOG_POS=170923;`,
`binlog.000002`,
`170923`,
},
{
`CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.008995', MASTER_LOG_POS=102052485;`,
`mysql-bin.008995`,
`102052485`,
},
}
for _, stmt := range stmts {
m := binlogExp.FindAllStringSubmatch(stmt.input, -1)
require.NotNil(t, m)
require.Equal(t, stmt.file, m[0][3])
require.Equal(t, stmt.pos, m[0][5])
}
}
func TestParseGtidExp(t *testing.T) {
// binlogExp := regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
// gtidExp := regexp.MustCompile("(\\w{8}(-\\w{4}){3}-\\w{12}:\\d+-\\d+)")
tbls := []struct {
input string
expected string
}{
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1-76,
2337be48-0456-11e9-bd1c-00505690543b:1-7,
41d816cd-0455-11e9-be42-005056901a22:1-2,
5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,
75848cdb-8131-11e7-b6fc-1c1b0de85e7b:1-151378598,
780ad602-0456-11e9-8bcd-005056901a22:1-516653148,
92809ddd-1e3c-11e9-9d04-00505690f6ab:1-11858565,
c59598c7-0467-11e9-bbbe-005056901a22:1-226464969,
cbd7809d-0433-11e9-b1cf-00505690543b:1-18233950,
cca778e9-8cdf-11e8-94d0-005056a247b1:1-303899574,
cf80679b-7695-11e8-8873-1c1b0d9a4ab9:1-12836047,
d0951f24-1e21-11e9-bb2e-00505690b730:1-4758092,
e7574090-b123-11e8-8bb4-005056a29643:1-12'
`, "071a84e8-b253-11e8-8472-005056a27e86:1-76,2337be48-0456-11e9-bd1c-00505690543b:1-7,41d816cd-0455-11e9-be42-005056901a22:1-2,5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,75848cdb-8131-11e7-b6fc-1c1b0de85e7b:1-151378598,780ad602-0456-11e9-8bcd-005056901a22:1-516653148,92809ddd-1e3c-11e9-9d04-00505690f6ab:1-11858565,c59598c7-0467-11e9-bbbe-005056901a22:1-226464969,cbd7809d-0433-11e9-b1cf-00505690543b:1-18233950,cca778e9-8cdf-11e8-94d0-005056a247b1:1-303899574,cf80679b-7695-11e8-8873-1c1b0d9a4ab9:1-12836047,d0951f24-1e21-11e9-bb2e-00505690b730:1-4758092,e7574090-b123-11e8-8bb4-005056a29643:1-12"},
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1-76,
2337be48-0456-11e9-bd1c-00505690543b:1-7';
`, "071a84e8-b253-11e8-8472-005056a27e86:1-76,2337be48-0456-11e9-bd1c-00505690543b:1-7"},
{`SET @@GLOBAL.GTID_PURGED='c0977f88-3104-11e9-81e1-00505690245b:1-274559';
`, "c0977f88-3104-11e9-81e1-00505690245b:1-274559"},
{`CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.008995', MASTER_LOG_POS=102052485;`, ""},
{
`SET @@GLOBAL.GTID_PURGED='e50bd2d3-6ad7-11e9-890c-42010af0017c:1-5291126581:5291126583-5323107666';
`,
"e50bd2d3-6ad7-11e9-890c-42010af0017c:1-5291126581:5291126583-5323107666",
},
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1,
2337be48-0456-11e9-bd1c-00505690543b:1-7,
5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,
e7574090-b123-11e8-8bb4-005056a29643:1'
`, "071a84e8-b253-11e8-8472-005056a27e86:1,2337be48-0456-11e9-bd1c-00505690543b:1-7,5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,e7574090-b123-11e8-8bb4-005056a29643:1"},
}
for _, tt := range tbls {
reader := strings.NewReader(tt.input)
var handler = new(testParseHandler)
err := Parse(reader, handler, true)
require.NoError(t, err)
if tt.expected == "" {
if handler.gset != nil {
require.Nil(t, handler.gset)
} else {
continue
}
}
expectedGtidset, err := mysql.ParseGTIDSet("mysql", tt.expected)
require.NoError(t, err)
require.True(t, expectedGtidset.Equal(handler.gset))
}
}
func TestParseFindTable(t *testing.T) {
tbl := []struct {
sql string
table string
}{
{"INSERT INTO `note` VALUES ('title', 'here is sql: INSERT INTO `table` VALUES (\\'some value\\')');", "note"},
{"INSERT INTO `note` VALUES ('1', '2', '3');", "note"},
{"INSERT INTO `a.b` VALUES ('1');", "a.b"},
}
for _, te := range tbl {
res := valuesExp.FindAllStringSubmatch(te.sql, -1)[0][1]
require.Equal(t, te.table, res)
}
}
func TestUnescape(t *testing.T) {
tbl := []struct {
escaped string
expected string
}{
{`\\n`, `\n`},
{`\\t`, `\t`},
{`\\"`, `\"`},
{`\\'`, `\'`},
{`\\0`, `\0`},
{`\\b`, `\b`},
{`\\Z`, `\Z`},
{`\\r`, `\r`},
{`abc`, `abc`},
{`abc\`, `abc`},
{`ab\c`, `abc`},
{`\abc`, `abc`},
}
for _, te := range tbl {
unesacped := unescapeString(te.escaped)
require.Equal(t, te.expected, unesacped)
}
}
func TestParseValue(t *testing.T) {
str := `'abc\\',''`
values, err := parseValues(str)
require.NoError(t, err)
require.Equal(t, []string{`'abc\'`, `''`}, values)
str = `123,'\Z#÷QÎx£. Æ‘ÇoPâÅ_\r—\\','','qn'`
values, err = parseValues(str)
require.NoError(t, err)
require.Len(t, values, 4)
str = `123,'\Z#÷QÎx£. Æ‘ÇoPâÅ_\r—\\','','qn\'`
_, err = parseValues(str)
require.Error(t, err)
}
func TestParseLine(t *testing.T) {
lines := []struct {
line string
expected string
}{
{line: "INSERT INTO `test` VALUES (1, 'first', 'hello mysql; 2', 'e1', 'a,b');",
expected: "1, 'first', 'hello mysql; 2', 'e1', 'a,b'"},
{line: "INSERT INTO `test` VALUES (0x22270073646661736661736466, 'first', 'hello mysql; 2', 'e1', 'a,b');",
expected: "0x22270073646661736661736466, 'first', 'hello mysql; 2', 'e1', 'a,b'"},
}
f := func(c rune) bool {
return c == '\r' || c == '\n'
}
for _, te := range lines {
l := strings.TrimRightFunc(te.line, f)
m := valuesExp.FindAllStringSubmatch(l, -1)
require.Len(t, m, 1)
require.Equal(t, "test", m[0][1])
require.Equal(t, te.expected, m[0][2])
}
}