diff --git a/repertory/repertory_test/src/database_test.cpp b/repertory/repertory_test/src/database_test.cpp index 970dcf89..b2942c57 100644 --- a/repertory/repertory_test/src/database_test.cpp +++ b/repertory/repertory_test/src/database_test.cpp @@ -53,6 +53,80 @@ public: } }; +static void common_insert(db::db3_t &db, bool dump = false) { + auto query = db::db_insert{db, "table"} + .column_value("column1", "test0") + .column_value("column2", "test1"); + if (dump) { + std::cout << query.dump() << std::endl; + } + + auto res = query.go(); + EXPECT_TRUE(res.ok()); +} + +static void common_select(db::db3_t &db, std::string value1, std::string value2, + bool dump = false) { + auto query = db::db_select{db, "table"}; + if (dump) { + std::cout << query.dump() << std::endl; + } + + auto res = query.go(); + EXPECT_TRUE(res.ok()); + EXPECT_TRUE(res.has_row()); + + std::size_t row_count{}; + while (res.has_row()) { + std::optional row; + EXPECT_TRUE(res.get_row(row)); + EXPECT_TRUE(row.has_value()); + if (row.has_value()) { + auto columns = row.value().get_columns(); + EXPECT_EQ(std::size_t(2U), columns.size()); + EXPECT_STREQ("column1", columns[0U].get_name().c_str()); + EXPECT_STREQ(value1.c_str(), + columns[0U].get_value().c_str()); + EXPECT_STREQ("column2", columns[1U].get_name().c_str()); + EXPECT_STREQ(value2.c_str(), + columns[1U].get_value().c_str()); + for (auto &&column : columns) { + std::cout << column.get_index() << ':'; + std::cout << column.get_name() << ':'; + std::cout << column.get_value() << std::endl; + } + } + ++row_count; + } + + EXPECT_EQ(std::size_t(1U), row_count); +} + +static void common_delete(db::db3_t &db, bool dump = false) { + { + auto query = db::db_delete{db, "table"}; + if (dump) { + std::cout << query.dump() << std::endl; + } + + auto res = query.go(); + EXPECT_TRUE(res.ok()); + } + + { + auto query = db::db_select{*db, "table"}; + auto res = query.go(); + EXPECT_TRUE(res.ok()); + + std::size_t row_count{}; + while (res.has_row()) { + ++row_count; + } + + EXPECT_EQ(std::size_t(0U), row_count); + } +} + TEST_F(database_test, db_delete_query) { auto query = db::db_delete{*db3.get(), "table"}; auto query_str = query.dump(); @@ -144,74 +218,15 @@ TEST_F(database_test, db_update_query) { } TEST_F(database_test, insert_select_delete) { - { - auto query = db::db_insert{*db3.get(), "table"} - .column_value("column1", "test0") - .column_value("column2", "test1"); - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } + common_insert(*db3.get(), true); - { - auto query = db::db_select{*db3.get(), "table"}; - std::cout << query.dump() << std::endl; + common_select(*db3.get(), "test0", "test1", true); - auto res = query.go(); - EXPECT_TRUE(res.ok()); - EXPECT_TRUE(res.has_row()); - - std::size_t row_count{}; - while (res.has_row()) { - std::optional row; - EXPECT_TRUE(res.get_row(row)); - EXPECT_TRUE(row.has_value()); - if (row.has_value()) { - auto columns = row.value().get_columns(); - EXPECT_EQ(std::size_t(2U), columns.size()); - EXPECT_STREQ("column1", columns[0U].get_name().c_str()); - EXPECT_STREQ("test0", columns[0U].get_value().c_str()); - EXPECT_STREQ("column2", columns[1U].get_name().c_str()); - EXPECT_STREQ("test1", columns[1U].get_value().c_str()); - for (auto &&column : columns) { - std::cout << column.get_index() << ':'; - std::cout << column.get_name() << ':'; - std::cout << column.get_value() << std::endl; - } - } - ++row_count; - } - - EXPECT_EQ(std::size_t(1U), row_count); - } - - { - auto query = db::db_delete{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } - - { - auto query = db::db_select{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - - std::size_t row_count{}; - while (res.has_row()) { - ++row_count; - } - - EXPECT_EQ(std::size_t(0U), row_count); - } + common_delete(*db3.get(), true); } TEST_F(database_test, insert_update_delete) { - { - auto query = db::db_insert{*db3.get(), "table"} - .column_value("column1", "test0") - .column_value("column2", "test1"); - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } + common_insert(*db3.get()); { auto query = db::db_update{*db3.get(), "table"} @@ -223,64 +238,13 @@ TEST_F(database_test, insert_update_delete) { EXPECT_TRUE(res.ok()); } - { - auto query = db::db_select{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - EXPECT_TRUE(res.has_row()); + common_select(*db3.get(), "moose", "test1"); - std::size_t row_count{}; - while (res.has_row()) { - std::optional row; - EXPECT_TRUE(res.get_row(row)); - EXPECT_TRUE(row.has_value()); - if (row.has_value()) { - auto columns = row.value().get_columns(); - EXPECT_EQ(std::size_t(2U), columns.size()); - EXPECT_STREQ("column1", columns[0U].get_name().c_str()); - EXPECT_STREQ("moose", columns[0U].get_value().c_str()); - EXPECT_STREQ("column2", columns[1U].get_name().c_str()); - EXPECT_STREQ("test1", columns[1U].get_value().c_str()); - for (auto &&column : columns) { - std::cout << column.get_index() << ':'; - std::cout << column.get_name() << ':'; - std::cout << column.get_value() << std::endl; - } - } - ++row_count; - } - - EXPECT_EQ(std::size_t(1U), row_count); - } - - { - auto query = db::db_delete{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } - - { - auto query = db::db_select{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - - std::size_t row_count{}; - while (res.has_row()) { - ++row_count; - } - - EXPECT_EQ(std::size_t(0U), row_count); - } + common_delete(*db3.get()); } TEST_F(database_test, insert_or_replace_and_delete) { - { - auto query = db::db_insert{*db3.get(), "table"} - .column_value("column1", "test0") - .column_value("column2", "test1"); - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } + common_insert(*db3.get()); { auto query = db::db_insert{*db3.get(), "table"} @@ -292,53 +256,8 @@ TEST_F(database_test, insert_or_replace_and_delete) { EXPECT_TRUE(res.ok()); } - { - auto query = db::db_select{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - EXPECT_TRUE(res.has_row()); + common_select(*db3.get(), "test0", "moose"); - std::size_t row_count{}; - while (res.has_row()) { - std::optional row; - EXPECT_TRUE(res.get_row(row)); - EXPECT_TRUE(row.has_value()); - if (row.has_value()) { - auto columns = row.value().get_columns(); - EXPECT_EQ(std::size_t(2U), columns.size()); - EXPECT_STREQ("column1", columns[0U].get_name().c_str()); - EXPECT_STREQ("test0", columns[0U].get_value().c_str()); - EXPECT_STREQ("column2", columns[1U].get_name().c_str()); - EXPECT_STREQ("moose", columns[1U].get_value().c_str()); - for (auto &&column : columns) { - std::cout << column.get_index() << ':'; - std::cout << column.get_name() << ':'; - std::cout << column.get_value() << std::endl; - } - } - ++row_count; - } - - EXPECT_EQ(std::size_t(1U), row_count); - } - - { - auto query = db::db_delete{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - } - - { - auto query = db::db_select{*db3.get(), "table"}; - auto res = query.go(); - EXPECT_TRUE(res.ok()); - - std::size_t row_count{}; - while (res.has_row()) { - ++row_count; - } - - EXPECT_EQ(std::size_t(0U), row_count); - } + common_delete(*db3.get()); } } // namespace repertory