Debian Codegraph @

unittest/gunit/xplugin/xpl/view_statement_builder_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2026, Oracle and/or its affiliates.
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, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27
28 #include "plugin/x/src/view_statement_builder.h"
29 #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h"
30
31 namespace xpl {
32 namespace test {
33
34 class View_statement_builder_impl : public View_statement_builder {
35 public:
36 explicit View_statement_builder_impl(const Expression_generator &gen)
37 : View_statement_builder(gen) {}
38
39 using View_statement_builder::add_algorithm;
40 using View_statement_builder::add_check_option;
41 using View_statement_builder::add_columns;
42 using View_statement_builder::add_definer;
43 using View_statement_builder::add_sql_security;
44 };
45
46 class View_statement_builder_test : public ::testing::Test {
47 public:
48 View_statement_builder_test()
49 : expr_gen(&query, args, schema, true), builder(expr_gen) {
50 *find.mutable_collection() = Collection("A");
51 }
52
53 Expression_generator::Arg_list args;
54 Query_string_builder query;
55 std::string schema;
56 Expression_generator expr_gen;
57 View_statement_builder_impl builder;
58 Find_statement_builder::Find find;
59
60 using View_delete = View_statement_builder_impl::View_drop;
61 using View_create = View_statement_builder_impl::View_create;
62 using View_modify = View_statement_builder_impl::View_modify;
63 using Column_list = Repeated_field_list<std::string, std::string>;
64 };
65
66 TEST_F(View_statement_builder_test, add_definer_empty) {
67 std::string definer;
68 ASSERT_NO_THROW(builder.add_definer(definer));
69 EXPECT_STREQ("", query.get().c_str());
70 }
71
72 TEST_F(View_statement_builder_test, add_definer) {
73 std::string definer = "user";
74 ASSERT_NO_THROW(builder.add_definer(definer));
75 EXPECT_STREQ("DEFINER='user' ", query.get().c_str());
76 }
77
78 TEST_F(View_statement_builder_test, add_definer_with_domain) {
79 std::string definer = "user@localhost";
80 ASSERT_NO_THROW(builder.add_definer(definer));
81 EXPECT_STREQ("DEFINER='user'@'localhost' ", query.get().c_str());
82 }
83
84 TEST_F(View_statement_builder_test, add_algorithm_undefined) {
85 ASSERT_NO_THROW(builder.add_algorithm(Mysqlx::Crud::UNDEFINED));
86 EXPECT_STREQ("ALGORITHM=UNDEFINED ", query.get().c_str());
87 }
88
89 TEST_F(View_statement_builder_test, add_algorithm_temptable) {
90 ASSERT_NO_THROW(builder.add_algorithm(Mysqlx::Crud::TEMPTABLE));
91 EXPECT_STREQ("ALGORITHM=TEMPTABLE ", query.get().c_str());
92 }
93
94 TEST_F(View_statement_builder_test, add_algorithm_merge) {
95 ASSERT_NO_THROW(builder.add_algorithm(Mysqlx::Crud::MERGE));
96 EXPECT_STREQ("ALGORITHM=MERGE ", query.get().c_str());
97 }
98
99 TEST_F(View_statement_builder_test, add_sql_security_definer) {
100 ASSERT_NO_THROW(builder.add_sql_security(Mysqlx::Crud::DEFINER));
101 EXPECT_STREQ("SQL SECURITY DEFINER ", query.get().c_str());
102 }
103
104 TEST_F(View_statement_builder_test, add_sql_security_invoker) {
105 ASSERT_NO_THROW(builder.add_sql_security(Mysqlx::Crud::INVOKER));
106 EXPECT_STREQ("SQL SECURITY INVOKER ", query.get().c_str());
107 }
108
109 TEST_F(View_statement_builder_test, add_check_option_cascaded) {
110 ASSERT_NO_THROW(builder.add_check_option(Mysqlx::Crud::CASCADED));
111 EXPECT_STREQ(" WITH CASCADED CHECK OPTION", query.get().c_str());
112 }
113
114 TEST_F(View_statement_builder_test, add_check_option_local) {
115 ASSERT_NO_THROW(builder.add_check_option(Mysqlx::Crud::LOCAL));
116 EXPECT_STREQ(" WITH LOCAL CHECK OPTION", query.get().c_str());
117 }
118
119 TEST_F(View_statement_builder_test, build_create_view_two_columns) {
120 ASSERT_NO_THROW(builder.add_columns(Column_list{"one", "two", "three"}));
121 EXPECT_STREQ(" (`one`,`two`,`three`)", query.get().c_str());
122 }
123
124 TEST_F(View_statement_builder_test, build_create_view_simple) {
125 View_create msg;
126 *msg.mutable_collection() = Collection("xview");
127 *msg.mutable_stmt() = find;
128 ASSERT_NO_THROW(builder.build(msg));
129 EXPECT_STREQ("CREATE VIEW `xview` AS SELECT doc FROM `A`",
130 query.get().c_str());
131 }
132
133 TEST_F(View_statement_builder_test, build_create_view_or_replace) {
134 View_create msg;
135 *msg.mutable_collection() = Collection("xview");
136 msg.set_replace_existing(true);
137 *msg.mutable_stmt() = find;
138 ASSERT_NO_THROW(builder.build(msg));
139 EXPECT_STREQ("CREATE OR REPLACE VIEW `xview` AS SELECT doc FROM `A`",
140 query.get().c_str());
141 }
142
143 TEST_F(View_statement_builder_test, build_create_view_no_replace) {
144 View_create msg;
145 *msg.mutable_collection() = Collection("xview");
146 msg.set_replace_existing(false);
147 *msg.mutable_stmt() = find;
148 ASSERT_NO_THROW(builder.build(msg));
149 EXPECT_STREQ("CREATE VIEW `xview` AS SELECT doc FROM `A`",
150 query.get().c_str());
151 }
152
153 TEST_F(View_statement_builder_test, build_create_view_column) {
154 View_create msg;
155 *msg.mutable_collection() = Collection("xview");
156 *msg.mutable_column() = Column_list{"one"};
157 *msg.mutable_stmt() = find;
158 ASSERT_NO_THROW(builder.build(msg));
159 EXPECT_STREQ("CREATE VIEW `xview` (`one`) AS SELECT doc FROM `A`",
160 query.get().c_str());
161 }
162
163 TEST_F(View_statement_builder_test, build_create_view_definer) {
164 View_create msg;
165 *msg.mutable_collection() = Collection("xview");
166 *msg.mutable_stmt() = find;
167 msg.set_definer("User");
168 ASSERT_NO_THROW(builder.build(msg));
169 EXPECT_STREQ("CREATE DEFINER='User' VIEW `xview` AS SELECT doc FROM `A`",
170 query.get().c_str());
171 }
172
173 TEST_F(View_statement_builder_test, build_create_view_algorithm) {
174 View_create msg;
175 *msg.mutable_collection() = Collection("xview");
176 *msg.mutable_stmt() = find;
177 msg.set_algorithm(Mysqlx::Crud::MERGE);
178 ASSERT_NO_THROW(builder.build(msg));
179 EXPECT_STREQ("CREATE ALGORITHM=MERGE VIEW `xview` AS SELECT doc FROM `A`",
180 query.get().c_str());
181 }
182
183 TEST_F(View_statement_builder_test, build_create_view_seciurity) {
184 View_create msg;
185 *msg.mutable_collection() = Collection("xview");
186 *msg.mutable_stmt() = find;
187 msg.set_security(Mysqlx::Crud::INVOKER);
188 ASSERT_NO_THROW(builder.build(msg));
189 EXPECT_STREQ(
190 "CREATE SQL SECURITY INVOKER VIEW `xview` AS SELECT doc FROM `A`",
191 query.get().c_str());
192 }
193
194 TEST_F(View_statement_builder_test, build_create_view_check) {
195 View_create msg;
196 *msg.mutable_collection() = Collection("xview");
197 *msg.mutable_stmt() = find;
198 msg.set_check(Mysqlx::Crud::CASCADED);
199 ASSERT_NO_THROW(builder.build(msg));
200 EXPECT_STREQ(
201 "CREATE VIEW `xview` AS SELECT doc FROM `A` WITH CASCADED CHECK OPTION",
202 query.get().c_str());
203 }
204
205 TEST_F(View_statement_builder_test, build_modify_view_simple) {
206 View_modify msg;
207 *msg.mutable_collection() = Collection("xview");
208 *msg.mutable_stmt() = find;
209 ASSERT_NO_THROW(builder.build(msg));
210 EXPECT_STREQ("ALTER VIEW `xview` AS SELECT doc FROM `A`",
211 query.get().c_str());
212 }
213
214 TEST_F(View_statement_builder_test, build_modify_view_column) {
215 View_modify msg;
216 *msg.mutable_collection() = Collection("xview");
217 *msg.mutable_column() = Column_list{"one"};
218 *msg.mutable_stmt() = find;
219 ASSERT_NO_THROW(builder.build(msg));
220 EXPECT_STREQ("ALTER VIEW `xview` (`one`) AS SELECT doc FROM `A`",
221 query.get().c_str());
222 }
223
224 TEST_F(View_statement_builder_test, build_modify_view_definer) {
225 View_modify msg;
226 *msg.mutable_collection() = Collection("xview");
227 *msg.mutable_stmt() = find;
228 msg.set_definer("User");
229 ASSERT_NO_THROW(builder.build(msg));
230 EXPECT_STREQ("ALTER DEFINER='User' VIEW `xview` AS SELECT doc FROM `A`",
231 query.get().c_str());
232 }
233
234 TEST_F(View_statement_builder_test, build_modify_view_algorithm) {
235 View_modify msg;
236 *msg.mutable_collection() = Collection("xview");
237 *msg.mutable_stmt() = find;
238 msg.set_algorithm(Mysqlx::Crud::MERGE);
239 ASSERT_NO_THROW(builder.build(msg));
240 EXPECT_STREQ("ALTER ALGORITHM=MERGE VIEW `xview` AS SELECT doc FROM `A`",
241 query.get().c_str());
242 }
243
244 TEST_F(View_statement_builder_test, build_modify_view_seciurity) {
245 View_modify msg;
246 *msg.mutable_collection() = Collection("xview");
247 *msg.mutable_stmt() = find;
248 msg.set_security(Mysqlx::Crud::INVOKER);
249 ASSERT_NO_THROW(builder.build(msg));
250 EXPECT_STREQ("ALTER SQL SECURITY INVOKER VIEW `xview` AS SELECT doc FROM `A`",
251 query.get().c_str());
252 }
253
254 TEST_F(View_statement_builder_test, build_modify_view_check) {
255 View_modify msg;
256 *msg.mutable_collection() = Collection("xview");
257 *msg.mutable_stmt() = find;
258 msg.set_check(Mysqlx::Crud::CASCADED);
259 ASSERT_NO_THROW(builder.build(msg));
260 EXPECT_STREQ(
261 "ALTER VIEW `xview` AS SELECT doc FROM `A` WITH CASCADED CHECK OPTION",
262 query.get().c_str());
263 }
264
265 TEST_F(View_statement_builder_test, build_delete_view) {
266 View_delete msg;
267 *msg.mutable_collection() = Collection("xview");
268 ASSERT_NO_THROW(builder.build(msg));
269 EXPECT_STREQ("DROP VIEW `xview`", query.get().c_str());
270 }
271
272 TEST_F(View_statement_builder_test, build_delete_view_verify_object) {
273 View_delete msg;
274 *msg.mutable_collection() = Collection("xview");
275 msg.set_if_exists(true);
276 ASSERT_NO_THROW(builder.build(msg));
277 EXPECT_STREQ("DROP VIEW IF EXISTS `xview`", query.get().c_str());
278 }
279
280 TEST_F(View_statement_builder_test, build_delete_view_no_verify_object) {
281 View_delete msg;
282 *msg.mutable_collection() = Collection("xview");
283 msg.set_if_exists(false);
284 ASSERT_NO_THROW(builder.build(msg));
285 EXPECT_STREQ("DROP VIEW `xview`", query.get().c_str());
286 }
287 } // namespace test
288 } // namespace xpl