From bf88d08f78d4d682d06448d1f6200095bf52ce0d Mon Sep 17 00:00:00 2001 From: An Tao Date: Sat, 17 Aug 2019 12:40:39 +0800 Subject: [PATCH] Modify the test of postgresql database (#215) --- orm_lib/inc/drogon/orm/Result.h | 14 ++ orm_lib/tests/db_test.cc | 382 ++++++++++++++++++++++++++++++-- 2 files changed, 374 insertions(+), 22 deletions(-) diff --git a/orm_lib/inc/drogon/orm/Result.h b/orm_lib/inc/drogon/orm/Result.h index 4a61e6b2..8ee1655a 100644 --- a/orm_lib/inc/drogon/orm/Result.h +++ b/orm_lib/inc/drogon/orm/Result.h @@ -146,5 +146,19 @@ class Result { } }; +inline void swap(Result &one, Result &two) noexcept +{ + one.swap(two); +} } // namespace orm } // namespace drogon + +namespace std +{ +template <> +inline void swap(drogon::orm::Result &one, + drogon::orm::Result &two) noexcept +{ + one.swap(two); +} +} // namespace std diff --git a/orm_lib/tests/db_test.cc b/orm_lib/tests/db_test.cc index 57fa39d9..30f853e4 100644 --- a/orm_lib/tests/db_test.cc +++ b/orm_lib/tests/db_test.cc @@ -24,15 +24,16 @@ using namespace drogon::orm; #define RESET "\033[0m" #define RED "\033[31m" /* Red */ #define GREEN "\033[32m" /* Green */ -#define TEST_COUNT 7 +#define TEST_COUNT 32 int counter = 0; std::promise pro; -auto f = pro.get_future(); +auto globalf = pro.get_future(); void addCount(int &count, std::promise &pro) { ++count; + // LOG_DEBUG << count; if (count == TEST_COUNT) { pro.set_value(1); @@ -67,7 +68,6 @@ int main() // Prepare the test environment *clientPtr << "DROP TABLE IF EXISTS USERS" >> [](const Result &r) { testOutput(true, "Prepare the test environment(0)"); - addCount(counter, pro); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "Prepare the test environment(0)"); @@ -94,9 +94,8 @@ int main() }; /// Test1:DbClient streaming-type interface /// 1.1 insert,non-blocking - *clientPtr << "insert into users \ - (user_id,user_name,password,org_name) \ - values($1,$2,$3,$4) returning *" + *clientPtr << "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4) returning *" << "pg" << "postgresql" << "123" @@ -111,9 +110,8 @@ int main() testOutput(false, "DbClient streaming-type interface(0)"); }; /// 1.2 insert,blocking - *clientPtr << "insert into users \ - (user_id,user_name,password,org_name) \ - values($1,$2,$3,$4) returning *" + *clientPtr << "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4) returning *" << "pg1" << "postgresql1" << "123" @@ -130,10 +128,7 @@ int main() /// 1.3 query,no-blocking *clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >> [](const Result &r) { - if (r.size() == 2) - testOutput(true, "DbClient streaming-type interface(2)"); - else - testOutput(false, "DbClient streaming-type interface(2)"); + testOutput(r.size() == 2, "DbClient streaming-type interface(2)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; @@ -142,10 +137,7 @@ int main() /// 1.4 query,blocking *clientPtr << "select * from users where 1 = 1" << Mode::Blocking >> [](const Result &r) { - if (r.size() == 2) - testOutput(true, "DbClient streaming-type interface(3)"); - else - testOutput(false, "DbClient streaming-type interface(3)"); + testOutput(r.size() == 2, "DbClient streaming-type interface(3)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; @@ -163,16 +155,362 @@ int main() count++; else { - if (count == 2) - testOutput(true, "DbClient streaming-type interface(4)"); - else - testOutput(false, "DbClient streaming-type interface(4)"); + testOutput(count == 2, "DbClient streaming-type interface(4)"); } } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "DbClient streaming-type interface(4)"); }; - f.get(); + /// 1.6 query, parameter binding + *clientPtr << "select * from users where id = $1" << 1 >> + [](const Result &r) { + testOutput(r.size() == 1, "DbClient streaming-type interface(5)"); + } >> + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(5)"); + }; + /// 1.7 query, parameter binding + *clientPtr << "select * from users where user_id = $1 and user_name = $2" + << "pg1" + << "postgresql1" >> + [](const Result &r) { + testOutput(r.size() == 1, "DbClient streaming-type interface(6)"); + } >> + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(6)"); + }; + /// 1.8 delete + *clientPtr << "delete from users where user_id = $1 and user_name = $2" + << "pg1" + << "postgresql1" >> + [](const Result &r) { + testOutput(r.affectedRows() == 1, + "DbClient streaming-type interface(7)"); + } >> + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(7)"); + }; + /// 1.9 update + *clientPtr << "update users set user_id = $1, user_name = $2 where user_id " + "= $3 and user_name = $4" + << "pg1" + << "postgresql1" + << "pg" + << "postgresql" >> + [](const Result &r) { + testOutput(r.affectedRows() == 1, + "DbClient streaming-type interface(8)"); + } >> + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(8)"); + }; + /// 1.10 truncate + *clientPtr << "truncate table users" >> [](const Result &r) { + testOutput(true, "DbClient streaming-type interface(9)"); + } >> [](const DrogonDbException &e) { + std::cerr << "error:" << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(9)"); + }; + /// Test asynchronous method + /// 2.1 insert + clientPtr->execSqlAsync( + "insert into users \ + (user_id,user_name,password,org_name) \ + values($1,$2,$3,$4) returning *", + [](const Result &r) { + // std::cout << "id=" << r[0]["id"].as() << std::endl; + testOutput(r[0]["id"].as() == 3, + "DbClient asynchronous interface(0)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(0)"); + }, + "pg", + "postgresql", + "123", + "default"); + /// 2.2 insert + clientPtr->execSqlAsync( + "insert into users \ + (user_id,user_name,password,org_name) \ + values($1,$2,$3,$4)", + [](const Result &r) { + // std::cout << "id=" << r[0]["id"].as() << std::endl; + testOutput(r.affectedRows() == 1, + "DbClient asynchronous interface(1)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(1)"); + }, + "pg1", + "postgresql1", + "123", + "default"); + /// 2.3 query + clientPtr->execSqlAsync( + "select * from users where 1 = 1", + [](const Result &r) { + testOutput(r.size() == 2, "DbClient asynchronous interface(2)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(2)"); + }); + /// 2.2 query, parameter binding + clientPtr->execSqlAsync( + "select * from users where id = $1", + [](const Result &r) { + testOutput(r.size() == 0, "DbClient asynchronous interface(3)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(3)"); + }, + 1); + /// 2.3 query, parameter binding + clientPtr->execSqlAsync( + "select * from users where user_id = $1 and user_name = $2", + [](const Result &r) { + testOutput(r.size() == 1, "DbClient asynchronous interface(4)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(4)"); + }, + "pg1", + "postgresql1"); + /// 2.4 delete + clientPtr->execSqlAsync( + "delete from users where user_id = $1 and user_name = $2", + [](const Result &r) { + testOutput(r.affectedRows() == 1, + "DbClient asynchronous interface(5)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(5)"); + }, + "pg1", + "postgresql1"); + /// 2.5 update + clientPtr->execSqlAsync( + "update users set user_id = $1, user_name = $2 where user_id " + "= $3 and user_name = $4", + [](const Result &r) { + testOutput(r.affectedRows() == 1, + "DbClient asynchronous interface(6)"); + }, + [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(6)"); + }, + "pg1", + "postgresql1", + "pg", + "postgresql"); + /// 2.6 truncate + clientPtr->execSqlAsync( + "truncate table users", + [](const Result &r) { + testOutput(true, "DbClient asynchronous interface(7)"); + }, + [](const DrogonDbException &e) { + std::cerr << "error:" << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(7)"); + }); + + /// Test synchronous method + /// 3.1 insert + try + { + auto r = clientPtr->execSqlSync( + "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4) returning *", + "pg", + "postgresql", + "123", + "default"); + testOutput(r[0]["id"].as() == 5, + "DbClient synchronous interface(0)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(0)"); + } + /// 3.2 insert + try + { + auto r = clientPtr->execSqlSync( + "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4)", + "pg1", + "postgresql1", + "123", + "default"); + testOutput(r.affectedRows() == 1, "DbClient synchronous interface(1)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(1)"); + } + /// 3.3 query + try + { + auto r = clientPtr->execSqlSync( + "select * from users where user_id=$1 and user_name=$2", + "pg1", + "postgresql1"); + testOutput(r.size() == 1, "DbClient synchronous interface(2)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(2)"); + } + /// 3.4 query for none + try + { + auto r = clientPtr->execSqlSync( + "select * from users where user_id=$1 and user_name=$2", + "pg111", + "postgresql1"); + testOutput(r.size() == 0, "DbClient synchronous interface(3)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient asynchronous interface(3)"); + } + /// 3.5 bad sql + try + { + auto r = clientPtr->execSqlSync( + "select * from users where user_id=$1 and user_name='12'", + "pg111", + "postgresql1"); + testOutput(r.size() == 0, "DbClient synchronous interface(4)"); + } + catch (const DrogonDbException &e) + { + // std::cerr << e.base().what() << std::endl; + testOutput(true, "DbClient asynchronous interface(4)"); + } + /// 3.6 truncate + try + { + auto r = clientPtr->execSqlSync("truncate table users"); + testOutput(true, "DbClient synchronous interface(5)"); + } + catch (const DrogonDbException &e) + { + // std::cerr << e.base().what() << std::endl; + testOutput(true, "DbClient asynchronous interface(5)"); + } + /// Test future interface + /// 4.1 insert + auto f = clientPtr->execSqlAsyncFuture( + "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4) returning *", + "pg", + "postgresql", + "123", + "default"); + try + { + auto r = f.get(); + testOutput(r[0]["id"].as() == 7, + "DbClient future interface(0)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient future interface(0)"); + } + /// 4.2 insert + f = clientPtr->execSqlAsyncFuture( + "insert into users (user_id,user_name,password,org_name) " + "values($1,$2,$3,$4)", + "pg1", + "postgresql1", + "123", + "default"); + try + { + auto r = f.get(); + testOutput(r.affectedRows() == 1, "DbClient future interface(1)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient future interface(1)"); + } + /// 4.3 query + f = clientPtr->execSqlAsyncFuture( + "select * from users where user_id=$1 and user_name=$2", + "pg1", + "postgresql1"); + try + { + auto r = f.get(); + testOutput(r.size() == 1, "DbClient future interface(2)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient future interface(2)"); + } + /// 4.4 query for none + f = clientPtr->execSqlAsyncFuture( + "select * from users where user_id=$1 and user_name=$2", + "pg111", + "postgresql1"); + try + { + auto r = f.get(); + testOutput(r.size() == 0, "DbClient future interface(3)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient future interface(3)"); + } + /// 4.5 bad sql + f = clientPtr->execSqlAsyncFuture( + "select * from users where user_id=$1 and user_name='12'", + "pg111", + "postgresql1"); + try + { + auto r = f.get(); + testOutput(r.size() == 0, "DbClient future interface(4)"); + } + catch (const DrogonDbException &e) + { + // std::cerr << e.base().what() << std::endl; + testOutput(true, "DbClient future interface(4)"); + } + /// 4.6 truncate + f = clientPtr->execSqlAsyncFuture("truncate table users"); + try + { + auto r = f.get(); + testOutput(true, "DbClient future interface(5)"); + } + catch (const DrogonDbException &e) + { + // std::cerr << e.base().what() << std::endl; + testOutput(true, "DbClient future interface(5)"); + } + globalf.get(); + return 0; }