github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/show_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 . "github.com/insionng/yougam/libraries/pingcap/check" 18 "github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx/variable" 19 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testkit" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 21 ) 22 23 func (s *testSuite) TestShow(c *C) { 24 defer testleak.AfterTest(c)() 25 tk := testkit.NewTestKit(c, s.store) 26 tk.MustExec("use test") 27 testSQL := `drop table if exists show_test` 28 tk.MustExec(testSQL) 29 testSQL = `create table show_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int comment "c1_comment", c2 int, c3 int default 1) ENGINE=InnoDB AUTO_INCREMENT=28934 DEFAULT CHARSET=utf8 COMMENT "table_comment";` 30 tk.MustExec(testSQL) 31 32 testSQL = "show columns from show_test;" 33 result := tk.MustQuery(testSQL) 34 c.Check(result.Rows(), HasLen, 4) 35 36 testSQL = "show create table show_test;" 37 result = tk.MustQuery(testSQL) 38 c.Check(result.Rows(), HasLen, 1) 39 row := result.Rows()[0] 40 // For issue https://yougam/libraries/pingcap/tidb/issues/1061 41 expectedRow := []interface{}{ 42 "show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n PRIMARY KEY (`id`) \n) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28934 COMMENT='table_comment'"} 43 for i, r := range row { 44 c.Check(r, Equals, expectedRow[i]) 45 } 46 47 testSQL = "SHOW VARIABLES LIKE 'character_set_results';" 48 result = tk.MustQuery(testSQL) 49 c.Check(result.Rows(), HasLen, 1) 50 51 // Test case for index type and comment 52 tk.MustExec(`create table show_index (c int, index cIdx using hash (c) comment "index_comment_for_cIdx");`) 53 testSQL = "SHOW index from show_index;" 54 result = tk.MustQuery(testSQL) 55 c.Check(result.Rows(), HasLen, 1) 56 expectedRow = []interface{}{ 57 "show_index", int64(1), "cIdx", int64(1), "c", "utf8_bin", 58 int64(0), nil, nil, "YES", "HASH", "", "index_comment_for_cIdx"} 59 row = result.Rows()[0] 60 c.Check(row, HasLen, len(expectedRow)) 61 for i, r := range row { 62 c.Check(r, Equals, expectedRow[i]) 63 } 64 65 // For show like with escape 66 testSQL = `show tables like 'show\_test'` 67 result = tk.MustQuery(testSQL) 68 c.Check(result.Rows(), HasLen, 1) 69 70 var ss statistics 71 variable.RegisterStatistics(ss) 72 testSQL = "show status like 'character_set_results';" 73 result = tk.MustQuery(testSQL) 74 c.Check(result.Rows(), NotNil) 75 } 76 77 type statistics struct { 78 } 79 80 func (s statistics) GetScope(status string) variable.ScopeFlag { return variable.DefaultScopeFlag } 81 82 func (s statistics) Stats() (map[string]interface{}, error) { 83 m := make(map[string]interface{}) 84 var a, b interface{} 85 b = "123" 86 m["test_interface_nil"] = a 87 m["test_interface"] = b 88 m["test_interface_slice"] = []interface{}{"a", "b", "c"} 89 return m, nil 90 } 91 92 func (s *testSuite) TestForeignKeyInShowCreateTable(c *C) { 93 tk := testkit.NewTestKit(c, s.store) 94 tk.MustExec("use test") 95 testSQL := `drop table if exists show_test` 96 tk.MustExec(testSQL) 97 testSQL = `drop table if exists t1` 98 tk.MustExec(testSQL) 99 testSQL = `CREATE TABLE t1 (id int PRIMARY KEY AUTO_INCREMENT)` 100 tk.MustExec(testSQL) 101 102 testSQL = "create table show_test (`id` int PRIMARY KEY AUTO_INCREMENT, FOREIGN KEY `fk` (`id`) REFERENCES `t1` (`a`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB" 103 tk.MustExec(testSQL) 104 testSQL = "show create table show_test;" 105 result := tk.MustQuery(testSQL) 106 c.Check(result.Rows(), HasLen, 1) 107 row := result.Rows()[0] 108 expectedRow := []interface{}{ 109 "show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"} 110 for i, r := range row { 111 c.Check(r, Equals, expectedRow[i]) 112 } 113 114 testSQL = "alter table show_test drop foreign key `fk`" 115 tk.MustExec(testSQL) 116 testSQL = "show create table show_test;" 117 result = tk.MustQuery(testSQL) 118 c.Check(result.Rows(), HasLen, 1) 119 row = result.Rows()[0] 120 expectedRow = []interface{}{ 121 "show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n) ENGINE=InnoDB"} 122 for i, r := range row { 123 c.Check(r, Equals, expectedRow[i]) 124 } 125 126 testSQL = "ALTER TABLE SHOW_TEST ADD CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n " 127 tk.MustExec(testSQL) 128 testSQL = "show create table show_test;" 129 result = tk.MustQuery(testSQL) 130 c.Check(result.Rows(), HasLen, 1) 131 row = result.Rows()[0] 132 expectedRow = []interface{}{ 133 "show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"} 134 for i, r := range row { 135 c.Check(r, Equals, expectedRow[i]) 136 } 137 138 }