From f37a1d036fa50b64f81b312a3d5f93dfc8c33cc8 Mon Sep 17 00:00:00 2001 From: Nitromelon Date: Wed, 29 May 2024 00:23:43 +0800 Subject: [PATCH] Fix pg client name; Add testcase. (#2043) --- lib/src/HttpAppFrameworkImpl.cc | 2 +- orm_lib/tests/CMakeLists.txt | 8 ++ orm_lib/tests/db_api_test.cc | 186 ++++++++++++++++++++++++++++++++ test.sh | 8 ++ 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 orm_lib/tests/db_api_test.cc diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 06609189..9ce5998f 100644 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -959,7 +959,7 @@ void HttpAppFrameworkImpl::addDbClient( userName, password, connectionNum, - userName, + name, isFast, characterSet, timeout, diff --git a/orm_lib/tests/CMakeLists.txt b/orm_lib/tests/CMakeLists.txt index f7160b6d..634d46af 100644 --- a/orm_lib/tests/CMakeLists.txt +++ b/orm_lib/tests/CMakeLists.txt @@ -41,6 +41,10 @@ add_executable(conn_options_test conn_options_test.cc ) +add_executable(db_api_test + db_api_test.cc + ) + set_property(TARGET db_test PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD}) set_property(TARGET db_test PROPERTY CXX_STANDARD_REQUIRED ON) set_property(TARGET db_test PROPERTY CXX_EXTENSIONS OFF) @@ -56,3 +60,7 @@ set_property(TARGET db_listener_test PROPERTY CXX_EXTENSIONS OFF) set_property(TARGET conn_options_test PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD}) set_property(TARGET conn_options_test PROPERTY CXX_STANDARD_REQUIRED ON) set_property(TARGET conn_options_test PROPERTY CXX_EXTENSIONS OFF) + +set_property(TARGET db_api_test PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD}) +set_property(TARGET db_api_test PROPERTY CXX_STANDARD_REQUIRED ON) +set_property(TARGET db_api_test PROPERTY CXX_EXTENSIONS OFF) diff --git a/orm_lib/tests/db_api_test.cc b/orm_lib/tests/db_api_test.cc new file mode 100644 index 00000000..45bc3fa7 --- /dev/null +++ b/orm_lib/tests/db_api_test.cc @@ -0,0 +1,186 @@ +#define DROGON_TEST_MAIN +#include +#include +#include + +using namespace drogon; +using namespace trantor; + +DROGON_TEST(DbApiTest) +{ +#if USE_POSTGRESQL + { + auto client = app().getDbClient("pg_non_fast"); + CHECK(client != nullptr); + client->closeAll(); + drogon::app().getLoop()->runInLoop([TEST_CTX]() { + auto client = app().getFastDbClient("pg_fast"); + CHECK(client != nullptr); + client->closeAll(); + }); + drogon::app().getIOLoop(0)->runInLoop([TEST_CTX]() { + auto client = app().getFastDbClient("pg_fast"); + CHECK(client != nullptr); + client->closeAll(); + }); + } +#endif + +#if USE_MYSQL + { + auto client = app().getDbClient("mysql_non_fast"); + CHECK(client != nullptr); + client->closeAll(); + drogon::app().getLoop()->runInLoop([TEST_CTX]() { + auto client = app().getFastDbClient("mysql_fast"); + CHECK(client != nullptr); + client->closeAll(); + }); + drogon::app().getIOLoop(0)->runInLoop([TEST_CTX]() { + auto client = app().getFastDbClient("mysql_fast"); + CHECK(client != nullptr); + client->closeAll(); + }); + } +#endif + +#if USE_SQLITE3 + { + auto client = app().getDbClient("sqlite3_non_fast"); + CHECK(client != nullptr); + client->closeAll(); + } +#endif + + app().getLoop()->runAfter(5, [TEST_CTX]() {}); // wait for some time +} + +const std::string_view pg_non_fast_config = R"({ + "name": "pg_non_fast", + "rdbms": "postgresql", + "host": "127.0.0.1", + "port": 5432, + "dbname": "test", + "user": "postgres", + "passwd": "123456", + "is_fast": false +})"; + +const std::string_view pg_fast_config = R"({ + "name": "pg_fast", + "rdbms": "postgresql", + "host": "127.0.0.1", + "port": 5432, + "dbname": "test", + "user": "postgres", + "passwd": "123456", + "is_fast": true +})"; + +const std::string_view mysql_non_fast_config = R"({ + "name": "mysql_non_fast", + "rdbms": "mysql", + "host": "127.0.0.1", + "port": 3306, + "dbname": "test", + "user": "root", + "passwd": "123456", + "is_fast": false +})"; + +const std::string_view mysql_fast_config = R"({ + "name": "mysql_fast", + "rdbms": "mysql", + "host": "127.0.0.1", + "port": 3306, + "dbname": "test", + "user": "root", + "passwd": "123456", + "is_fast": true +})"; + +const std::string_view sqlite3_non_fast_config = R"({ + "name": "sqlite3_non_fast", + "rdbms": "sqlite3", + "filename": "test.db", + "is_fast": false +})"; + +bool parseJson(std::string_view str, Json::Value *root, Json::String *errs) +{ + static Json::CharReaderBuilder &builder = + []() -> Json::CharReaderBuilder & { + static Json::CharReaderBuilder builder; + builder["collectComments"] = false; + return builder; + }(); + std::unique_ptr jsonReader(builder.newCharReader()); + return jsonReader->parse(str.data(), str.data() + str.size(), root, errs); +} + +int main(int argc, char **argv) +{ + Json::Value config; + config["app"]["number_of_threads"] = 1; + config["db_clients"] = Json::arrayValue; + + std::string err; + Json::Value cfg; +#if USE_POSTGRESQL + cfg = Json::objectValue; + if (!parseJson(pg_non_fast_config, &cfg, &err)) + { + LOG_ERROR << "Failed to parse json: " << err; + return 1; + } + config["db_clients"].append(cfg); + cfg = Json::objectValue; + if (!parseJson(pg_fast_config.data(), &cfg, &err)) + { + LOG_ERROR << "Failed to parse json: " << err; + return 1; + } + config["db_clients"].append(cfg); +#endif + +#if USE_MYSQL + cfg = Json::objectValue; + if (!parseJson(mysql_non_fast_config, &cfg, &err)) + { + LOG_ERROR << "Failed to parse json: " << err; + return 1; + } + config["db_clients"].append(cfg); + cfg = Json::objectValue; + if (!parseJson(mysql_fast_config.data(), &cfg, &err)) + { + LOG_ERROR << "Failed to parse json: " << err; + return 1; + } + config["db_clients"].append(cfg); +#endif + +#if USE_SQLITE3 + cfg = Json::objectValue; + if (!parseJson(sqlite3_non_fast_config, &cfg, &err)) + { + LOG_ERROR << "Failed to parse json: " << err; + return 1; + } + config["db_clients"].append(cfg); +#endif + + std::promise p1; + std::future f1 = p1.get_future(); + app().setThreadNum(1); + std::thread thr([&]() { + app().getLoop()->queueInLoop([&]() { p1.set_value(); }); + app().loadConfigJson(config).run(); + }); + + f1.get(); + int testStatus = test::run(argc, argv); + app().getLoop()->queueInLoop([]() { app().quit(); }); + thr.join(); + return testStatus; +} diff --git a/test.sh b/test.sh index 6894f4bc..220b44d9 100755 --- a/test.sh +++ b/test.sh @@ -222,6 +222,14 @@ function do_db_test() exit -1 fi fi + if [ -f "./orm_lib/tests/db_api_test" ]; then + echo "Test getDbClient() api" + ./orm_lib/tests/db_api_test -s + if [ $? -ne 0 ]; then + echo "Error in testing" + exit -1 + fi + fi if [ -f "./nosql_lib/redis/tests/redis_test" ]; then echo "Test redis" ./nosql_lib/redis/tests/redis_test -s