Ejemplo n.º 1
0
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)
	}
}
Ejemplo n.º 2
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)
	}
}
Ejemplo n.º 3
0
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)
	}
}
Ejemplo n.º 4
0
				Sql:           fmt.Sprintf("%s /*split %v */", query.Sql, i),
				BindVariables: query.BindVariables,
			},
			RowCount: sandboxSQRowCount,
		}
		splits = append(splits, split)
	}
	return splits, nil
}

// Close does not change ExecCount
func (sbc *sandboxConn) Close() {
	sbc.CloseCount.Add(1)
}

func (sbc *sandboxConn) EndPoint() topo.EndPoint {
	return sbc.endPoint
}

var singleRowResult = &mproto.QueryResult{
	Fields: []mproto.Field{
		{"id", 3},
		{"value", 253}},
	RowsAffected: 1,
	InsertId:     0,
	Rows: [][]sqltypes.Value{{
		{sqltypes.Numeric("1")},
		{sqltypes.String("foo")},
	}},
}