diff --git a/.travis.yml b/.travis.yml index dc120496..34296090 100644 --- a/.travis.yml +++ b/.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 diff --git a/build.sh b/build.sh index 792fe2d2..99093f11 100755 --- a/build.sh +++ b/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 diff --git a/orm_lib/tests/db_test.cc b/orm_lib/tests/db_test.cc index d659cef9..57fa39d9 100644 --- a/orm_lib/tests/db_test.cc +++ b/orm_lib/tests/db_test.cc @@ -18,10 +18,26 @@ #include #include #include +#include + 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 pro; +auto f = pro.get_future(); + +void addCount(int &count, std::promise &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() << " " - << r[i]["user_id"].as() << " " - << r[i]["user_name"].as() << 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() << " " - << item["user_id"].as() << " " - << item["user_name"].as() << 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; } diff --git a/test.sh b/test.sh index ea073696..3362a795 100755 --- a/test.sh +++ b/test.sh @@ -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