vitess.io/vitess@v0.16.2/go/vt/binlog/tables_filter_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package binlog 18 19 import ( 20 "fmt" 21 "testing" 22 23 binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" 24 querypb "vitess.io/vitess/go/vt/proto/query" 25 ) 26 27 var testTables = []string{ 28 "included1", 29 "included2", 30 } 31 32 func TestTablesFilterPass(t *testing.T) { 33 statements := []FullBinlogStatement{ 34 { 35 Statement: &binlogdatapb.BinlogTransaction_Statement{ 36 Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, 37 Sql: []byte("set1"), 38 }, 39 }, 40 { 41 Statement: &binlogdatapb.BinlogTransaction_Statement{ 42 Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 43 Sql: []byte("dml1 /* _stream included1 (id ) (500 ); */"), 44 }, 45 }, 46 { 47 Statement: &binlogdatapb.BinlogTransaction_Statement{ 48 Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 49 Sql: []byte("dml2 /* _stream included2 (id ) (500 ); */"), 50 }, 51 }, 52 } 53 eventToken := &querypb.EventToken{ 54 Position: "MariaDB/0-41983-1", 55 } 56 var got string 57 f := tablesFilterFunc(testTables, func(reply *binlogdatapb.BinlogTransaction) error { 58 got = bltToString(reply) 59 return nil 60 }) 61 _ = f(eventToken, statements) 62 want := `statement: <6, "set1"> statement: <7, "dml1 /* _stream included1 (id ) (500 ); */"> statement: <7, "dml2 /* _stream included2 (id ) (500 ); */"> position: "MariaDB/0-41983-1" ` 63 if want != got { 64 t.Errorf("want\n%s, got\n%s", want, got) 65 } 66 } 67 68 func TestTablesFilterSkip(t *testing.T) { 69 statements := []FullBinlogStatement{ 70 { 71 Statement: &binlogdatapb.BinlogTransaction_Statement{ 72 Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, 73 Sql: []byte("set1"), 74 }, 75 }, 76 { 77 Statement: &binlogdatapb.BinlogTransaction_Statement{ 78 Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 79 Sql: []byte("dml1 /* _stream excluded1 (id ) (500 ); */"), 80 }, 81 }, 82 } 83 eventToken := &querypb.EventToken{ 84 Position: "MariaDB/0-41983-1", 85 } 86 var got string 87 f := tablesFilterFunc(testTables, func(reply *binlogdatapb.BinlogTransaction) error { 88 got = bltToString(reply) 89 return nil 90 }) 91 _ = f(eventToken, statements) 92 want := `position: "MariaDB/0-41983-1" ` 93 if want != got { 94 t.Errorf("want %s, got %s", want, got) 95 } 96 } 97 98 func TestTablesFilterDDL(t *testing.T) { 99 statements := []FullBinlogStatement{ 100 { 101 Statement: &binlogdatapb.BinlogTransaction_Statement{ 102 Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, 103 Sql: []byte("set1"), 104 }, 105 }, 106 { 107 Statement: &binlogdatapb.BinlogTransaction_Statement{ 108 Category: binlogdatapb.BinlogTransaction_Statement_BL_DDL, 109 Sql: []byte("ddl"), 110 }, 111 }, 112 } 113 eventToken := &querypb.EventToken{ 114 Position: "MariaDB/0-41983-1", 115 } 116 var got string 117 f := tablesFilterFunc(testTables, func(reply *binlogdatapb.BinlogTransaction) error { 118 got = bltToString(reply) 119 return nil 120 }) 121 _ = f(eventToken, statements) 122 want := `position: "MariaDB/0-41983-1" ` 123 if want != got { 124 t.Errorf("want %s, got %s", want, got) 125 } 126 } 127 128 func TestTablesFilterMalformed(t *testing.T) { 129 statements := []FullBinlogStatement{ 130 { 131 Statement: &binlogdatapb.BinlogTransaction_Statement{ 132 Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, 133 Sql: []byte("set1"), 134 }, 135 }, 136 { 137 Statement: &binlogdatapb.BinlogTransaction_Statement{ 138 Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 139 Sql: []byte("ddl"), 140 }, 141 }, 142 { 143 Statement: &binlogdatapb.BinlogTransaction_Statement{ 144 Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 145 Sql: []byte("dml1 /* _stream excluded1*/"), 146 }, 147 }, 148 } 149 eventToken := &querypb.EventToken{ 150 Position: "MariaDB/0-41983-1", 151 } 152 var got string 153 f := tablesFilterFunc(testTables, func(reply *binlogdatapb.BinlogTransaction) error { 154 got = bltToString(reply) 155 return nil 156 }) 157 _ = f(eventToken, statements) 158 want := `position: "MariaDB/0-41983-1" ` 159 if want != got { 160 t.Errorf("want %s, got %s", want, got) 161 } 162 } 163 164 func bltToString(tx *binlogdatapb.BinlogTransaction) string { 165 result := "" 166 for _, statement := range tx.Statements { 167 result += fmt.Sprintf("statement: <%d, \"%s\"> ", statement.Category, string(statement.Sql)) 168 } 169 result += fmt.Sprintf("position: \"%v\" ", tx.EventToken.Position) 170 return result 171 }