-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog_coverage_tests.cpp
More file actions
286 lines (227 loc) · 8.83 KB
/
catalog_coverage_tests.cpp
File metadata and controls
286 lines (227 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* @file catalog_coverage_tests.cpp
* @brief Targeted unit tests to increase coverage of the Catalog module
*/
#include <gtest/gtest.h>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <vector>
#include "catalog/catalog.hpp"
#include "common/value.hpp"
#include "distributed/raft_types.hpp"
using namespace cloudsql;
namespace {
/**
* @brief Tests Catalog behavior with missing entities and invalid lookups.
*/
TEST(CatalogCoverageTests, MissingEntities) {
auto catalog = Catalog::create();
// Invalid table lookup
EXPECT_FALSE(catalog->get_table(9999).has_value());
EXPECT_FALSE(catalog->table_exists(9999));
EXPECT_FALSE(catalog->table_exists_by_name("non_existent"));
EXPECT_FALSE(catalog->get_table_by_name("non_existent").has_value());
// Invalid index lookup
EXPECT_FALSE(catalog->get_index(8888).has_value());
EXPECT_TRUE(catalog->get_table_indexes(9999).empty());
// Dropping non-existent entities
EXPECT_FALSE(catalog->drop_table(9999));
EXPECT_FALSE(catalog->drop_index(8888));
// Update stats for non-existent table
EXPECT_FALSE(catalog->update_table_stats(9999, 100));
}
/**
* @brief Tests Catalog behavior with duplicate entities and creation edge cases.
*/
TEST(CatalogCoverageTests, DuplicateEntities) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
oid_t tid = catalog->create_table("test_table", cols);
ASSERT_NE(tid, 0);
// Duplicate table creation should throw
EXPECT_THROW(catalog->create_table("test_table", cols), std::runtime_error);
// Create an index
oid_t iid = catalog->create_index("idx_id", tid, {0}, IndexType::BTree, true);
ASSERT_NE(iid, 0);
// Duplicate index creation should throw
EXPECT_THROW(catalog->create_index("idx_id", tid, {0}, IndexType::BTree, true),
std::runtime_error);
// Creating index on missing table
EXPECT_EQ(catalog->create_index("idx_missing", 9999, {0}, IndexType::BTree, false), 0);
}
/**
* @brief Helper to serialize CreateTable command for Raft simulation
*/
std::vector<uint8_t> serialize_create_table(const std::string& name,
const std::vector<ColumnInfo>& columns) {
std::vector<uint8_t> data;
data.push_back(1); // Type 1
uint32_t name_len = name.size();
size_t off = data.size();
data.resize(off + 4 + name_len);
std::memcpy(data.data() + off, &name_len, 4);
std::memcpy(data.data() + off + 4, name.data(), name_len);
uint32_t col_count = columns.size();
off = data.size();
data.resize(off + 4);
std::memcpy(data.data() + off, &col_count, 4);
for (const auto& col : columns) {
uint32_t cname_len = col.name.size();
off = data.size();
data.resize(off + 4 + cname_len + 1 + 2);
std::memcpy(data.data() + off, &cname_len, 4);
std::memcpy(data.data() + off + 4, col.name.data(), cname_len);
data[off + 4 + cname_len] = static_cast<uint8_t>(col.type);
std::memcpy(data.data() + off + 4 + cname_len + 1, &col.position, 2);
}
uint32_t shard_count = 1;
off = data.size();
data.resize(off + 4);
std::memcpy(data.data() + off, &shard_count, 4);
std::string addr = "127.0.0.1";
uint32_t addr_len = addr.size();
uint32_t sid = 0;
uint16_t port = 6441;
off = data.size();
data.resize(off + 4 + addr_len + 4 + 2);
std::memcpy(data.data() + off, &addr_len, 4);
std::memcpy(data.data() + off + 4, addr.data(), addr_len);
std::memcpy(data.data() + off + 4 + addr_len, &sid, 4);
std::memcpy(data.data() + off + 4 + addr_len + 4, &port, 2);
return data;
}
/**
* @brief Tests the Raft state machine application (apply) in the Catalog.
*/
TEST(CatalogCoverageTests, RaftApply) {
auto catalog = Catalog::create();
// 1. Replay CreateTable
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
std::vector<uint8_t> create_data = serialize_create_table("raft_table", cols);
raft::LogEntry entry;
entry.term = 1;
entry.index = 1;
entry.data = create_data;
catalog->apply(entry);
EXPECT_TRUE(catalog->table_exists_by_name("raft_table"));
auto table_opt = catalog->get_table_by_name("raft_table");
ASSERT_TRUE(table_opt.has_value());
oid_t tid = (*table_opt)->table_id;
// 2. Replay DropTable
std::vector<uint8_t> drop_data;
drop_data.push_back(2); // Type 2
drop_data.resize(5);
std::memcpy(drop_data.data() + 1, &tid, 4);
entry.index = 2;
entry.data = drop_data;
catalog->apply(entry);
EXPECT_FALSE(catalog->table_exists(tid));
EXPECT_FALSE(catalog->table_exists_by_name("raft_table"));
// 3. Replay with empty data (should do nothing)
entry.index = 3;
entry.data.clear();
catalog->apply(entry);
}
// ============= New Tests =============
/**
* @brief Tests basic table creation and retrieval by ID
*/
TEST(CatalogCoverageTests, CreateAndGetTable) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0},
{"name", common::ValueType::TYPE_TEXT, 1}};
oid_t tid = catalog->create_table("users", cols);
ASSERT_NE(tid, 0);
// Retrieve by ID
auto table_opt = catalog->get_table(tid);
ASSERT_TRUE(table_opt.has_value());
EXPECT_EQ((*table_opt)->name, "users");
EXPECT_EQ((*table_opt)->num_columns(), 2);
}
/**
* @brief Tests table creation and retrieval by name
*/
TEST(CatalogCoverageTests, CreateAndGetTableByName) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
oid_t tid = catalog->create_table("products", cols);
ASSERT_NE(tid, 0);
// Retrieve by name
auto table_opt = catalog->get_table_by_name("products");
ASSERT_TRUE(table_opt.has_value());
EXPECT_EQ((*table_opt)->table_id, tid);
}
/**
* @brief Tests get_all_tables returns created tables
*/
TEST(CatalogCoverageTests, GetAllTables) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
catalog->create_table("table1", cols);
catalog->create_table("table2", cols);
catalog->create_table("table3", cols);
auto tables = catalog->get_all_tables();
EXPECT_EQ(tables.size(), 3);
}
/**
* @brief Tests get_table_indexes returns created indexes
*/
TEST(CatalogCoverageTests, GetTableIndexes) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
oid_t tid = catalog->create_table("indexed_table", cols);
ASSERT_NE(tid, 0);
oid_t idx1_id = catalog->create_index("idx1", tid, {0}, IndexType::BTree, false);
ASSERT_NE(idx1_id, 0);
oid_t idx2_id = catalog->create_index("idx2", tid, {0}, IndexType::Hash, true);
ASSERT_NE(idx2_id, 0);
auto indexes = catalog->get_table_indexes(tid);
EXPECT_EQ(indexes.size(), 2);
}
/**
* @brief Tests catalog save and load functionality
*
* Note: save() and load() are stubs that don't fully persist table data.
* save() writes a header comment but no table data.
* load() reads but doesn't parse table entries.
*/
TEST(CatalogCoverageTests, SaveAndLoad) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
catalog->create_table("persisted_table", cols);
// Use unique temp path to avoid collisions in parallel runs
static int test_counter = 0;
std::string temp_filename = "/tmp/test_catalog_" + std::to_string(++test_counter) + ".bin";
std::filesystem::path temp_path(temp_filename);
// Save catalog - should succeed
ASSERT_TRUE(catalog->save(temp_path.string()));
// Create new catalog and load - should succeed (returns true)
auto loaded_catalog = Catalog::create();
ASSERT_TRUE(loaded_catalog->load(temp_path.string()));
// Note: Due to stub implementation, loaded catalog won't have the table
// This test verifies the save/load cycle works without crashing
// Cleanup
std::filesystem::remove(temp_path);
}
/**
* @brief Tests version increments after catalog operations
*/
TEST(CatalogCoverageTests, VersionIncrement) {
auto catalog = Catalog::create();
uint64_t initial_version = catalog->get_version();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
catalog->create_table("versioned_table", cols);
EXPECT_GT(catalog->get_version(), initial_version);
}
/**
* @brief Tests that print() doesn't crash
*/
TEST(CatalogCoverageTests, PrintDoesNotCrash) {
auto catalog = Catalog::create();
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
catalog->create_table("printed_table", cols);
// Should not throw or crash
EXPECT_NO_THROW(catalog->print());
}
} // namespace