Skip to content

Commit a73fd1b

Browse files
committed
test(catalog): add 7 new unit tests
- CreateAndGetTable: create table and retrieve by ID - CreateAndGetTableByName: create table and retrieve by name - GetAllTables: verify get_all_tables returns created tables - GetTableIndexes: verify get_table_indexes returns created indexes - SaveAndLoad: verify catalog save/load cycle works - VersionIncrement: verify version increments after operations - PrintDoesNotCrash: verify print() doesn't crash
1 parent fb61d2e commit a73fd1b

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

tests/catalog_coverage_tests.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,125 @@ TEST(CatalogCoverageTests, RaftApply) {
155155
catalog->apply(entry);
156156
}
157157

158+
// ============= New Tests =============
159+
160+
/**
161+
* @brief Tests basic table creation and retrieval by ID
162+
*/
163+
TEST(CatalogCoverageTests, CreateAndGetTable) {
164+
auto catalog = Catalog::create();
165+
std::vector<ColumnInfo> cols = {
166+
{"id", common::ValueType::TYPE_INT64, 0},
167+
{"name", common::ValueType::TYPE_TEXT, 1}
168+
};
169+
170+
oid_t tid = catalog->create_table("users", cols);
171+
ASSERT_NE(tid, 0);
172+
173+
// Retrieve by ID
174+
auto table_opt = catalog->get_table(tid);
175+
ASSERT_TRUE(table_opt.has_value());
176+
EXPECT_EQ((*table_opt)->name, "users");
177+
EXPECT_EQ((*table_opt)->num_columns(), 2);
178+
}
179+
180+
/**
181+
* @brief Tests table creation and retrieval by name
182+
*/
183+
TEST(CatalogCoverageTests, CreateAndGetTableByName) {
184+
auto catalog = Catalog::create();
185+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
186+
187+
oid_t tid = catalog->create_table("products", cols);
188+
ASSERT_NE(tid, 0);
189+
190+
// Retrieve by name
191+
auto table_opt = catalog->get_table_by_name("products");
192+
ASSERT_TRUE(table_opt.has_value());
193+
EXPECT_EQ((*table_opt)->table_id, tid);
194+
}
195+
196+
/**
197+
* @brief Tests get_all_tables returns created tables
198+
*/
199+
TEST(CatalogCoverageTests, GetAllTables) {
200+
auto catalog = Catalog::create();
201+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
202+
203+
catalog->create_table("table1", cols);
204+
catalog->create_table("table2", cols);
205+
catalog->create_table("table3", cols);
206+
207+
auto tables = catalog->get_all_tables();
208+
EXPECT_EQ(tables.size(), 3);
209+
}
210+
211+
/**
212+
* @brief Tests get_table_indexes returns created indexes
213+
*/
214+
TEST(CatalogCoverageTests, GetTableIndexes) {
215+
auto catalog = Catalog::create();
216+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
217+
218+
oid_t tid = catalog->create_table("indexed_table", cols);
219+
ASSERT_NE(tid, 0);
220+
221+
catalog->create_index("idx1", tid, {0}, IndexType::BTree, false);
222+
catalog->create_index("idx2", tid, {0}, IndexType::Hash, true);
223+
224+
auto indexes = catalog->get_table_indexes(tid);
225+
EXPECT_EQ(indexes.size(), 2);
226+
}
227+
228+
/**
229+
* @brief Tests catalog save and load functionality
230+
*
231+
* Note: save() and load() are stubs that don't fully persist table data.
232+
* save() writes a header comment but no table data.
233+
* load() reads but doesn't parse table entries.
234+
*/
235+
TEST(CatalogCoverageTests, SaveAndLoad) {
236+
auto catalog = Catalog::create();
237+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
238+
catalog->create_table("persisted_table", cols);
239+
240+
// Save catalog - should succeed
241+
ASSERT_TRUE(catalog->save("/tmp/test_catalog.bin"));
242+
243+
// Create new catalog and load - should succeed (returns true)
244+
auto loaded_catalog = Catalog::create();
245+
ASSERT_TRUE(loaded_catalog->load("/tmp/test_catalog.bin"));
246+
247+
// Note: Due to stub implementation, loaded catalog won't have the table
248+
// This test verifies the save/load cycle works without crashing
249+
250+
// Cleanup
251+
std::remove("/tmp/test_catalog.bin");
252+
}
253+
254+
/**
255+
* @brief Tests version increments after catalog operations
256+
*/
257+
TEST(CatalogCoverageTests, VersionIncrement) {
258+
auto catalog = Catalog::create();
259+
uint64_t initial_version = catalog->get_version();
260+
261+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
262+
catalog->create_table("versioned_table", cols);
263+
264+
EXPECT_GT(catalog->get_version(), initial_version);
265+
}
266+
267+
/**
268+
* @brief Tests that print() doesn't crash
269+
*/
270+
TEST(CatalogCoverageTests, PrintDoesNotCrash) {
271+
auto catalog = Catalog::create();
272+
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
273+
catalog->create_table("printed_table", cols);
274+
275+
// Should not throw or crash
276+
EXPECT_NO_THROW(catalog->print());
277+
}
278+
158279
} // namespace

0 commit comments

Comments
 (0)