зеркало из https://github.com/github/vitess-gh.git
tabletserver endtoend: ExecuteBatch tests
This commit is contained in:
Родитель
59a20a7a57
Коммит
5c84087b0c
|
@ -80,3 +80,19 @@ func (client *QueryClient) Execute(query string, bindvars map[string]interface{}
|
|||
)
|
||||
return qr, err
|
||||
}
|
||||
|
||||
// ExecuteBatch executes a batch of queries.
|
||||
func (client *QueryClient) ExecuteBatch(queries []proto.BoundQuery, asTransaction bool) (*proto.QueryResultList, error) {
|
||||
var qr = &proto.QueryResultList{}
|
||||
err := client.server.ExecuteBatch(
|
||||
context.Background(),
|
||||
&client.target,
|
||||
&proto.QueryList{
|
||||
Queries: queries,
|
||||
AsTransaction: asTransaction,
|
||||
TransactionId: client.transactionID,
|
||||
},
|
||||
qr,
|
||||
)
|
||||
return qr, err
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
mproto "github.com/youtube/vitess/go/mysql/proto"
|
||||
"github.com/youtube/vitess/go/sqltypes"
|
||||
"github.com/youtube/vitess/go/vt/tabletserver/endtoend/framework"
|
||||
"github.com/youtube/vitess/go/vt/tabletserver/proto"
|
||||
)
|
||||
|
||||
func TestSimpleRead(t *testing.T) {
|
||||
|
@ -326,3 +327,145 @@ func TestConsolidation(t *testing.T) {
|
|||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchRead(t *testing.T) {
|
||||
client := framework.NewDefaultClient()
|
||||
queries := []proto.BoundQuery{{
|
||||
Sql: "select * from vtocc_a where id = :a",
|
||||
BindVariables: map[string]interface{}{"a": 2},
|
||||
}, {
|
||||
Sql: "select * from vtocc_b where id = :b",
|
||||
BindVariables: map[string]interface{}{"b": 2},
|
||||
}}
|
||||
qr1 := mproto.QueryResult{
|
||||
Fields: []mproto.Field{{
|
||||
Name: "eid",
|
||||
Type: mysql.TypeLonglong,
|
||||
Flags: 0,
|
||||
}, {
|
||||
Name: "id",
|
||||
Type: mysql.TypeLong,
|
||||
Flags: 0,
|
||||
}, {
|
||||
Name: "name",
|
||||
Type: mysql.TypeVarString,
|
||||
Flags: 0,
|
||||
}, {
|
||||
Name: "foo",
|
||||
Type: mysql.TypeVarString,
|
||||
Flags: mysql.FlagBinary,
|
||||
}},
|
||||
RowsAffected: 1,
|
||||
Rows: [][]sqltypes.Value{
|
||||
[]sqltypes.Value{
|
||||
sqltypes.Value{Inner: sqltypes.Numeric("1")},
|
||||
sqltypes.Value{Inner: sqltypes.Numeric("2")},
|
||||
sqltypes.Value{Inner: sqltypes.String("bcde")},
|
||||
sqltypes.Value{Inner: sqltypes.String("fghi")},
|
||||
},
|
||||
},
|
||||
}
|
||||
qr2 := mproto.QueryResult{
|
||||
Fields: []mproto.Field{{
|
||||
Name: "eid",
|
||||
Type: mysql.TypeLonglong,
|
||||
Flags: 0,
|
||||
}, {
|
||||
Name: "id",
|
||||
Type: mysql.TypeLong,
|
||||
Flags: 0,
|
||||
}},
|
||||
RowsAffected: 1,
|
||||
Rows: [][]sqltypes.Value{
|
||||
[]sqltypes.Value{
|
||||
sqltypes.Value{Inner: sqltypes.Numeric("1")},
|
||||
sqltypes.Value{Inner: sqltypes.Numeric("2")},
|
||||
},
|
||||
},
|
||||
}
|
||||
want := &proto.QueryResultList{
|
||||
List: []mproto.QueryResult{qr1, qr2},
|
||||
}
|
||||
|
||||
qrl, err := client.ExecuteBatch(queries, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(qrl, want) {
|
||||
t.Errorf("ExecueBatch: \n%#v, want \n%#v", qrl, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTransaction(t *testing.T) {
|
||||
client := framework.NewDefaultClient()
|
||||
queries := []proto.BoundQuery{{
|
||||
Sql: "insert into vtocc_test values(4, null, null, null)",
|
||||
}, {
|
||||
Sql: "select * from vtocc_test where intval = 4",
|
||||
}, {
|
||||
Sql: "delete from vtocc_test where intval = 4",
|
||||
}}
|
||||
|
||||
wantRows := [][]sqltypes.Value{
|
||||
[]sqltypes.Value{
|
||||
sqltypes.Value{Inner: sqltypes.Numeric("4")},
|
||||
sqltypes.Value{},
|
||||
sqltypes.Value{},
|
||||
sqltypes.Value{},
|
||||
},
|
||||
}
|
||||
|
||||
// Not in transaction, AsTransaction false
|
||||
qrl, err := client.ExecuteBatch(queries, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
|
||||
t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
|
||||
}
|
||||
|
||||
// Not in transaction, AsTransaction true
|
||||
qrl, err = client.ExecuteBatch(queries, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
|
||||
t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
|
||||
}
|
||||
|
||||
// In transaction, AsTransaction false
|
||||
func() {
|
||||
err = client.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer client.Commit()
|
||||
qrl, err = client.ExecuteBatch(queries, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
|
||||
t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
|
||||
}
|
||||
}()
|
||||
|
||||
// In transaction, AsTransaction true
|
||||
func() {
|
||||
err = client.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer client.Rollback()
|
||||
qrl, err = client.ExecuteBatch(queries, true)
|
||||
want := "error: cannot start a new transaction in the scope of an existing one"
|
||||
if err == nil || err.Error() != want {
|
||||
t.Errorf("Error: %v, want %s", err, want)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
|
@ -13,44 +13,6 @@ import utils
|
|||
|
||||
class TestNocache(framework.TestCase):
|
||||
|
||||
def test_batch(self):
|
||||
queries = ["select * from vtocc_a where id = :a",
|
||||
"select * from vtocc_b where id = :b"]
|
||||
bvars = [{"a": 2}, {"b": 2}]
|
||||
results = self.env.conn._execute_batch(queries, bvars, False)
|
||||
self.assertEqual(
|
||||
results,
|
||||
[([(1L, 2L, "bcde", "fghi")], 1, 0,
|
||||
[("eid", 8), ("id", 3), ("name", 253), ("foo", 253)]),
|
||||
([(1L, 2L)], 1, 0, [("eid", 8), ("id", 3)])])
|
||||
|
||||
# Not in transaction, as_transaction false
|
||||
queries = [
|
||||
"insert into vtocc_test (intval, floatval, charval, binval) "
|
||||
"values(4, null, null, null)",
|
||||
"select * from vtocc_test where intval = 4",
|
||||
"delete from vtocc_test where intval = 4",
|
||||
]
|
||||
results = self.env.conn._execute_batch(queries, [{}, {}, {}], False)
|
||||
self.assertEqual(results[1][0], [(4L, None, None, None)])
|
||||
|
||||
# In transaction, as_transaction false
|
||||
self.env.conn.begin()
|
||||
results = self.env.conn._execute_batch(queries, [{}, {}, {}], False)
|
||||
self.assertEqual(results[1][0], [(4L, None, None, None)])
|
||||
self.env.conn.commit()
|
||||
|
||||
# In transaction, as_transaction true
|
||||
self.env.conn.begin()
|
||||
with self.assertRaisesRegexp(
|
||||
dbexceptions.DatabaseError, ".*cannot start a new transaction.*"):
|
||||
self.env.conn._execute_batch(queries, [{}, {}, {}], True)
|
||||
self.env.conn.rollback()
|
||||
|
||||
# Not in transaction, as_transaction true
|
||||
results = self.env.conn._execute_batch(queries, [{}, {}, {}], True)
|
||||
self.assertEqual(results[1][0], [(4L, None, None, None)])
|
||||
|
||||
def test_bind_in_select(self):
|
||||
bv = {"bv": 1}
|
||||
cu = self.env.execute("select :bv from vtocc_test", bv)
|
||||
|
|
Загрузка…
Ссылка в новой задаче