MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
explain_filename-t.cc
1 /*
2  Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
19 #include "my_config.h"
20 #include <gtest/gtest.h>
21 
22 #include <mysqld_error.h>
23 #include <sql_class.h>
24 #include <sql_table.h>
25 
26 namespace explain_filename_unittest {
27 
28 const int BUFLEN= 1000;
29 
30 char to[BUFLEN];
31 char from[BUFLEN];
32 
33 const char *error_messages[1000];
34 
35 class PartitionTest : public ::testing::Test
36 {
37 protected:
38  virtual void SetUp()
39  {
40  // Save global settings.
41  m_charset= system_charset_info;
42  m_locale= my_default_lc_messages;
43  m_errmsgs= my_default_lc_messages->errmsgs->errmsgs;
44 
45  system_charset_info = &my_charset_utf8_bin;
46  my_default_lc_messages = &my_locale_en_US;
47 
48  /* Populate the necessary error messages */
49  error_messages[ER_DATABASE_NAME - ER_ERROR_FIRST] = "Database";
50  error_messages[ER_TABLE_NAME - ER_ERROR_FIRST] = "Table";
51  error_messages[ER_PARTITION_NAME - ER_ERROR_FIRST] = "Partition";
52  error_messages[ER_SUBPARTITION_NAME - ER_ERROR_FIRST] = "Subpartition";
53  error_messages[ER_TEMPORARY_NAME - ER_ERROR_FIRST] = "Temporary";
54  error_messages[ER_RENAMED_NAME - ER_ERROR_FIRST] = "Renamed";
55 
56  my_default_lc_messages->errmsgs->errmsgs = error_messages;
57  }
58 
59  virtual void TearDown()
60  {
61  // Restore global settings.
62  system_charset_info= m_charset;
63  my_default_lc_messages= m_locale;
64  my_default_lc_messages->errmsgs->errmsgs= m_errmsgs;
65  }
66 
67 private:
68  CHARSET_INFO *m_charset;
69  MY_LOCALE *m_locale;
70  const char **m_errmsgs;
71 };
72 
73 void test_1(const char *in, const char *exp, enum_explain_filename_mode mode)
74 {
75  char out[BUFLEN];
76 
77  uint len1 = explain_filename(0, in, out, BUFLEN, mode);
78 
79  /* expected output and actual output must be same */
80  bool pass = (strcmp(exp, out) == 0);
81 
82  /* length returned by explain_filename is fine */
83  bool length = (len1 == strlen(exp));
84 
85  EXPECT_EQ( (pass && length), true);
86  if (pass && length)
87  {
88  // pass
89  }
90  else
91  {
92  ADD_FAILURE() << "input file name: '" << in << "' explain output: '"
93  << out << "'" << std::endl;
94  }
95 }
96 
97 
98 TEST_F(PartitionTest, ExplainFilename)
99 {
100  test_1("test/t1.ibd",
101  "Database \"test\", Table \"t1.ibd\"",
102  EXPLAIN_ALL_VERBOSE);
103 
104  test_1("test/t1.ibd",
105  "\"test\".\"t1.ibd\"",
106  EXPLAIN_PARTITIONS_VERBOSE);
107 
108  test_1("test/t1.ibd",
109  "\"test\".\"t1.ibd\"",
110  EXPLAIN_PARTITIONS_AS_COMMENT);
111 
112  test_1("test/t1#TMP#",
113  "Database \"test\", Table \"t1#TMP#\"",
114  EXPLAIN_ALL_VERBOSE);
115 
116  test_1("test/#sql-2882.ibd",
117  "Database \"test\", Table \"#sql-2882.ibd\"",
118  EXPLAIN_ALL_VERBOSE);
119 
120  test_1("test/t1#REN#",
121  "Database \"test\", Table \"t1#REN#\"",
122  EXPLAIN_ALL_VERBOSE);
123 
124  test_1("test/t1@0023REN@0023",
125  "Database \"test\", Table \"t1#REN#\"",
126  EXPLAIN_ALL_VERBOSE);
127 
128  test_1("test/t1#p#p1",
129  "Database \"test\", Table \"t1\", Partition \"p1\"",
130  EXPLAIN_ALL_VERBOSE);
131 
132  test_1("test/t1#P#p1",
133  "\"test\".\"t1\" /* Partition \"p1\" */",
134  EXPLAIN_PARTITIONS_AS_COMMENT);
135 
136  test_1("test/t1#P#p1@00231",
137  "\"test\".\"t1\" /* Partition \"p1#1\" */",
138  EXPLAIN_PARTITIONS_AS_COMMENT);
139 
140  test_1("test/t1#P#p1#SP#sp1",
141  "\"test\".\"t1\" /* Partition \"p1\", Subpartition \"sp1\" */",
142  EXPLAIN_PARTITIONS_AS_COMMENT);
143 
144  test_1("test/t1#p1#SP#sp1",
145  "\"test\".\"t1#p1#SP#sp1\"",
146  EXPLAIN_PARTITIONS_AS_COMMENT);
147 
148  test_1("test/t1#p#p1@00232#SP#sp1@00231#REN#",
149  "\"test\".\"t1\" /* Renamed Partition \"p1#2\", Subpartition \"sp1#1\" */",
150  EXPLAIN_PARTITIONS_AS_COMMENT);
151 
152  test_1("test/t1#p#p1#SP#sp1#TMP#",
153  "\"test\".\"t1\" /* Temporary Partition \"p1\", Subpartition \"sp1\" */",
154  EXPLAIN_PARTITIONS_AS_COMMENT);
155 
156  test_1("test/#sql-t1#P#p1#SP#sp1#TMP#",
157  "\"test\".\"#sql-t1#P#p1#SP#sp1#TMP#\" /* Temporary Partition \"p1\", Subpartition \"sp1\" */",
158  EXPLAIN_PARTITIONS_AS_COMMENT);
159 
160  test_1("test/#sql-t1#P#p1#SP#sp1",
161  "\"test\".\"#sql-t1#P#p1#SP#sp1\" /* Partition \"p1\", Subpartition \"sp1\" */",
162  EXPLAIN_PARTITIONS_AS_COMMENT);
163 
164  test_1("test/#sqlx-33",
165  "\"test\".\"#sqlx-33\"",
166  EXPLAIN_PARTITIONS_AS_COMMENT);
167 
168  test_1("test/#mysql50#t",
169  "\"test\".\"#mysql50#t\"",
170  EXPLAIN_PARTITIONS_AS_COMMENT);
171 
172  test_1("#mysql50#t",
173  "\"#mysql50#t\"",
174  EXPLAIN_PARTITIONS_AS_COMMENT);
175 
176  test_1("@0023t",
177  "\"#t\"",
178  EXPLAIN_PARTITIONS_AS_COMMENT);
179 
180  test_1("test/t@0023",
181  "\"test\".\"t#\"",
182  EXPLAIN_PARTITIONS_AS_COMMENT);
183 
184  /*
185  If a character not allowed in my_charset_filename is encountered,
186  then it will not be converted to system_charset_info!
187  */
188  test_1("test/t@0023#",
189  "\"test\".\"t@0023#\"",
190  EXPLAIN_PARTITIONS_AS_COMMENT);
191 }
192 
193 } // namespace explain_filename_unittest