mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 03:38:03 +08:00
Add database tests in the travis CI (#214)
This commit is contained in:
parent
a6e2d4f007
commit
10db494556
25
.travis.yml
25
.travis.yml
@ -1,13 +1,12 @@
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
osx_image: xcode10.1
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
dist: xenial
|
||||
- os: osx
|
||||
osx_image: xcode11
|
||||
|
||||
sudo: required
|
||||
|
||||
dist: xenial
|
||||
|
||||
language: cpp
|
||||
|
||||
addons:
|
||||
@ -37,10 +36,16 @@ addons:
|
||||
- cmake
|
||||
- libtool
|
||||
- lz4
|
||||
- postgresql
|
||||
- mariadb
|
||||
- sqlite3
|
||||
update: true
|
||||
before_script:
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then
|
||||
brew services restart postgresql;
|
||||
sleep 2;
|
||||
psql -c 'create user postgres superuser;' postgres;
|
||||
fi
|
||||
|
||||
services:
|
||||
- postgresql
|
||||
script:
|
||||
- ./build.sh && ./test.sh
|
||||
- ./build.sh -t && ./test.sh -t
|
||||
|
14
build.sh
14
build.sh
@ -24,8 +24,12 @@ function build_drogon() {
|
||||
cd $build_dir
|
||||
|
||||
echo "Start building drogon ..."
|
||||
cmake ..
|
||||
|
||||
if [ $1 -eq 1 ]; then
|
||||
cmake .. -DMAKETEST=YES
|
||||
else
|
||||
cmake ..
|
||||
fi
|
||||
|
||||
#If errors then exit
|
||||
if [ "$?" != "0" ]; then
|
||||
exit -1
|
||||
@ -46,4 +50,8 @@ function build_drogon() {
|
||||
#Ok!
|
||||
}
|
||||
|
||||
build_drogon
|
||||
if [ "$1" = "-t" ]; then
|
||||
build_drogon 1
|
||||
else
|
||||
build_drogon 0
|
||||
fi
|
||||
|
@ -18,10 +18,26 @@
|
||||
#include <iostream>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace drogon::orm;
|
||||
#define RESET "\033[0m"
|
||||
#define RED "\033[31m" /* Red */
|
||||
#define GREEN "\033[32m" /* Green */
|
||||
#define TEST_COUNT 7
|
||||
|
||||
int counter = 0;
|
||||
std::promise<int> pro;
|
||||
auto f = pro.get_future();
|
||||
|
||||
void addCount(int &count, std::promise<int> &pro)
|
||||
{
|
||||
++count;
|
||||
if (count == TEST_COUNT)
|
||||
{
|
||||
pro.set_value(1);
|
||||
}
|
||||
}
|
||||
|
||||
void testOutput(bool isGood, const std::string &testMessage)
|
||||
{
|
||||
@ -29,6 +45,7 @@ void testOutput(bool isGood, const std::string &testMessage)
|
||||
{
|
||||
std::cout << GREEN << testMessage << "\t\tOK\n";
|
||||
std::cout << RESET;
|
||||
addCount(counter, pro);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -43,13 +60,14 @@ int main()
|
||||
trantor::Logger::setLogLevel(trantor::Logger::DEBUG);
|
||||
#if USE_POSTGRESQL
|
||||
auto clientPtr = DbClient::newPgClient(
|
||||
"host=127.0.0.1 port=5432 dbname=postgres user=antao", 1);
|
||||
"host=127.0.0.1 port=5432 dbname=postgres user=postgres", 1);
|
||||
#endif
|
||||
LOG_DEBUG << "start!";
|
||||
sleep(1);
|
||||
// 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)");
|
||||
@ -112,36 +130,49 @@ int main()
|
||||
/// 1.3 query,no-blocking
|
||||
*clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >>
|
||||
[](const Result &r) {
|
||||
for (Result::size_type i = 0; i < r.size(); ++i)
|
||||
{
|
||||
std::cout << r[i]["id"].as<int64_t>() << " "
|
||||
<< r[i]["user_id"].as<std::string>() << " "
|
||||
<< r[i]["user_name"].as<std::string>() << std::endl;
|
||||
}
|
||||
testOutput(true, "DbClient streaming-type interface(0)");
|
||||
if (r.size() == 2)
|
||||
testOutput(true, "DbClient streaming-type interface(2)");
|
||||
else
|
||||
testOutput(false, "DbClient streaming-type interface(2)");
|
||||
} >>
|
||||
[](const DrogonDbException &e) {
|
||||
std::cerr << e.base().what() << std::endl;
|
||||
testOutput(false, "DbClient streaming-type interface(0)");
|
||||
testOutput(false, "DbClient streaming-type interface(2)");
|
||||
};
|
||||
/// 1.4 query,blocking
|
||||
*clientPtr << "select * from users where 1 = 1" << Mode::Blocking >>
|
||||
[](const Result &r) {
|
||||
for (const auto &item : r)
|
||||
{
|
||||
std::cout << item["id"].as<int64_t>() << " "
|
||||
<< item["user_id"].as<std::string>() << " "
|
||||
<< item["user_name"].as<std::string>() << std::endl;
|
||||
}
|
||||
testOutput(true, "DbClient streaming-type interface(1)");
|
||||
if (r.size() == 2)
|
||||
testOutput(true, "DbClient streaming-type interface(3)");
|
||||
else
|
||||
testOutput(false, "DbClient streaming-type interface(3)");
|
||||
} >>
|
||||
[](const DrogonDbException &e) {
|
||||
std::cerr << e.base().what() << std::endl;
|
||||
testOutput(false, "DbClient streaming-type interface(1)");
|
||||
testOutput(false, "DbClient streaming-type interface(3)");
|
||||
};
|
||||
|
||||
/// 2 DbClient execSqlAsync()...
|
||||
///
|
||||
/// Model and Mapper....
|
||||
getchar();
|
||||
/// 1.5 query,blocking
|
||||
int count = 0;
|
||||
*clientPtr << "select user_name, user_id, id from users where 1 = 1"
|
||||
<< Mode::Blocking >>
|
||||
[&count](bool isNull,
|
||||
const std::string &name,
|
||||
std::string user_id,
|
||||
int id) {
|
||||
if (!isNull)
|
||||
count++;
|
||||
else
|
||||
{
|
||||
if (count == 2)
|
||||
testOutput(true, "DbClient streaming-type interface(4)");
|
||||
else
|
||||
testOutput(false, "DbClient streaming-type interface(4)");
|
||||
}
|
||||
} >>
|
||||
[](const DrogonDbException &e) {
|
||||
std::cerr << e.base().what() << std::endl;
|
||||
testOutput(false, "DbClient streaming-type interface(4)");
|
||||
};
|
||||
f.get();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user