github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/optimizer/validator_test.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package optimizer_test 15 16 import ( 17 "github.com/insionng/yougam/libraries/juju/errors" 18 . "github.com/insionng/yougam/libraries/pingcap/check" 19 "github.com/insionng/yougam/libraries/pingcap/tidb" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/context" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/optimizer" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/parser" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/terror" 24 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 25 ) 26 27 var _ = Suite(&testValidatorSuite{}) 28 29 type testValidatorSuite struct { 30 } 31 32 func (s *testValidatorSuite) TestValidator(c *C) { 33 defer testleak.AfterTest(c)() 34 cases := []struct { 35 sql string 36 inPrepare bool 37 err error 38 }{ 39 {"select ?", false, parser.ErrSyntax}, 40 {"select ?", true, nil}, 41 {"create table t(id int not null auto_increment default 2, key (id))", true, 42 errors.New("Invalid default value for 'id'")}, 43 {"create table t(id int not null default 2 auto_increment, key (id))", true, 44 errors.New("Invalid default value for 'id'")}, 45 {"create table t(id int not null auto_increment)", true, 46 errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, 47 {"create table t(id int not null auto_increment, c int auto_increment, key (id, c))", true, 48 errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, 49 {"create table t(id int not null auto_increment, c int, key (c, id))", true, 50 errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, 51 {"create table t(id decimal auto_increment, key (id))", true, 52 errors.New("Incorrect column specifier for column 'id'")}, 53 {"create table t(id float auto_increment, key (id))", true, nil}, 54 } 55 store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) 56 c.Assert(err, IsNil) 57 defer store.Close() 58 se, err := tidb.CreateSession(store) 59 c.Assert(err, IsNil) 60 for _, ca := range cases { 61 stmts, err1 := tidb.Parse(se.(context.Context), ca.sql) 62 c.Assert(err1, IsNil) 63 c.Assert(stmts, HasLen, 1) 64 stmt := stmts[0] 65 err = optimizer.Validate(stmt, ca.inPrepare) 66 c.Assert(terror.ErrorEqual(err, ca.err), IsTrue) 67 } 68 }