Add database tests in the travis CI (#214)

This commit is contained in:
An Tao 2019-08-16 23:41:03 +08:00 committed by GitHub
parent a6e2d4f007
commit 10db494556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}

10
test.sh
View File

@ -103,5 +103,15 @@ fi
cd ../../
rm -rf drogon_test
if [ "$1" = "-t" ];then
echo "Test database"
cd ../
./orm_lib/tests/db_test
if [ $? -ne 0 ];then
echo "Error in testing"
exit -1
fi
fi
echo "Everything is ok!"
exit 0