func TestKeyRangeFilterMalformed(t *testing.T) {
	input := proto.BinlogTransaction{
		Statements: []proto.Statement{
			{
				Category: proto.BL_SET,
				Sql:      []byte("set1"),
			}, {
				Category: proto.BL_DML,
				Sql:      []byte("ddl"),
			}, {
				Category: proto.BL_DML,
				Sql:      []byte("dml1 /* EMD keyspace_id:20*/"),
			}, {
				Category: proto.BL_DML,
				Sql:      []byte("dml1 /* EMD keyspace_id:2a */"),
			},
		},
		GTIDField: myproto.GTIDField{Value: myproto.MustParseGTID("GoogleMysql", "41983-1")},
	}
	var got string
	f := KeyRangeFilterFunc(key.KIT_UINT64, testKeyRange, func(reply *proto.BinlogTransaction) error {
		got = bltToString(reply)
		return nil
	})
	f(&input)
	want := `position: "41983-1" `
	if want != got {
		t.Errorf("want %s, got %s", want, got)
	}
}
func TestUpdateBlpCheckpointTimestamp(t *testing.T) {
	gtid := myproto.MustParseGTID("GoogleMysql", "41983-58283")
	want := "UPDATE _vt.blp_checkpoint " +
		"SET pos='GoogleMysql/41983-58283', time_updated=88822, transaction_timestamp=481828 " +
		"WHERE source_shard_uid=78522"

	got := UpdateBlpCheckpoint(78522, myproto.ReplicationPosition{GTIDSet: gtid.GTIDSet()}, 88822, 481828)
	if got != want {
		t.Errorf("UpdateBlpCheckpoint() = %#v, want %#v", got, want)
	}
}
func TestPopulateBlpCheckpoint(t *testing.T) {
	gtid := myproto.MustParseGTID("GoogleMysql", "41983-19283")
	want := "INSERT INTO _vt.blp_checkpoint " +
		"(source_shard_uid, pos, time_updated, transaction_timestamp, flags) " +
		"VALUES (18372, 'GoogleMysql/41983-19283', 481823, 0, 'myflags')"

	got := PopulateBlpCheckpoint(18372, myproto.ReplicationPosition{GTIDSet: gtid.GTIDSet()}, 481823, "myflags")
	if got != want {
		t.Errorf("PopulateBlpCheckpoint() = %#v, want %#v", got, want)
	}
}
func TestDMLEvent(t *testing.T) {
	trans := &proto.BinlogTransaction{
		Statements: []proto.Statement{
			{
				Category: proto.BL_SET,
				Sql:      []byte("SET TIMESTAMP=2"),
			}, {
				Category: proto.BL_SET,
				Sql:      []byte("SET INSERT_ID=10"),
			}, {
				Category: proto.BL_DML,
				Sql:      []byte("query /* _stream vtocc_e (eid id name)  (null -1 'bmFtZQ==' ) (null 18446744073709551615 'bmFtZQ==' ); */"),
			}, {
				Category: proto.BL_DML,
				Sql:      []byte("query"),
			},
		},
		Timestamp: 1,
		GTIDField: myproto.GTIDField{Value: myproto.MustParseGTID("GoogleMysql", "41983-20")},
	}
	evs := &EventStreamer{
		sendEvent: func(event *proto.StreamEvent) error {
			switch event.Category {
			case "DML":
				want := `&{DML vtocc_e [eid id name] [[10 -1 [110 97 109 101]] [11 18446744073709551615 [110 97 109 101]]]  1 <nil>}`
				got := fmt.Sprintf("%v", event)
				if got != want {
					t.Errorf("got \n%s, want \n%s", got, want)
				}
			case "ERR":
				want := `&{ERR  [] [] query 1 <nil>}`
				got := fmt.Sprintf("%v", event)
				if got != want {
					t.Errorf("got %s, want %s", got, want)
				}
			case "POS":
				want := `&{POS  [] []  1 41983-20}`
				got := fmt.Sprintf("%v", event)
				if got != want {
					t.Errorf("got %s, want %s", got, want)
				}
			default:
				t.Errorf("unexppected: %#v", event)
			}
			return nil
		},
	}
	err := evs.transactionToEvent(trans)
	if err != nil {
		t.Error(err)
	}
}
func TestDDLEvent(t *testing.T) {
	trans := &proto.BinlogTransaction{
		Statements: []proto.Statement{
			{
				Category: proto.BL_SET,
				Sql:      []byte("SET TIMESTAMP=2"),
			}, {
				Category: proto.BL_DDL,
				Sql:      []byte("DDL"),
			},
		},
		Timestamp: 1,
		GTIDField: myproto.GTIDField{Value: myproto.MustParseGTID("GoogleMysql", "41983-20")},
	}
	evs := &EventStreamer{
		sendEvent: func(event *proto.StreamEvent) error {
			switch event.Category {
			case "DDL":
				want := `&{DDL  [] [] DDL 1 <nil>}`
				got := fmt.Sprintf("%v", event)
				if got != want {
					t.Errorf("got %s, want %s", got, want)
				}
			case "POS":
				want := `&{POS  [] []  1 41983-20}`
				got := fmt.Sprintf("%v", event)
				if got != want {
					t.Errorf("got %s, want %s", got, want)
				}
			default:
				t.Errorf("unexppected: %#v", event)
			}
			return nil
		},
	}
	err := evs.transactionToEvent(trans)
	if err != nil {
		t.Error(err)
	}
}