Exemple #1
0
func TestQueryList(t *testing.T) {
	reflected, err := bson.Marshal(&reflectQueryList{
		Queries: []BoundQuery{{
			Sql:           "query",
			BindVariables: map[string]interface{}{"val": int64(1)},
		}},
		SessionId:     2,
		TransactionId: 1,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := QueryList{
		Queries: []BoundQuery{{
			Sql:           "query",
			BindVariables: map[string]interface{}{"val": int64(1)},
		}},
		SessionId:     2,
		TransactionId: 1,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled QueryList
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom.TransactionId != unmarshalled.TransactionId {
		t.Errorf("want %v, got %v", custom.TransactionId, unmarshalled.TransactionId)
	}
	if custom.SessionId != unmarshalled.SessionId {
		t.Errorf("want %v, got %v", custom.SessionId, unmarshalled.SessionId)
	}
	if custom.Queries[0].Sql != unmarshalled.Queries[0].Sql {
		t.Errorf("want %v, got %v", custom.Queries[0].Sql, unmarshalled.Queries[0].Sql)
	}
	if custom.Queries[0].BindVariables["val"].(int64) != unmarshalled.Queries[0].BindVariables["val"].(int64) {
		t.Errorf("want %v, got %v", custom.Queries[0].BindVariables["val"], unmarshalled.Queries[0].BindVariables["val"])
	}

	extra, err := bson.Marshal(&extraQueryList{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestQueryResultList(t *testing.T) {
	reflected, err := bson.Marshal(&reflectQueryResultList{
		List: []mproto.QueryResult{{
			Fields:       []mproto.Field{{"name", 1}},
			RowsAffected: 2,
			InsertId:     3,
			Rows: [][]sqltypes.Value{
				{{sqltypes.String("1")}, {sqltypes.String("aa")}},
			},
		}},
		Session: &commonSession,
		Error:   "error",
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := QueryResultList{
		List: []mproto.QueryResult{{
			Fields:       []mproto.Field{{"name", 1}},
			RowsAffected: 2,
			InsertId:     3,
			Rows: [][]sqltypes.Value{
				{{sqltypes.String("1")}, {sqltypes.String("aa")}},
			},
		}},
		Session: &commonSession,
		Error:   "error",
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled QueryResultList
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraQueryResultList{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestBinlogTransaction(t *testing.T) {
	reflected, err := bson.Marshal(&reflectBinlogTransaction{
		Statements: []reflectStatement{
			{
				Category: 1,
				Charset:  &mproto.Charset{Client: 12, Conn: 34, Server: 56},
				Sql:      []byte("sql"),
			},
		},
		Timestamp: 456,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := BinlogTransaction{
		Statements: []Statement{
			{
				Category: 1,
				Charset:  &mproto.Charset{Client: 12, Conn: 34, Server: 56},
				Sql:      []byte("sql"),
			},
		},
		Timestamp: 456,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled BinlogTransaction
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("%#v != %#v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraBinlogTransaction{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
Exemple #4
0
func TestQueryResult(t *testing.T) {
	want := "\x85\x00\x00\x00\x04Fields\x00*\x00\x00\x00\x030\x00\"\x00\x00\x00\x05Name\x00\x04\x00\x00\x00\x00name\x12Type\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00?RowsAffected\x00\x02\x00\x00\x00\x00\x00\x00\x00?InsertId\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04Rows\x00 \x00\x00\x00\x040\x00\x18\x00\x00\x00\x050\x00\x01\x00\x00\x00\x001\x051\x00\x02\x00\x00\x00\x00aa\x00\x00\x00"
	custom := QueryResult{
		Fields:       []Field{{"name", 1}},
		RowsAffected: 2,
		InsertId:     3,
		Rows: [][]sqltypes.Value{
			{{sqltypes.Numeric("1")}, {sqltypes.String("aa")}},
		},
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled QueryResult
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom.RowsAffected != unmarshalled.RowsAffected {
		t.Errorf("want %v, got %#v", custom.RowsAffected, unmarshalled.RowsAffected)
	}
	if custom.InsertId != unmarshalled.InsertId {
		t.Errorf("want %v, got %#v", custom.InsertId, unmarshalled.InsertId)
	}
	if custom.Fields[0].Name != unmarshalled.Fields[0].Name {
		t.Errorf("want %v, got %#v", custom.Fields[0].Name, unmarshalled.Fields[0].Name)
	}
	if custom.Fields[0].Type != unmarshalled.Fields[0].Type {
		t.Errorf("want %v, got %#v", custom.Fields[0].Type, unmarshalled.Fields[0].Type)
	}
	if !bytes.Equal(custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}
	if !bytes.Equal(custom.Rows[0][1].Raw(), unmarshalled.Rows[0][1].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}

	extra, err := bson.Marshal(&extraQueryResult{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestResponseBson(t *testing.T) {
	reflected, err := bson.Marshal(&reflectResponseBson{
		ServiceMethod: "aa",
		Seq:           1,
		Error:         "err",
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := ResponseBson{
		&rpc.Response{
			ServiceMethod: "aa",
			Seq:           1,
			Error:         "err",
		},
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	unmarshalled := ResponseBson{Response: new(rpc.Response)}
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom.ServiceMethod != unmarshalled.ServiceMethod {
		t.Errorf("want %v, got %#v", custom.ServiceMethod, unmarshalled.ServiceMethod)
	}
	if custom.Seq != unmarshalled.Seq {
		t.Errorf("want %v, got %#v", custom.Seq, unmarshalled.Seq)
	}
	if custom.Error != unmarshalled.Error {
		t.Errorf("want %v, got %#v", custom.Error, unmarshalled.Error)
	}

	extra, err := bson.Marshal(&extraResponseBson{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestKeyRangeQuery(t *testing.T) {
	reflected, err := bson.Marshal(&reflectKeyRangeQuery{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
		Keyspace:      "keyspace",
		KeyRanges:     []kproto.KeyRange{kproto.KeyRange{Start: "10", End: "18"}},
		TabletType:    "replica",
		Session:       &commonSession,
	})

	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := KeyRangeQuery{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
		Keyspace:      "keyspace",
		KeyRanges:     []kproto.KeyRange{kproto.KeyRange{Start: "10", End: "18"}},
		TabletType:    "replica",
		Session:       &commonSession,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled KeyRangeQuery
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraKeyRangeQuery{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestQueryShard(t *testing.T) {
	reflected, err := bson.Marshal(&reflectQueryShard{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
		Keyspace:      "keyspace",
		Shards:        []string{"shard1", "shard2"},
		TabletType:    topo.TabletType("replica"),
		Session:       &commonSession,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := QueryShard{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
		Keyspace:      "keyspace",
		Shards:        []string{"shard1", "shard2"},
		TabletType:    topo.TabletType("replica"),
		Session:       &commonSession,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled QueryShard
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraQueryShard{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestSession(t *testing.T) {
	reflected, err := bson.Marshal(&reflectSession{
		InTransaction: true,
		ShardSessions: []*ShardSession{{
			Keyspace:      "a",
			Shard:         "0",
			TabletType:    topo.TabletType("replica"),
			TransactionId: 1,
		}, {
			Keyspace:      "b",
			Shard:         "1",
			TabletType:    topo.TabletType("master"),
			TransactionId: 2,
		}},
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := commonSession
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled Session
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraSession{})
	if err != nil {
		t.Fatal(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestBsonMarshalUnmarshalReplicationPositionInStruct(t *testing.T) {
	gtidSetParsers["golf"] = func(s string) (GTIDSet, error) {
		return fakeGTID{flavor: "golf", value: s}, nil
	}
	input := ReplicationPosition{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
	want := ReplicationPosition{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}

	type mystruct struct {
		ReplicationPosition
	}

	buf, err := bson.Marshal(&mystruct{ReplicationPosition: input})
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var gotStruct mystruct
	if err = bson.Unmarshal(buf, &gotStruct); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if got := gotStruct.ReplicationPosition; !got.Equal(want) {
		t.Errorf("marshal->unmarshal mismatch, got %#v, want %#v", got, want)
	}
}
Exemple #10
0
func TestBsonMarshalUnmarshalGTIDFieldInStruct(t *testing.T) {
	gtidParsers["golf"] = func(s string) (GTID, error) {
		return fakeGTID{flavor: "golf", value: s}, nil
	}
	input := fakeGTID{flavor: "golf", value: "par"}
	want := fakeGTID{flavor: "golf", value: "par"}

	type mystruct struct {
		GTIDField
	}

	buf, err := bson.Marshal(&mystruct{GTIDField{input}})
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var gotStruct mystruct
	if err = bson.Unmarshal(buf, &gotStruct); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if got := gotStruct.GTIDField.Value; got != want {
		t.Errorf("marshal->unmarshal mismatch, got %#v, want %#v", got, want)
	}
}
Exemple #11
0
func TestBoundQuery(t *testing.T) {
	reflected, err := bson.Marshal(&reflectBoundQuery{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := BoundQuery{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled BoundQuery
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom.Sql != unmarshalled.Sql {
		t.Errorf("want %v, got %v", custom.Sql, unmarshalled.Sql)
	}
	if custom.BindVariables["val"].(int64) != unmarshalled.BindVariables["val"].(int64) {
		t.Errorf("want %v, got %v", custom.BindVariables["val"], unmarshalled.BindVariables["val"])
	}

	extra, err := bson.Marshal(&extraBoundQuery{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
Exemple #12
0
func TestSession(t *testing.T) {
	reflected, err := bson.Marshal(&reflectSession{
		SessionId:     2,
		TransactionId: 1,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := Session{
		SessionId:     2,
		TransactionId: 1,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled Session
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom != unmarshalled {
		t.Errorf("want %v, got %#v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraSession{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestBatchQueryShardBadType(t *testing.T) {
	unexpected, err := bson.Marshal(&badTypeBatchQueryShard{})
	if err != nil {
		t.Error(err)
	}
	var unmarshalled BatchQueryShard
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want := "unexpected kind 5 for batchQueryShard.Queries"
	if err == nil || want != err.Error() {
		t.Errorf("want %v, got %v", want, err)
	}
}
Exemple #14
0
func TestUnmarshalEmptyStructIntoUnused(t *testing.T) {
	buf, err := bson.Marshal(&struct{}{})
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// Check that a new-style server expecting Unused{} can handle an empty struct.
	var unused Unused
	if err := bson.Unmarshal(buf, &unused); err != nil {
		t.Errorf("bson.Unmarshal: %v", err)
	}
}
func TestKeyspaceIdsBatchQueryBadType(t *testing.T) {
	unexpected, err := bson.Marshal(&badTypeKeyspaceIdsBatchQuery{})
	if err != nil {
		t.Error(err)
	}
	var unmarshalled KeyspaceIdBatchQuery
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want := "unexpected kind 5 for keyspaceIdBatchQuery.Queries"
	if err == nil || want != err.Error() {
		t.Errorf("want %v, got %v", want, err)
	}
}
Exemple #16
0
func TestUnmarshalEmptyStructIntoStruct(t *testing.T) {
	buf, err := bson.Marshal(&struct{}{})
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// It should always be possible to add fields to something that's already a
	// struct. The struct name is irrelevant since it's never encoded.
	var out struct{ A, B string }
	if err := bson.Unmarshal(buf, &out); err != nil {
		t.Errorf("bson.Unmarshal: %v", err)
	}
}
Exemple #17
0
func TestUnmarshalUnusedIntoString(t *testing.T) {
	buf, err := bson.Marshal(&Unused{})
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// Check that it's safe for new clients to send Unused{} to an old server
	// expecting the naked empty string convention.
	var str string
	if err := bson.Unmarshal(buf, &str); err != nil {
		t.Errorf("bson.Unmarshal: %v", err)
	}
}
Exemple #18
0
func TestUnmarshalStructIntoUnused(t *testing.T) {
	buf, err := bson.Marshal(&struct{ A, B string }{"A", "B"})
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// Check that a new-style server expecting Unused{} can handle an actual
	// struct being sent.
	var unused Unused
	if err := bson.Unmarshal(buf, &unused); err != nil {
		t.Errorf("bson.Unmarshal: %v", err)
	}
}
Exemple #19
0
func TestUnmarshalStringIntoStruct(t *testing.T) {
	str := ""
	buf, err := bson.Marshal(&str)
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// This fails. That's why you can't have old-style (empty string) clients
	// talking to servers that already expect a real struct (not Unused).
	var out struct{ A, B string }
	if err := bson.Unmarshal(buf, &out); err == nil {
		t.Errorf("expected error from bson.Unmarshal, got none")
	}
}
Exemple #20
0
func TestUnmarshalStructIntoString(t *testing.T) {
	buf, err := bson.Marshal(&struct{ A, B string }{"A", "B"})
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// This fails. That's why you can't upgrade a method from unused (either old
	// or new style) to a real struct with public fields, if there are still
	// servers around that expect old-style string.
	var str string
	if err := bson.Unmarshal(buf, &str); err == nil {
		t.Errorf("expected error from bson.Unmarshal, got none")
	}
}
Exemple #21
0
func test(t *testing.T, caseno int, tcase TestCase) {
	actual, err := bson.Marshal(&tcase.qr)
	if err != nil {
		t.Errorf("Error on %d: %v", caseno, err)
	}
	if tcase.encoded != "" && string(actual) != tcase.encoded {
		t.Errorf("Expecting vs actual for %d:\n%#v\n%#v", caseno, tcase.encoded, string(actual))
	}
	var newqr QueryResult
	err = bson.Unmarshal(actual, &newqr)
	if err != nil {
		t.Errorf("Error on %d: %v", caseno, err)
	}
	compare(t, caseno, tcase.qr, newqr)
}
Exemple #22
0
func TestUnmarshalStringIntoUnused(t *testing.T) {
	// Previous versions would marshal a naked empty string for unused args.
	str := ""
	buf, err := bson.Marshal(&str)
	if err != nil {
		t.Fatalf("bson.Marshal: %v", err)
	}

	// Check that a new-style server expecting Unused{} can handle a naked empty
	// string sent by an old-style client.
	var unused Unused
	if err := bson.Unmarshal(buf, &unused); err != nil {
		t.Errorf("bson.Unmarshal: %v", err)
	}
}
Exemple #23
0
func TestExtraFieldsBson(t *testing.T) {
	swra := &SlaveWasRestartedArgs{
		Parent: topo.TabletAlias{
			Uid:  1,
			Cell: "aa",
		},
	}
	data, err := bson.Marshal(swra)
	if err != nil {
		t.Fatal(err)
	}

	output := &slaveWasRestartedTestArgs{}

	err = bson.Unmarshal(data, output)
	if err != nil {
		t.Error(err)
	}
}
Exemple #24
0
func TestBsonMarshalUnmarshalNilGTID(t *testing.T) {
	gtidParsers["golf"] = func(s string) (GTID, error) {
		return fakeGTID{flavor: "golf", value: s}, nil
	}
	input := GTID(nil)
	want := GTID(nil)

	buf, err := bson.Marshal(GTIDField{input})
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var gotField GTIDField
	if err = bson.Unmarshal(buf, &gotField); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if got := gotField.Value; got != want {
		t.Errorf("marshal->unmarshal mismatch, got %#v, want %#v", got, want)
	}
}
Exemple #25
0
func TestBsonMarshalUnmarshalReplicationPositionZero(t *testing.T) {
	gtidSetParsers["golf"] = func(s string) (GTIDSet, error) {
		return fakeGTID{flavor: "golf", value: s}, nil
	}
	input := ReplicationPosition{}
	want := ReplicationPosition{}

	buf, err := bson.Marshal(input)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var got ReplicationPosition
	if err = bson.Unmarshal(buf, &got); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if !got.Equal(want) {
		t.Errorf("marshal->unmarshal mismatch, got %#v, want %#v", got, want)
	}
}
Exemple #26
0
func TestMissingFieldsBson(t *testing.T) {
	swra := &slaveWasRestartedTestArgs{
		Parent: topo.TabletAlias{
			Uid:  1,
			Cell: "aa",
		},
		ExpectedMasterAddr:   "a1",
		ExpectedMasterIpAddr: "i1",
		ScrapStragglers:      true,
	}
	data, err := bson.Marshal(swra)
	if err != nil {
		t.Fatal(err)
	}

	output := &SlaveWasRestartedArgs{}
	err = bson.Unmarshal(data, output)
	if err != nil {
		t.Error(err)
	}
}
func TestStreamEvent(t *testing.T) {
	reflected, err := bson.Marshal(&reflectStreamEvent{
		Category:   "str1",
		TableName:  "str2",
		PKColNames: []string{"str3", "str4"},
		PKValues: [][]interface{}{
			[]interface{}{
				[]byte("str5"), 1, uint64(0xffffffffffffffff),
			},
			[]interface{}{
				[]byte("str6"), 2, uint64(0xfffffffffffffffe),
			},
		},
		Sql:       "str7",
		Timestamp: 3,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := StreamEvent{
		Category:   "str1",
		TableName:  "str2",
		PKColNames: []string{"str3", "str4"},
		PKValues: [][]interface{}{
			[]interface{}{
				[]byte("str5"), 1, uint64(0xffffffffffffffff),
			},
			[]interface{}{
				[]byte("str6"), 2, uint64(0xfffffffffffffffe),
			},
		},
		Sql:       "str7",
		Timestamp: 3,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled StreamEvent
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	want = fmt.Sprintf("%#v", custom)
	got = fmt.Sprintf("%#v", unmarshalled)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	extra, err := bson.Marshal(&extraStreamEvent{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestKeyspaceIdBatchQuery(t *testing.T) {
	reflected, err := bson.Marshal(&reflectKeyspaceIdBatchQuery{
		Queries: []reflectBoundQuery{{
			Sql:           "query",
			BindVariables: map[string]interface{}{"val": int64(1)},
		}},
		Keyspace:    "keyspace",
		KeyspaceIds: []kproto.KeyspaceId{kproto.KeyspaceId("10"), kproto.KeyspaceId("20")},
		Session: &Session{InTransaction: true,
			ShardSessions: []*ShardSession{{
				Keyspace:      "a",
				Shard:         "0",
				TabletType:    topo.TabletType("replica"),
				TransactionId: 1,
			}, {
				Keyspace:      "b",
				Shard:         "1",
				TabletType:    topo.TabletType("master"),
				TransactionId: 2,
			}},
		},
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := KeyspaceIdBatchQuery{
		Queries: []tproto.BoundQuery{{
			Sql:           "query",
			BindVariables: map[string]interface{}{"val": int64(1)},
		}},
		Keyspace:    "keyspace",
		KeyspaceIds: []kproto.KeyspaceId{kproto.KeyspaceId("10"), kproto.KeyspaceId("20")},
		Session:     &commonSession,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled KeyspaceIdBatchQuery
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraKeyspaceIdBatchQuery{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
func TestQueryResult(t *testing.T) {
	// We can't do the reflection test because bson
	// doesn't do it correctly for embedded fields.
	want := "|\x01\x00\x00" +
		"\x03Result\x00\x85\x00\x00\x00" +
		"\x04Fields\x00*\x00\x00\x00" +
		"\x030\x00\"\x00\x00\x00" +
		"\x05Name\x00\x04\x00\x00\x00\x00name" +
		"\x12Type\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
		"?RowsAffected\x00\x02\x00\x00\x00\x00\x00\x00\x00" +
		"?InsertId\x00\x03\x00\x00\x00\x00\x00\x00\x00" +
		"\x04Rows\x00 \x00\x00\x00" +
		"\x040\x00\x18\x00\x00\x00" +
		"\x050\x00\x01\x00\x00\x00" +
		"\x001\x051\x00\x02\x00\x00\x00\x00aa" +
		"\x00\x00\x00" +
		"\x03Session\x00\xd0\x00\x00\x00" +
		"\bInTransaction\x00\x01" +
		"\x04ShardSessions\x00\xac\x00\x00\x00" +
		"\x030\x00Q\x00\x00\x00" +
		"\x05Keyspace\x00\x01\x00\x00\x00\x00a" +
		"\x05Shard\x00\x01\x00\x00\x00\x000" +
		"\x05TabletType\x00\a\x00\x00\x00\x00replica" +
		"\x12TransactionId\x00\x01\x00\x00\x00\x00\x00\x00\x00" +
		"\x00" +
		"\x031\x00P\x00\x00\x00" +
		"\x05Keyspace\x00\x01\x00\x00\x00\x00b" +
		"\x05Shard\x00\x01\x00\x00\x00\x001" +
		"\x05TabletType\x00\x06\x00\x00\x00\x00master" +
		"\x12TransactionId\x00\x02\x00\x00\x00\x00\x00\x00\x00" +
		"\x00\x00\x00" +
		"\x05Error\x00\x05\x00\x00\x00\x00error" +
		"\x00"

	custom := QueryResult{
		Result: &mproto.QueryResult{
			Fields:       []mproto.Field{{"name", 1}},
			RowsAffected: 2,
			InsertId:     3,
			Rows: [][]sqltypes.Value{
				{{sqltypes.String("1")}, {sqltypes.String("aa")}},
			},
		},
		Session: &commonSession,
		Error:   "error",
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%+v, got\n%+v", want, got)
	}

	var unmarshalled QueryResult
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
	}
}
Exemple #30
0
func TestSrvKeySpace(t *testing.T) {
	reflected, err := bson.Marshal(&reflectSrvKeyspace{
		Partitions: map[string]*KeyspacePartition{
			string(TYPE_MASTER): &KeyspacePartition{
				Shards: []SrvShard{
					SrvShard{
						Name:        "test_shard",
						ServedTypes: []TabletType{TYPE_MASTER},
						MasterCell:  "test_cell",
					},
				},
			},
		},
		TabletTypes:        []TabletType{TYPE_MASTER},
		ShardingColumnName: "video_id",
		ShardingColumnType: key.KIT_UINT64,
		ServedFrom: map[string]string{
			string(TYPE_REPLICA): "other_keyspace",
		},
		SplitShardCount: 32,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := SrvKeyspace{
		Partitions: map[TabletType]*KeyspacePartition{
			TYPE_MASTER: &KeyspacePartition{
				Shards: []SrvShard{
					SrvShard{
						Name:        "test_shard",
						ServedTypes: []TabletType{TYPE_MASTER},
						MasterCell:  "test_cell",
						TabletTypes: []TabletType{},
					},
				},
			},
		},
		Shards:             []SrvShard{},
		TabletTypes:        []TabletType{TYPE_MASTER},
		ShardingColumnName: "video_id",
		ShardingColumnType: key.KIT_UINT64,
		ServedFrom: map[TabletType]string{
			TYPE_REPLICA: "other_keyspace",
		},
		SplitShardCount: 32,
	}

	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled SrvKeyspace
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(custom, unmarshalled) {
		t.Errorf("want \n%#v, got \n%#v", custom, unmarshalled)
	}

	extra, err := bson.Marshal(&extraSrvKeyspace{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}