Debian Codegraph @

unittest/gunit/dd_schema-t.cc

raw symbols SCIP
1 /* Copyright (c) 2014, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <stddef.h>
26
27 #include "my_inttypes.h"
28 #include "sql/dd/impl/dictionary_impl.h"
29 #include "sql/dd/impl/raw/object_keys.h"
30 #include "sql/dd/impl/raw/raw_record.h"
31 #include "sql/dd/impl/raw/raw_table.h"
32 #include "sql/dd/impl/tables/schemata.h"
33 #include "sql/dd/impl/transaction_impl.h"
34 #include "sql/dd/impl/types/schema_impl.h"
35 #include "sql/thd_raii.h"
36 #include "unittest/gunit/dd.h"
37 #include "unittest/gunit/test_utils.h"
38
39 namespace dd_schema_unittest {
40
41 using namespace dd;
42 using namespace dd_unittest;
43
44 using dd_unittest::Mock_dd_field_longlong;
45 using dd_unittest::Mock_dd_field_varstring;
46 using dd_unittest::Mock_dd_HANDLER;
47
48 using my_testing::Server_initializer;
49 using ::testing::_;
50 using ::testing::Invoke;
51 using ::testing::NiceMock;
52 using ::testing::Return;
53 using ::testing::SetArgPointee;
54 using ::testing::StrictMock;
55 using ::testing::WithArgs;
56
57 /**
58 Test fixture for testing the dd::Schema, Schema_impl and Raw_* classes.
59 A fresh instance of this class will be created for each of the TEST_F
60 functions below.
61
62 The functions SetUp(), TearDown(), SetUpTestCase(), TearDownTestCase() are
63 inherited from ::testing::Test (google naming style differs from MySQL).
64 */
65
66 class SchemaTest : public ::testing::Test {
67 protected:
68 SchemaTest() = default;
69
70 void SetUp() override {
71 m_dict = new Dictionary_impl();
72 m_def_cat_id = Dictionary_impl::DEFAULT_CATALOG_ID;
73
74 // Set handlerton slot, needed for lookup in handler's ht_info.
75 m_hton.slot = 0;
76
77 // Dummy server initialization.
78 m_init.SetUp();
79
80 // Ensure that we can use Update_dictionary_tables_ctx without
81 // employing Disable_autocommit_guard
82 m_init.thd()->variables.option_bits &= ~OPTION_AUTOCOMMIT;
83 m_init.thd()->variables.option_bits |= OPTION_NOT_AUTOCOMMIT;
84 }
85
86 void TearDown() override {
87 delete m_dict;
88
89 // Tear down dummy server.
90 m_init.TearDown();
91 }
92
93 // Return dummy thd.
94 THD *thd() { return m_init.thd(); }
95
96 // Return dummy handlerton.
97 handlerton *hton() { return &m_hton; }
98
99 // Begin transaction.
100 Update_dictionary_tables_ctx *begin_dd_updates() {
101 Update_dictionary_tables_ctx *ctx =
102 new (std::nothrow) Update_dictionary_tables_ctx(thd());
103 EXPECT_TRUE(thd()->variables.option_bits & OPTION_DD_UPDATE_CONTEXT);
104
105 // Add schema table to transaction context.
106 ctx->otx.register_tables<Schema>();
107
108 // Fake ctx->open_tables() by assigning fake schema TABLE object directly.
109 ctx->otx.get_table<dd::Schema>()->get_table_ref()->table =
110 get_schema_table(thd(), hton());
111
112 return ctx;
113 }
114
115 // Delete instances and commit transaction.
116 void commit_transaction(Update_dictionary_tables_ctx *ctx,
117 Fake_TABLE *table) {
118 delete ctx;
119
120 // Must destroy fields and handler explicitly to avoid gmock warning
121 for (uint i = 0; i < table->s->fields; ++i) destroy(table->field[i]);
122
123 destroy(table->file);
124 delete[] table->s->default_values;
125 delete[] table->record[1];
126 delete table;
127 }
128
129 handlerton m_hton; // Dummy handlerton.
130 Dictionary_impl *m_dict; // Dictionary instance.
131 Object_id m_def_cat_id; // Default catalog id.
132 Server_initializer m_init; // Server initializer.
133
134 private:
135 SchemaTest(SchemaTest const &) = delete;
136 SchemaTest &operator=(SchemaTest const &) = delete;
137 };
138
139 /**
140 When storing an object, Weak_object_impl::store is called.
141
142 The primary key for the object is created, returning a new Primary_id_key
143 instance with the object id, given by Entity_object::id(). The primary
144 key instance represents the primary key value.
145
146 Then, a Raw_record is created by Raw_table::prepare_record_for_update.
147 Here, the record is essentially retrieved by find_record based on the
148 primary key. This is done by creating a raw access key, which is the primary
149 key value with a physical representation suitable for looking up in a storage
150 engine. The raw key is used to get the appropriate record from the dd tables
151 by calling ha_index_read_map. By instrumenting this function to return
152 1, indicating no record found, we will provoke insert.
153
154 Then, for insert, Weak_object_impl::store will first call
155 prepare_record_for_insert, which just creates a new Raw_new_record instance.
156 Then, the schema object members are copied into the appropriate table fields
157 by calling the function Schema_impl::store_attributes. This, in turn, invokes
158 the various Field*::store functions, which are set up to invoke the
159 corresponding fake_store methods, and finally, Raw_new_record::insert
160 is called, which calls ha_write_row. The write_row function in the handler
161 api is instrumented to return 0 (success).
162
163 After storing the schema object, we verify that the values saved in
164 the various fields by fake_store are the same as we used to create the schema
165 object.
166 */
167
168 TEST_F(SchemaTest, CreateSchema) {
169 // Execution context.
170 Schema_impl *schema = nullptr;
171 Update_dictionary_tables_ctx *ctx = begin_dd_updates();
172 Fake_TABLE *schemata_table =
173 static_cast<Fake_TABLE *>(ctx->otx.get_table<Schema>()->get_table());
174 Mock_dd_HANDLER *ha = static_cast<Mock_dd_HANDLER *>(schemata_table->file);
175
176 // Schemata table fields.
177 Mock_dd_field_longlong *id =
178 static_cast<Mock_dd_field_longlong *>(schemata_table->field[0]);
179 Mock_dd_field_longlong *catalog_id =
180 static_cast<Mock_dd_field_longlong *>(schemata_table->field[1]);
181 Mock_dd_field_varstring *name =
182 static_cast<Mock_dd_field_varstring *>(schemata_table->field[2]);
183 Mock_dd_field_longlong *collation_id =
184 static_cast<Mock_dd_field_longlong *>(schemata_table->field[3]);
185 Mock_dd_field_longlong *created =
186 static_cast<Mock_dd_field_longlong *>(schemata_table->field[4]);
187 Mock_dd_field_longlong *last_altered =
188 static_cast<Mock_dd_field_longlong *>(schemata_table->field[5]);
189
190 // Schema properties.
191 Object_id real_id = 10;
192 const char *real_name = "testdd_schema";
193 Object_id real_collation_id = 20;
194 ulonglong real_created = 30;
195 ulonglong real_last_altered = 40;
196
197 // Create a new schema, set properties.
198 schema = new Schema_impl();
199 schema->set_id(real_id);
200 schema->set_name(real_name);
201 schema->set_default_collation_id(real_collation_id);
202 schema->set_created(real_created);
203 schema->set_last_altered(real_last_altered);
204
205 // Set expectations for insert:
206
207 // ha->index_read_map: Called once, return 1
208 ON_CALL(*ha, index_read_map(_, _, _, _))
209 .WillByDefault(Return(HA_ERR_KEY_NOT_FOUND));
210 EXPECT_CALL(*ha, index_read_map(_, _, _, _)).Times(1);
211
212 // id->store: Called twice, return 0
213 ON_CALL(*id, store(real_id, true))
214 .WillByDefault(Invoke(id, &Mock_dd_field_longlong::fake_store));
215 EXPECT_CALL(*id, store(real_id, true)).Times(2);
216
217 // catalog_id->store: Called once, return 0
218 ON_CALL(*catalog_id, store(m_def_cat_id, true))
219 .WillByDefault(Invoke(catalog_id, &Mock_dd_field_longlong::fake_store));
220 EXPECT_CALL(*catalog_id, store(m_def_cat_id, true)).Times(1);
221
222 // name->store: Called once, return 0
223 ON_CALL(*name, store(schema->name().c_str(), _, _))
224 .WillByDefault(Invoke(name, &Mock_dd_field_varstring::fake_store));
225 EXPECT_CALL(*name, store(schema->name().c_str(), _, _)).Times(1);
226
227 // collation_id->store: Called once, return 0
228 ON_CALL(*collation_id, store(real_collation_id, true))
229 .WillByDefault(Invoke(collation_id, &Mock_dd_field_longlong::fake_store));
230 EXPECT_CALL(*collation_id, store(real_collation_id, true)).Times(1);
231
232 // created->store: Called once, return 0
233 ON_CALL(*created, store(real_created, true))
234 .WillByDefault(Invoke(created, &Mock_dd_field_longlong::fake_store));
235 EXPECT_CALL(*created, store(real_created, true)).Times(1);
236
237 // last_altered->store: Called once, return 0
238 ON_CALL(*last_altered, store(real_last_altered, true))
239 .WillByDefault(Invoke(last_altered, &Mock_dd_field_longlong::fake_store));
240 EXPECT_CALL(*last_altered, store(real_last_altered, true)).Times(1);
241
242 // ha->write_row: Called once, return 0
243 ON_CALL(*ha, write_row(_)).WillByDefault(Return(0));
244 EXPECT_CALL(*ha, write_row(_)).Times(1);
245
246 // Store the schema.
247 schema->store(&ctx->otx);
248
249 // Verify real contents stored into faked fields.
250 EXPECT_TRUE(id->fake_val_uint() == real_id);
251 EXPECT_TRUE(catalog_id->fake_val_uint() == m_def_cat_id);
252 EXPECT_TRUE(strcmp(name->fake_val_c_str(), real_name) == 0);
253 EXPECT_TRUE(collation_id->fake_val_uint() == real_collation_id);
254 EXPECT_TRUE(created->fake_val_uint() == real_created);
255 EXPECT_TRUE(last_altered->fake_val_uint() == real_last_altered);
256
257 // Commit transaction and cleanup.
258 commit_transaction(ctx, schemata_table);
259 delete schema;
260 }
261
262 /**
263 To provoke an update, the setup is pretty much the same as for insert (see
264 above), but we must instrument index_read_map to return 0. This
265 makes a new Raw_record be created, and makes ha_update_row be called.
266 */
267
268 TEST_F(SchemaTest, UpdateSchema) {
269 // Execution context.
270 Schema_impl *schema = nullptr;
271 Update_dictionary_tables_ctx *ctx = begin_dd_updates();
272 Fake_TABLE *schemata_table =
273 static_cast<Fake_TABLE *>(ctx->otx.get_table<Schema>()->get_table());
274 Mock_dd_HANDLER *ha = static_cast<Mock_dd_HANDLER *>(schemata_table->file);
275
276 // Schemata table fields.
277 Mock_dd_field_longlong *id =
278 static_cast<Mock_dd_field_longlong *>(schemata_table->field[0]);
279 Mock_dd_field_longlong *catalog_id =
280 static_cast<Mock_dd_field_longlong *>(schemata_table->field[1]);
281 Mock_dd_field_varstring *name =
282 static_cast<Mock_dd_field_varstring *>(schemata_table->field[2]);
283 Mock_dd_field_longlong *collation_id =
284 static_cast<Mock_dd_field_longlong *>(schemata_table->field[3]);
285 Mock_dd_field_longlong *created =
286 static_cast<Mock_dd_field_longlong *>(schemata_table->field[4]);
287 Mock_dd_field_longlong *last_altered =
288 static_cast<Mock_dd_field_longlong *>(schemata_table->field[5]);
289
290 // Schema properties.
291 Object_id real_id = 11;
292 const char *real_name = "testdd_schema";
293 Object_id real_collation_id = 21;
294 ulonglong real_created = 31;
295 ulonglong real_last_altered = 41;
296
297 // Create a schema, set properties.
298 schema = new Schema_impl();
299 schema->set_id(real_id);
300 schema->set_name(real_name);
301 schema->set_default_collation_id(real_collation_id);
302 schema->set_created(real_created);
303 schema->set_last_altered(real_last_altered);
304
305 // Set expectations for update:
306
307 // ha->index_read_map: Called once, return 0
308 ON_CALL(*ha, index_read_map(_, _, _, _)).WillByDefault(Return(0));
309 EXPECT_CALL(*ha, index_read_map(_, _, _, _)).Times(1);
310
311 // id->store: Called twice, return 0
312 ON_CALL(*id, store(real_id, true))
313 .WillByDefault(Invoke(id, &Mock_dd_field_longlong::fake_store));
314 EXPECT_CALL(*id, store(real_id, true)).Times(2);
315
316 // catalog_id->store: Called once, return 0
317 ON_CALL(*catalog_id, store(m_def_cat_id, true))
318 .WillByDefault(Invoke(catalog_id, &Mock_dd_field_longlong::fake_store));
319 EXPECT_CALL(*catalog_id, store(m_def_cat_id, true)).Times(1);
320
321 // name->store: Called once, return 0
322 ON_CALL(*name, store(schema->name().c_str(), _, _))
323 .WillByDefault(Invoke(name, &Mock_dd_field_varstring::fake_store));
324 EXPECT_CALL(*name, store(schema->name().c_str(), _, _)).Times(1);
325
326 // collation_id->store: Called once, return 0
327 ON_CALL(*collation_id, store(real_collation_id, true))
328 .WillByDefault(Invoke(collation_id, &Mock_dd_field_longlong::fake_store));
329 EXPECT_CALL(*collation_id, store(real_collation_id, true)).Times(1);
330
331 // created->store: Called once, return 0
332 ON_CALL(*created, store(real_created, true))
333 .WillByDefault(Invoke(created, &Mock_dd_field_longlong::fake_store));
334 EXPECT_CALL(*created, store(real_created, true)).Times(1);
335
336 // last_altered->store: Called once, return 0
337 ON_CALL(*last_altered, store(real_last_altered, true))
338 .WillByDefault(Invoke(last_altered, &Mock_dd_field_longlong::fake_store));
339 EXPECT_CALL(*last_altered, store(real_last_altered, true)).Times(1);
340
341 // ha->update_row: Called once, return 0
342 ON_CALL(*ha, update_row(_, _)).WillByDefault(Return(0));
343 EXPECT_CALL(*ha, update_row(_, _)).Times(1);
344
345 // Store the schema.
346 schema->store(&ctx->otx);
347
348 // Verify real contents stored into faked fields.
349 EXPECT_TRUE(id->fake_val_uint() == real_id);
350 EXPECT_TRUE(catalog_id->fake_val_uint() == m_def_cat_id);
351 EXPECT_TRUE(strcmp(name->fake_val_c_str(), real_name) == 0);
352 EXPECT_TRUE(collation_id->fake_val_uint() == real_collation_id);
353 EXPECT_TRUE(created->fake_val_uint() == real_created);
354 EXPECT_TRUE(last_altered->fake_val_uint() == real_last_altered);
355
356 // Commit transaction and cleanup.
357 commit_transaction(ctx, schemata_table);
358 delete schema;
359 }
360
361 /**
362 For testing getting a schema, the setup is slightly different than for
363 insert and update. For a name lookup, the schemata table is first
364 read using catalog_id and name to get the primary key id. Then, the
365 table is read to retrieve the full schema object.
366
367 To get the expected behavior, we explicitly call the various fake_store
368 methods on the table fields to set the contents in advance. This will make
369 the val_* calls done when reading the schema object return the expected
370 values. After the schema object has been retrieved, we verify that the
371 contents of the object are the same as the faked and predefined values
372 we stored in the various table fields.
373 */
374
375 TEST_F(SchemaTest, GetSchema) {
376 // Execution context.
377 const Schema_impl *schema = nullptr;
378 Update_dictionary_tables_ctx *ctx = begin_dd_updates();
379 Fake_TABLE *schemata_table =
380 static_cast<Fake_TABLE *>(ctx->otx.get_table<Schema>()->get_table());
381 Mock_dd_HANDLER *ha = static_cast<Mock_dd_HANDLER *>(schemata_table->file);
382
383 // Schemata table fields.
384 Mock_dd_field_longlong *id =
385 static_cast<Mock_dd_field_longlong *>(schemata_table->field[0]);
386 Mock_dd_field_longlong *catalog_id =
387 static_cast<Mock_dd_field_longlong *>(schemata_table->field[1]);
388 Mock_dd_field_varstring *name =
389 static_cast<Mock_dd_field_varstring *>(schemata_table->field[2]);
390 Mock_dd_field_longlong *collation_id =
391 static_cast<Mock_dd_field_longlong *>(schemata_table->field[3]);
392 Mock_dd_field_longlong *created =
393 static_cast<Mock_dd_field_longlong *>(schemata_table->field[4]);
394 Mock_dd_field_longlong *last_altered =
395 static_cast<Mock_dd_field_longlong *>(schemata_table->field[5]);
396
397 // Schema properties.
398 Object_id real_id = 12;
399 const char *real_name = "testdd_schema";
400 Object_id real_collation_id = 22;
401 ulonglong real_created = 32;
402 ulonglong real_last_altered = 42;
403
404 // Set expectations for read execution:
405
406 // catalog_id->store: Called once, return 0
407 ON_CALL(*catalog_id, store(m_def_cat_id, true))
408 .WillByDefault(Return(TYPE_OK));
409 EXPECT_CALL(*catalog_id, store(m_def_cat_id, true)).Times(1);
410
411 // name->store: Called once, return TYPE_OK
412 ON_CALL(*name, store(_, _, _)).WillByDefault(Return(TYPE_OK));
413 EXPECT_CALL(*name, store(_, _, _)).Times(1);
414
415 // ha->index_read: Called once, return 0
416 ON_CALL(*ha, index_read_map(_, _, _, _)).WillByDefault(Return(0));
417 EXPECT_CALL(*ha, index_read_map(_, _, _, _)).Times(1);
418
419 // id->val_int: Called once, get faked id
420 ON_CALL(*id, val_int())
421 .WillByDefault(Invoke(id, &Mock_dd_field_longlong::fake_val_int));
422 EXPECT_CALL(*id, val_int()).Times(1);
423
424 // catalog_id->val_int: Never called, get faked id
425 ON_CALL(*catalog_id, val_int())
426 .WillByDefault(Invoke(catalog_id, &Mock_dd_field_longlong::fake_val_int));
427 EXPECT_CALL(*catalog_id, val_int()).Times(0);
428
429 // name->val_str: Called once, get faked name
430 ON_CALL(*name, val_str(_, _))
431 .WillByDefault(Invoke(name, &Mock_dd_field_varstring::fake_val_str));
432 EXPECT_CALL(*name, val_str(_, _)).Times(1);
433
434 // collation_id->val_int: Called once, get faked id
435 ON_CALL(*collation_id, val_int())
436 .WillByDefault(
437 Invoke(collation_id, &Mock_dd_field_longlong::fake_val_int));
438 EXPECT_CALL(*collation_id, val_int()).Times(1);
439
440 // created->val_int: Called once, get faked value
441 ON_CALL(*created, val_int())
442 .WillByDefault(Invoke(created, &Mock_dd_field_longlong::fake_val_int));
443 EXPECT_CALL(*created, val_int()).Times(1);
444
445 // last_altered->val_int: Called once, get faked value
446 ON_CALL(*last_altered, val_int())
447 .WillByDefault(
448 Invoke(last_altered, &Mock_dd_field_longlong::fake_val_int));
449 EXPECT_CALL(*last_altered, val_int()).Times(1);
450
451 // Set faked field contents.
452 id->fake_store(real_id, true);
453 catalog_id->fake_store(m_def_cat_id, true);
454 name->fake_store(real_name, 0, nullptr);
455 collation_id->fake_store(real_collation_id, true);
456 created->fake_store(real_created, true);
457 last_altered->fake_store(real_last_altered, true);
458
459 // Create a name key.
460 Item_name_key key;
461 Schema::update_name_key(&key, real_name);
462
463 // Get the raw table and lookup the object by the name key.
464 Raw_table *t = ctx->otx.get_table<Schema>();
465 std::unique_ptr<Raw_record> r;
466 EXPECT_FALSE(t->find_record(key, r));
467
468 // Restore the object from the record.
469 Entity_object *new_object = nullptr;
470 EXPECT_FALSE(Schema::DD_table::instance().restore_object_from_record(
471 &ctx->otx, *r.get(), &new_object));
472 schema = dynamic_cast<const Schema_impl *>(new_object);
473
474 // Verify values stored into faked fields are read into schema object.
475 EXPECT_TRUE(schema->id() == real_id);
476
477 // Catalog id not exposed in dd api yet
478 EXPECT_TRUE(schema->name() == real_name);
479 EXPECT_TRUE(schema->default_collation_id() == real_collation_id);
480 EXPECT_TRUE(schema->created(false) == real_created);
481 EXPECT_TRUE(schema->last_altered(false) == real_last_altered);
482
483 // Commit transaction and cleanup.
484 commit_transaction(ctx, schemata_table);
485 delete schema;
486 }
487 } // namespace dd_schema_unittest