Modify the test of postgresql database (#215)

This commit is contained in:
An Tao 2019-08-17 12:40:39 +08:00 committed by GitHub
parent 10db494556
commit bf88d08f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 374 additions and 22 deletions

View File

@ -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>(drogon::orm::Result &one,
drogon::orm::Result &two) noexcept
{
one.swap(two);
}
} // namespace std

View File

@ -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<int> pro;
auto f = pro.get_future();
auto globalf = pro.get_future();
void addCount(int &count, std::promise<int> &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<int64_t>() << std::endl;
testOutput(r[0]["id"].as<int64_t>() == 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<int64_t>() << 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<int64_t>() == 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<int64_t>() == 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;
}