Ejemplo n.º 1
0
func TestQuery(t *testing.T) {
	reflected, err := bson.Marshal(&reflectQuery{
		Sql:           "query",
		BindVariables: map[string]interface{}{"val": int64(1)},
		TransactionId: 1,
		ConnectionId:  2,
		SessionId:     3,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

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

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

	unexpected, err := bson.Marshal(&badQuery{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want = "Unrecognized tag Extra"
	if err == nil || want != err.Error() {
		t.Error("want %v, got %v", want, err)
	}
}
Ejemplo n.º 2
0
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.Error("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.Error("want %v, got %#v", custom.ServiceMethod, unmarshalled.ServiceMethod)
	}
	if custom.Seq != unmarshalled.Seq {
		t.Error("want %v, got %#v", custom.Seq, unmarshalled.Seq)
	}
	if custom.Error != unmarshalled.Error {
		t.Error("want %v, got %#v", custom.Error, unmarshalled.Error)
	}

	unexpected, err := bson.Marshal(&badResponseBson{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want = "Unrecognized tag Extra"
	if err == nil || want != err.Error() {
		t.Error("want %v, got %v", want, err)
	}
}
Ejemplo n.º 3
0
func TestSession(t *testing.T) {
	reflected, err := bson.Marshal(&reflectSession{
		TransactionId: 1,
		ConnectionId:  2,
		SessionId:     3,
	})
	if err != nil {
		t.Error(err)
	}
	want := string(reflected)

	custom := Session{
		TransactionId: 1,
		ConnectionId:  2,
		SessionId:     3,
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Error("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.Error("want %v, got %#v", custom, unmarshalled)
	}

	unexpected, err := bson.Marshal(&badSession{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want = "Unrecognized tag Extra"
	if err == nil || want != err.Error() {
		t.Error("want %v, got %v", want, err)
	}
}