github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/executor_simple_test.go (about)

     1  // Copyright 2016 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 executor_test
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx/variable"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/util"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testkit"
    25  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    26  )
    27  
    28  func (s *testSuite) TestCharsetDatabase(c *C) {
    29  	defer testleak.AfterTest(c)()
    30  	tk := testkit.NewTestKit(c, s.store)
    31  	testSQL := `create database if not exists cd_test_utf8 CHARACTER SET utf8 COLLATE utf8_bin;`
    32  	tk.MustExec(testSQL)
    33  
    34  	testSQL = `create database if not exists cd_test_latin1 CHARACTER SET latin1 COLLATE latin1_swedish_ci;`
    35  	tk.MustExec(testSQL)
    36  
    37  	testSQL = `use cd_test_utf8;`
    38  	tk.MustExec(testSQL)
    39  	tk.MustQuery(`select @@character_set_database;`).Check(testkit.Rows("utf8"))
    40  	tk.MustQuery(`select @@collation_database;`).Check(testkit.Rows("utf8_bin"))
    41  
    42  	testSQL = `use cd_test_latin1;`
    43  	tk.MustExec(testSQL)
    44  	tk.MustQuery(`select @@character_set_database;`).Check(testkit.Rows("latin1"))
    45  	tk.MustQuery(`select @@collation_database;`).Check(testkit.Rows("latin1_swedish_ci"))
    46  }
    47  
    48  func (s *testSuite) TestSetVar(c *C) {
    49  	defer testleak.AfterTest(c)()
    50  	tk := testkit.NewTestKit(c, s.store)
    51  	testSQL := "SET @a = 1;"
    52  	tk.MustExec(testSQL)
    53  
    54  	testSQL = `SET @a = "1";`
    55  	tk.MustExec(testSQL)
    56  
    57  	testSQL = "SET @a = null;"
    58  	tk.MustExec(testSQL)
    59  
    60  	testSQL = "SET @@global.autocommit = 1;"
    61  	tk.MustExec(testSQL)
    62  
    63  	testSQL = "SET @@global.autocommit = null;"
    64  	tk.MustExec(testSQL)
    65  
    66  	testSQL = "SET @@autocommit = 1;"
    67  	tk.MustExec(testSQL)
    68  
    69  	testSQL = "SET @@autocommit = null;"
    70  	tk.MustExec(testSQL)
    71  
    72  	errTestSql := "SET @@date_format = 1;"
    73  	_, err := tk.Exec(errTestSql)
    74  	c.Assert(err, NotNil)
    75  
    76  	errTestSql = "SET @@rewriter_enabled = 1;"
    77  	_, err = tk.Exec(errTestSql)
    78  	c.Assert(err, NotNil)
    79  
    80  	errTestSql = "SET xxx = abcd;"
    81  	_, err = tk.Exec(errTestSql)
    82  	c.Assert(err, NotNil)
    83  
    84  	errTestSql = "SET @@global.a = 1;"
    85  	_, err = tk.Exec(errTestSql)
    86  	c.Assert(err, NotNil)
    87  
    88  	errTestSql = "SET @@global.timestamp = 1;"
    89  	_, err = tk.Exec(errTestSql)
    90  	c.Assert(err, NotNil)
    91  
    92  	// For issue 998
    93  	testSQL = "SET @issue998a=1, @issue998b=5;"
    94  	tk.MustExec(testSQL)
    95  	tk.MustQuery(`select @issue998a, @issue998b;`).Check(testkit.Rows("1 5"))
    96  	testSQL = "SET @@autocommit=0, @issue998a=2;"
    97  	tk.MustExec(testSQL)
    98  	tk.MustQuery(`select @issue998a, @@autocommit;`).Check(testkit.Rows("2 0"))
    99  	testSQL = "SET @@global.autocommit=1, @issue998b=6;"
   100  	tk.MustExec(testSQL)
   101  	tk.MustQuery(`select @issue998b, @@global.autocommit;`).Check(testkit.Rows("6 1"))
   102  }
   103  
   104  func (s *testSuite) TestSetCharset(c *C) {
   105  	defer testleak.AfterTest(c)()
   106  	tk := testkit.NewTestKit(c, s.store)
   107  	tk.MustExec(`SET NAMES latin1`)
   108  
   109  	ctx := tk.Se.(context.Context)
   110  	sessionVars := variable.GetSessionVars(ctx)
   111  	for _, v := range variable.SetNamesVariables {
   112  		c.Assert(sessionVars.Systems[v] != "utf8", IsTrue)
   113  	}
   114  	tk.MustExec(`SET NAMES utf8`)
   115  	for _, v := range variable.SetNamesVariables {
   116  		c.Assert(sessionVars.Systems[v], Equals, "utf8")
   117  	}
   118  	c.Assert(sessionVars.Systems[variable.CollationConnection], Equals, "utf8_general_ci")
   119  }
   120  
   121  func (s *testSuite) TestDo(c *C) {
   122  	defer testleak.AfterTest(c)()
   123  	tk := testkit.NewTestKit(c, s.store)
   124  	tk.MustExec("do 1, 2")
   125  }
   126  
   127  func (s *testSuite) TestTransaction(c *C) {
   128  	defer testleak.AfterTest(c)()
   129  	tk := testkit.NewTestKit(c, s.store)
   130  	tk.MustExec("begin")
   131  	ctx := tk.Se.(context.Context)
   132  	c.Assert(inTxn(ctx), IsTrue)
   133  	tk.MustExec("commit")
   134  	c.Assert(inTxn(ctx), IsFalse)
   135  	tk.MustExec("begin")
   136  	c.Assert(inTxn(ctx), IsTrue)
   137  	tk.MustExec("rollback")
   138  	c.Assert(inTxn(ctx), IsFalse)
   139  }
   140  
   141  func inTxn(ctx context.Context) bool {
   142  	return (variable.GetSessionVars(ctx).Status & mysql.ServerStatusInTrans) > 0
   143  }
   144  
   145  func (s *testSuite) TestCreateUser(c *C) {
   146  	defer testleak.AfterTest(c)()
   147  	tk := testkit.NewTestKit(c, s.store)
   148  	// Make sure user test not in mysql.User.
   149  	result := tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="test" and Host="localhost"`)
   150  	result.Check(nil)
   151  	// Create user test.
   152  	createUserSQL := `CREATE USER 'test'@'localhost' IDENTIFIED BY '123';`
   153  	tk.MustExec(createUserSQL)
   154  	// Make sure user test in mysql.User.
   155  	result = tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="test" and Host="localhost"`)
   156  	rowStr := fmt.Sprintf("%v", []byte(util.EncodePassword("123")))
   157  	result.Check(testkit.Rows(rowStr))
   158  	// Create duplicate user with IfNotExists will be success.
   159  	createUserSQL = `CREATE USER IF NOT EXISTS 'test'@'localhost' IDENTIFIED BY '123';`
   160  	tk.MustExec(createUserSQL)
   161  
   162  	// Create duplicate user without IfNotExists will cause error.
   163  	createUserSQL = `CREATE USER 'test'@'localhost' IDENTIFIED BY '123';`
   164  	_, err := tk.Exec(createUserSQL)
   165  	c.Check(err, NotNil)
   166  }
   167  
   168  func (s *testSuite) TestSetPwd(c *C) {
   169  	defer testleak.AfterTest(c)()
   170  	tk := testkit.NewTestKit(c, s.store)
   171  	createUserSQL := `CREATE USER 'testpwd'@'localhost' IDENTIFIED BY '';`
   172  	tk.MustExec(createUserSQL)
   173  
   174  	result := tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
   175  	rowStr := fmt.Sprintf("%v", []byte(""))
   176  	result.Check(testkit.Rows(rowStr))
   177  
   178  	tk.MustExec(`SET PASSWORD FOR 'testpwd'@'localhost' = 'password';`)
   179  
   180  	result = tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
   181  	rowStr = fmt.Sprintf("%v", []byte(util.EncodePassword("password")))
   182  	result.Check(testkit.Rows(rowStr))
   183  }