74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
|
|
package binlog
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/starainrt/go-mysql/mysql"
|
||
|
|
"github.com/starainrt/go-mysql/replication"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestParseBinlogEvent_TableMapEvent(t *testing.T) {
|
||
|
|
ev := &replication.BinlogEvent{
|
||
|
|
Header: &replication.EventHeader{EventType: replication.TABLE_MAP_EVENT},
|
||
|
|
Event: &replication.TableMapEvent{
|
||
|
|
Schema: []byte("db1"),
|
||
|
|
Table: []byte("tb1"),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
events := ParseBinlogEvent(ev)
|
||
|
|
if len(events) != 1 {
|
||
|
|
t.Fatalf("expected 1 event, got %d", len(events))
|
||
|
|
}
|
||
|
|
if events[0].Type != "tablemap" {
|
||
|
|
t.Fatalf("expected tablemap event type, got %q", events[0].Type)
|
||
|
|
}
|
||
|
|
if events[0].DB != "db1" || events[0].TB != "tb1" {
|
||
|
|
t.Fatalf("unexpected db/table: %s.%s", events[0].DB, events[0].TB)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParseBinlogEvent_TransactionPayloadContainsTableMap(t *testing.T) {
|
||
|
|
table := &replication.TableMapEvent{
|
||
|
|
Schema: []byte("db2"),
|
||
|
|
Table: []byte("tb2"),
|
||
|
|
ColumnType: []byte{mysql.MYSQL_TYPE_LONG},
|
||
|
|
}
|
||
|
|
|
||
|
|
payload := &replication.TransactionPayloadEvent{
|
||
|
|
CompressionType: CompressionZSTD,
|
||
|
|
Events: []*replication.BinlogEvent{
|
||
|
|
{
|
||
|
|
Header: &replication.EventHeader{EventType: replication.TABLE_MAP_EVENT},
|
||
|
|
Event: table,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Header: &replication.EventHeader{EventType: replication.WRITE_ROWS_EVENTv2},
|
||
|
|
Event: &replication.RowsEvent{
|
||
|
|
Table: table,
|
||
|
|
Rows: [][]interface{}{{int32(1)}},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
ev := &replication.BinlogEvent{
|
||
|
|
Header: &replication.EventHeader{EventType: replication.TRANSACTION_PAYLOAD_EVENT},
|
||
|
|
Event: payload,
|
||
|
|
}
|
||
|
|
|
||
|
|
events := ParseBinlogEvent(ev)
|
||
|
|
if len(events) != 2 {
|
||
|
|
t.Fatalf("expected 2 events from payload, got %d", len(events))
|
||
|
|
}
|
||
|
|
if events[0].Type != "tablemap" {
|
||
|
|
t.Fatalf("expected first payload event to be tablemap, got %q", events[0].Type)
|
||
|
|
}
|
||
|
|
if events[1].Type != "insert" {
|
||
|
|
t.Fatalf("expected second payload event to be insert, got %q", events[1].Type)
|
||
|
|
}
|
||
|
|
if events[0].CompressionType != "ZSTD" || events[1].CompressionType != "ZSTD" {
|
||
|
|
t.Fatalf("expected payload events to carry compression type, got %q/%q", events[0].CompressionType, events[1].CompressionType)
|
||
|
|
}
|
||
|
|
}
|