github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/executor_ddl_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/util/testkit" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 22 ) 23 24 func (s *testSuite) TestTruncateTable(c *C) { 25 defer testleak.AfterTest(c)() 26 tk := testkit.NewTestKit(c, s.store) 27 tk.MustExec("use test") 28 tk.MustExec(`drop table if exists truncate_test;`) 29 tk.MustExec(`create table truncate_test (a int)`) 30 tk.MustExec(`insert truncate_test values (1),(2),(3)`) 31 result := tk.MustQuery("select * from truncate_test") 32 result.Check(testkit.Rows("1", "2", "3")) 33 tk.MustExec("truncate table truncate_test") 34 result = tk.MustQuery("select * from truncate_test") 35 result.Check(nil) 36 } 37 38 func (s *testSuite) TestCreateTable(c *C) { 39 defer testleak.AfterTest(c)() 40 tk := testkit.NewTestKit(c, s.store) 41 tk.MustExec("use test") 42 // Test create an exist database 43 _, err := tk.Exec("CREATE database test") 44 c.Assert(err, NotNil) 45 46 // Test create an exist table 47 tk.MustExec("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 48 49 _, err = tk.Exec("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 50 c.Assert(err, NotNil) 51 52 // Test "if not exist" 53 tk.MustExec("CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 54 55 // Testcase for https://yougam/libraries/pingcap/tidb/issues/312 56 tk.MustExec(`create table issue312_1 (c float(24));`) 57 tk.MustExec(`create table issue312_2 (c float(25));`) 58 rs, err := tk.Exec(`desc issue312_1`) 59 c.Assert(err, IsNil) 60 for { 61 row, err2 := rs.Next() 62 c.Assert(err2, IsNil) 63 if row == nil { 64 break 65 } 66 c.Assert(row.Data[1].GetString(), Equals, "float") 67 } 68 rs, err = tk.Exec(`desc issue312_2`) 69 c.Assert(err, IsNil) 70 for { 71 row, err2 := rs.Next() 72 c.Assert(err2, IsNil) 73 if row == nil { 74 break 75 } 76 c.Assert(row.Data[1].GetString(), Equals, "double") 77 } 78 79 // table option is auto-increment 80 tk.MustExec("drop table if exists create_auto_increment_test;") 81 tk.MustExec("create table create_auto_increment_test (id int not null auto_increment, name varchar(255), primary key(id)) auto_increment = 999;") 82 tk.MustExec("insert into create_auto_increment_test (name) values ('aa')") 83 tk.MustExec("insert into create_auto_increment_test (name) values ('bb')") 84 tk.MustExec("insert into create_auto_increment_test (name) values ('cc')") 85 r := tk.MustQuery("select * from create_auto_increment_test;") 86 rowStr1 := fmt.Sprintf("%v %v", 999, []byte("aa")) 87 rowStr2 := fmt.Sprintf("%v %v", 1000, []byte("bb")) 88 rowStr3 := fmt.Sprintf("%v %v", 1001, []byte("cc")) 89 r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3)) 90 tk.MustExec("drop table create_auto_increment_test") 91 tk.MustExec("create table create_auto_increment_test (id int not null auto_increment, name varchar(255), primary key(id)) auto_increment = 1999;") 92 tk.MustExec("insert into create_auto_increment_test (name) values ('aa')") 93 tk.MustExec("insert into create_auto_increment_test (name) values ('bb')") 94 tk.MustExec("insert into create_auto_increment_test (name) values ('cc')") 95 r = tk.MustQuery("select * from create_auto_increment_test;") 96 rowStr1 = fmt.Sprintf("%v %v", 1999, []byte("aa")) 97 rowStr2 = fmt.Sprintf("%v %v", 2000, []byte("bb")) 98 rowStr3 = fmt.Sprintf("%v %v", 2001, []byte("cc")) 99 r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3)) 100 tk.MustExec("drop table create_auto_increment_test") 101 tk.MustExec("create table create_auto_increment_test (id int not null auto_increment, name varchar(255), key(id)) auto_increment = 1000;") 102 tk.MustExec("insert into create_auto_increment_test (name) values ('aa')") 103 r = tk.MustQuery("select * from create_auto_increment_test;") 104 rowStr1 = fmt.Sprintf("%v %v", 1000, []byte("aa")) 105 r.Check(testkit.Rows(rowStr1)) 106 } 107 108 func (s *testSuite) TestCreateDropDatabase(c *C) { 109 defer testleak.AfterTest(c)() 110 tk := testkit.NewTestKit(c, s.store) 111 tk.MustExec("create database if not exists drop_test;") 112 tk.MustExec("drop database if exists drop_test;") 113 tk.MustExec("create database drop_test;") 114 tk.MustExec("drop database drop_test;") 115 } 116 117 func (s *testSuite) TestCreateDropTable(c *C) { 118 defer testleak.AfterTest(c)() 119 tk := testkit.NewTestKit(c, s.store) 120 tk.MustExec("use test") 121 tk.MustExec("create table if not exists drop_test (a int)") 122 tk.MustExec("drop table if exists drop_test") 123 tk.MustExec("create table drop_test (a int)") 124 tk.MustExec("drop table drop_test") 125 } 126 127 func (s *testSuite) TestCreateDropIndex(c *C) { 128 defer testleak.AfterTest(c)() 129 tk := testkit.NewTestKit(c, s.store) 130 tk.MustExec("use test") 131 tk.MustExec("create table if not exists drop_test (a int)") 132 tk.MustExec("create index idx_a on drop_test (a)") 133 tk.MustExec("drop index idx_a on drop_test") 134 tk.MustExec("drop table drop_test") 135 } 136 137 func (s *testSuite) TestAlterTable(c *C) { 138 defer testleak.AfterTest(c)() 139 tk := testkit.NewTestKit(c, s.store) 140 tk.MustExec("use test") 141 tk.MustExec("create table if not exists alter_test (c1 int)") 142 tk.MustExec("alter table alter_test add column c2 int") 143 }