make fakesqldb returns an error for unrecognized query

fakesqldb returns an empty QueryResult if a query is not recognized.
This behavior reduces typing in unit tests but also causes confusions.
In general, if caller does not set the mock result for a particular
query, fakesqldb should return an error.
This commit is contained in:
Shengzhe Yao 2015-05-29 15:16:47 -07:00
Родитель b5f5970651
Коммит b2d56113c0
5 изменённых файлов: 100 добавлений и 34 удалений

Просмотреть файл

@ -7,6 +7,7 @@ package tabletserver
import (
"fmt"
"math/rand"
"reflect"
"testing"
"time"
@ -99,15 +100,23 @@ func TestQueryExecutorPlanPassDmlStrictModeAutoCommit(t *testing.T) {
}
func TestQueryExecutorPlanInsertPk(t *testing.T) {
setUpQueryExecutorTest()
testUtils := newTestUtils()
db := setUpQueryExecutorTest()
db.AddQuery("insert into test_table values (1) /* _stream test_table (pk ) (1 ); */", &mproto.QueryResult{})
want := &mproto.QueryResult{
Fields: make([]mproto.Field, 0),
Rows: make([][]sqltypes.Value, 0),
}
sql := "insert into test_table values(1)"
qre, sqlQuery := newTestQueryExecutor(
"insert into test_table values(1)",
sql,
context.Background(),
enableRowCache|enableStrict)
defer sqlQuery.disallowQueries()
checkPlanID(t, planbuilder.PLAN_INSERT_PK, qre.plan.PlanId)
testUtils.checkEqual(t, &mproto.QueryResult{}, qre.Execute())
got := qre.Execute()
if !reflect.DeepEqual(got, want) {
t.Fatalf("query: %s, QueryExecutor.Execute() = %v, want: %v", sql, got, want)
}
}
func TestQueryExecutorPlanInsertSubQueryAutoCommmit(t *testing.T) {
@ -405,20 +414,31 @@ func TestQueryExecutorPlanSelectSubQuery(t *testing.T) {
}
func TestQueryExecutorPlanSet(t *testing.T) {
setUpQueryExecutorTest()
db := setUpQueryExecutorTest()
testUtils := &testUtils{}
expected := &mproto.QueryResult{}
setQuery := "set unknown_key = 1"
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery := newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
testUtils.checkEqual(t, expected, qre.Execute())
// unrecognized set field will be delegated to MySQL and both Fields and Rows should be
// empty arrays in this case.
want := &mproto.QueryResult{
Fields: make([]mproto.Field, 0),
Rows: make([][]sqltypes.Value, 0),
}
got := qre.Execute()
if !reflect.DeepEqual(got, want) {
t.Fatalf("query: %s failed, got: %+v, want: %+v", setQuery, got, want)
}
sqlQuery.disallowQueries()
// set vt_pool_size
vtPoolSize := int64(37)
setQuery = fmt.Sprintf("set vt_pool_size = %d", vtPoolSize)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -432,6 +452,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_stream_pool_size
vtStreamPoolSize := int64(41)
setQuery = fmt.Sprintf("set vt_stream_pool_size = %d", vtStreamPoolSize)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -444,6 +465,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_transaction_cap
vtTransactionCap := int64(43)
setQuery = fmt.Sprintf("set vt_transaction_cap = %d", vtTransactionCap)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -456,6 +478,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_transaction_timeout
vtTransactionTimeout := 47
setQuery = fmt.Sprintf("set vt_transaction_timeout = %d", vtTransactionTimeout)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -469,6 +492,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_schema_reload_time
vtSchemaReloadTime := 53
setQuery = fmt.Sprintf("set vt_schema_reload_time = %d", vtSchemaReloadTime)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -482,6 +506,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_query_cache_size
vtQueryCacheSize := int64(59)
setQuery = fmt.Sprintf("set vt_query_cache_size = %d", vtQueryCacheSize)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -494,6 +519,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_query_timeout
vtQueryTimeout := int64(61)
setQuery = fmt.Sprintf("set vt_query_timeout = %d", vtQueryTimeout)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -507,6 +533,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_idle_timeout
vtIdleTimeout := int64(67)
setQuery = fmt.Sprintf("set vt_idle_timeout = %d", vtIdleTimeout)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -526,6 +553,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_query_timeout
vtSpotCheckRatio := 0.771
setQuery = fmt.Sprintf("set vt_spot_check_ratio = %f", vtSpotCheckRatio)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -539,6 +567,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_strict_mode, any non zero value enables strict mode
vtStrictMode := int64(2)
setQuery = fmt.Sprintf("set vt_strict_mode = %d", vtStrictMode)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -551,6 +580,7 @@ func TestQueryExecutorPlanSet(t *testing.T) {
// set vt_txpool_timeout
vtTxPoolTimeout := int64(71)
setQuery = fmt.Sprintf("set vt_txpool_timeout = %d", vtTxPoolTimeout)
db.AddQuery(setQuery, &mproto.QueryResult{})
qre, sqlQuery = newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
@ -588,15 +618,17 @@ func TestQueryExecutorPlanSetMaxResultSize(t *testing.T) {
func TestQueryExecutorPlanSetMaxDmlRows(t *testing.T) {
setUpQueryExecutorTest()
testUtils := &testUtils{}
expected := &mproto.QueryResult{}
want := &mproto.QueryResult{}
vtMaxDmlRows := int64(256)
setQuery := fmt.Sprintf("set vt_max_dml_rows = %d", vtMaxDmlRows)
qre, sqlQuery := newTestQueryExecutor(
setQuery, context.Background(), enableRowCache|enableStrict)
defer sqlQuery.disallowQueries()
checkPlanID(t, planbuilder.PLAN_SET, qre.plan.PlanId)
testUtils.checkEqual(t, expected, qre.Execute())
got := qre.Execute()
if !reflect.DeepEqual(got, want) {
t.Fatalf("query executor Execute() = %v, want: %v", got, want)
}
if qre.qe.maxDMLRows.Get() != vtMaxDmlRows {
t.Fatalf("set query failed, expected to have vt_max_dml_rows: %d, but got: %d", vtMaxDmlRows, qre.qe.maxDMLRows.Get())
}

Просмотреть файл

@ -416,6 +416,12 @@ func TestSchemaInfoQueryCache(t *testing.T) {
for query, result := range getSchemaInfoTestSupportedQueries() {
db.AddQuery(query, result)
}
firstSqlQuery := "select * from test_table_01"
secondSqlQuery := "select * from test_table_02"
db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{})
db.AddQuery("select * from test_table_02 where 1 != 1", &mproto.QueryResult{})
schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, true)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}
@ -429,13 +435,11 @@ func TestSchemaInfoQueryCache(t *testing.T) {
ctx := context.Background()
logStats := newSqlQueryStats("GetPlanStats", ctx)
firstSqlQuery := "select * from test_table_01"
schemaInfo.SetQueryCacheSize(1)
firstPlan := schemaInfo.GetPlan(ctx, logStats, firstSqlQuery)
if firstPlan == nil {
t.Fatalf("plan should not be nil")
}
secondSqlQuery := "select * from test_table_02"
secondPlan := schemaInfo.GetPlan(ctx, logStats, secondSqlQuery)
if secondPlan == nil {
t.Fatalf("plan should not be nil")
@ -471,6 +475,8 @@ func TestSchemaInfoStatsURL(t *testing.T) {
for query, result := range getSchemaInfoTestSupportedQueries() {
db.AddQuery(query, result)
}
sqlQuery := "select * from test_table_01"
db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{})
schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}
@ -482,7 +488,6 @@ func TestSchemaInfoStatsURL(t *testing.T) {
// warm up cache
ctx := context.Background()
logStats := newSqlQueryStats("GetPlanStats", ctx)
sqlQuery := "select * from test_table_01"
schemaInfo.GetPlan(ctx, logStats, sqlQuery)
request, _ := http.NewRequest("GET", schemaInfo.endpoints[debugQueryPlansKey], nil)
@ -787,6 +792,8 @@ func getSchemaInfoTestSupportedQueries() map[string]*mproto.QueryResult {
},
},
},
"begin": &mproto.QueryResult{},
"commit": &mproto.QueryResult{},
}
}

Просмотреть файл

@ -826,7 +826,20 @@ func TestExecuteBatchNestedTransaction(t *testing.T) {
}
func TestSqlQuerySplitQuery(t *testing.T) {
setUpSqlQueryTest()
db := setUpSqlQueryTest()
db.AddQuery("SELECT MIN(pk), MAX(pk) FROM test_table", &mproto.QueryResult{
Fields: []mproto.Field{
mproto.Field{Name: "pk", Type: mproto.VT_LONG},
},
RowsAffected: 1,
Rows: [][]sqltypes.Value{
[]sqltypes.Value{
sqltypes.MakeNumeric([]byte("1")),
sqltypes.MakeNumeric([]byte("100")),
},
},
})
testUtils := newTestUtils()
config := testUtils.newQueryServiceConfig()
sqlQuery := NewSqlQuery(config)

Просмотреть файл

@ -16,10 +16,13 @@ import (
"golang.org/x/net/context"
)
func TestExecuteCommit(t *testing.T) {
func TestTxPoolExecuteCommit(t *testing.T) {
tableName := "test_table"
sql := fmt.Sprintf("ALTER TABLE %s ADD test_column INT", tableName)
fakesqldb.Register()
sql := fmt.Sprintf("alter table %s add test_column int", tableName)
db := fakesqldb.Register()
db.AddQuery("begin", &proto.QueryResult{})
db.AddQuery(sql, &proto.QueryResult{})
txPool := newTxPool(true)
txPool.SetTimeout(1 * time.Second)
txPool.SetPoolTimeout(1 * time.Second)
@ -47,9 +50,13 @@ func TestExecuteCommit(t *testing.T) {
_ = txPool.Begin(ctx)
}
func TestExecuteRollback(t *testing.T) {
sql := "ALTER TABLE test_table ADD test_column INT"
fakesqldb.Register()
func TestTxPoolExecuteRollback(t *testing.T) {
sql := "alter table test_table add test_column int"
db := fakesqldb.Register()
db.AddQuery(sql, &proto.QueryResult{})
db.AddQuery("begin", &proto.QueryResult{})
db.AddQuery("rollback", &proto.QueryResult{})
txPool := newTxPool(false)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}
@ -67,9 +74,12 @@ func TestExecuteRollback(t *testing.T) {
}
}
func TestTransactionKiller(t *testing.T) {
sql := "ALTER TABLE test_table ADD test_column INT"
fakesqldb.Register()
func TestTxPoolTransactionKiller(t *testing.T) {
sql := "alter table test_table add test_column int"
db := fakesqldb.Register()
db.AddQuery(sql, &proto.QueryResult{})
db.AddQuery("begin", &proto.QueryResult{})
txPool := newTxPool(false)
// make sure transaction killer will run frequent enough
txPool.SetTimeout(time.Duration(10))
@ -91,7 +101,7 @@ func TestTransactionKiller(t *testing.T) {
}
}
func TestBeginAfterConnPoolClosed(t *testing.T) {
func TestTxPoolBeginAfterConnPoolClosed(t *testing.T) {
fakesqldb.Register()
txPool := newTxPool(false)
txPool.SetTimeout(time.Duration(10))
@ -113,8 +123,10 @@ func TestBeginAfterConnPoolClosed(t *testing.T) {
txPool.Begin(ctx)
}
func TestBeginWithPoolTimeout(t *testing.T) {
fakesqldb.Register()
func TestTxPoolBeginWithPoolTimeout(t *testing.T) {
db := fakesqldb.Register()
db.AddQuery("begin", &proto.QueryResult{})
txPool := newTxPool(false)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}
@ -131,7 +143,7 @@ func TestBeginWithPoolTimeout(t *testing.T) {
txPool.Begin(ctx)
}
func TestBeginWithShortDeadline(t *testing.T) {
func TestTxPoolBeginWithShortDeadline(t *testing.T) {
fakesqldb.Register()
txPool := newTxPool(false)
appParams := sqldb.ConnParams{}
@ -147,7 +159,7 @@ func TestBeginWithShortDeadline(t *testing.T) {
txPool.Begin(ctx)
}
func TestBeginWithPoolConnectionError(t *testing.T) {
func TestTxPoolBeginWithPoolConnectionError(t *testing.T) {
db := fakesqldb.Register()
db.EnableConnFail()
txPool := newTxPool(false)
@ -160,7 +172,7 @@ func TestBeginWithPoolConnectionError(t *testing.T) {
txPool.Begin(ctx)
}
func TestBeginWithExecError(t *testing.T) {
func TestTxPoolBeginWithExecError(t *testing.T) {
db := fakesqldb.Register()
db.AddRejectedQuery("begin")
txPool := newTxPool(false)
@ -199,9 +211,12 @@ func TestTxPoolSafeCommitFail(t *testing.T) {
}
func TestTxPoolRollbackFail(t *testing.T) {
sql := "alter table test_table add test_column int"
db := fakesqldb.Register()
db.AddQuery(sql, &proto.QueryResult{})
db.AddQuery("begin", &proto.QueryResult{})
db.AddRejectedQuery("rollback")
sql := "ALTER TABLE test_table ADD test_column INT"
txPool := newTxPool(false)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}
@ -233,6 +248,8 @@ func TestTxPoolGetConnFail(t *testing.T) {
func TestTxPoolExecFailDueToConnFail(t *testing.T) {
db := fakesqldb.Register()
db.AddQuery("begin", &proto.QueryResult{})
txPool := newTxPool(false)
appParams := sqldb.ConnParams{}
dbaParams := sqldb.ConnParams{}

Просмотреть файл

@ -12,7 +12,6 @@ import (
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/sqltypes"
@ -144,8 +143,7 @@ func (conn *Conn) ExecuteFetch(query string, maxrows int, wantfields bool) (*pro
}
result, ok := conn.db.GetQuery(query)
if !ok {
log.Warningf("unexpected query: %s, will return an empty result", query)
return &proto.QueryResult{}, nil
return nil, fmt.Errorf("query: %s is not supported", query)
}
qr := &proto.QueryResult{}
qr.RowsAffected = result.RowsAffected
@ -211,8 +209,7 @@ func (conn *Conn) ExecuteStreamFetch(query string) error {
}
result, ok := conn.db.GetQuery(query)
if !ok {
log.Warningf("unexpected query: %s, will return an empty result", query)
result = &proto.QueryResult{}
return fmt.Errorf("query: %s is not supported", query)
}
conn.curQueryResult = result
conn.curQueryRow = 0