From e84466135f73074263f4851c92795dbde0bd8689 Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Fri, 22 Nov 2019 07:44:58 +1030 Subject: [PATCH] Run tests against actual InfluxDB instance (#42) --- .travis.yml | 38 ++++++++++++++++----------- CMakeLists.txt | 4 ++- include/Point.h | 6 +++++ src/InfluxDB.cxx | 3 ++- src/Point.cxx | 15 +++++++++++ test/testHttp.cxx | 27 +++++++++++++++++++ test/testQuery.cxx | 23 ++++++++++++++++ test/{testFactory.cxx => testUdp.cxx} | 2 +- 8 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 test/testHttp.cxx create mode 100644 test/testQuery.cxx rename test/{testFactory.cxx => testUdp.cxx} (93%) diff --git a/.travis.yml b/.travis.yml index fffbe9b5..662a6dcf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,29 +1,37 @@ language: cpp +sudo: true matrix: include: - os: osx - osx_image: xcode10.2 + osx_image: xcode11.2 - os: linux - dist: xenial - addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - lcov - - gcc-7 - - g++-7 - - cmake - - libboost-system1.58-dev - - libboost-test1.58-dev - - libboost-program-options1.58-dev + dist: bionic + +before_install: + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -q update; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -y install wget lcov g++-7 cmake libboost-system1.65-dev libboost-test1.65-dev libboost-program-options1.65-dev libcurl4-openssl-dev; fi + +install: + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then wget https://dl.influxdata.com/influxdb/releases/influxdb_1.7.4_amd64.deb; fi; + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo dpkg -i influxdb_1.7.4_amd64.deb; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo systemctl unmask influxdb.service; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo systemctl start influxdb; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then HOMEBREW_NO_AUTO_UPDATE=1 brew install influxdb; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ln -sfv /usr/local/opt/influxdb/*.plist ~/Library/LaunchAgents; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then launchctl load ~/Library/LaunchAgents/homebrew.mxcl.influxdb.plist; fi + - sleep 10 + - influx -execute 'CREATE DATABASE test' before_script: - cd $TRAVIS_BUILD_DIR; mkdir build; cd build script: - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then cmake ..; else cmake .. -DCMAKE_C_COMPILER=/usr/bin/gcc-7 -DCMAKE_CXX_COMPILER=/usr/bin/g++-7 -DCMAKE_BUILD_TYPE=Debug; fi; - make -j - - make test + - influx -database "test" -execute "select * from test" + - ./bin/testHttp + - influx -database "test" -execute "select * from test" + - ./bin/testQuery after_success: - | if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then diff --git a/CMakeLists.txt b/CMakeLists.txt index da12e0da..7c0c34b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,8 +110,10 @@ if (Boost_FOUND) enable_testing() set(TEST_SRCS - test/testFactory.cxx + test/testUdp.cxx test/testPoint.cxx + test/testHttp.cxx + test/testQuery.cxx ) foreach (test ${TEST_SRCS}) diff --git a/include/Point.h b/include/Point.h index 1d759212..558adf9e 100644 --- a/include/Point.h +++ b/include/Point.h @@ -37,9 +37,15 @@ class Point /// Sets custom timestamp Point&& setTimestamp(std::chrono::time_point timestamp); + /// Name getter std::string getName() const; + + /// Timestamp getter std::chrono::time_point getTimestamp() const; + /// Fields getter + std::string getFields() const; + protected: /// A value std::variant mValue; diff --git a/src/InfluxDB.cxx b/src/InfluxDB.cxx index de4957de..47140cf8 100644 --- a/src/InfluxDB.cxx +++ b/src/InfluxDB.cxx @@ -76,7 +76,6 @@ void InfluxDB::write(Point&& metric) std::vector InfluxDB::query(const std::string& query) { auto response = mTransport->query(query); - std::cout << response << std::endl; std::stringstream ss; ss << response; std::vector points; @@ -84,6 +83,8 @@ std::vector InfluxDB::query(const std::string& query) boost::property_tree::read_json(ss, pt); for (auto& result : pt.get_child("results")) { + auto isResultEmpty = result.second.find("series"); + if (isResultEmpty == result.second.not_found()) return {}; for (auto& series : result.second.get_child("series")) { auto columns = series.second.get_child("columns"); diff --git a/src/Point.cxx b/src/Point.cxx index fc805180..fb0d069a 100644 --- a/src/Point.cxx +++ b/src/Point.cxx @@ -65,4 +65,19 @@ std::string Point::toLineProtocol() const ); } +std::string Point::getName() const +{ + return mMeasurement; +} + +std::chrono::time_point Point::getTimestamp() const +{ + return mTimestamp; +} + +std::string Point::getFields() const +{ + return mFields; +} + } // namespace influxdb diff --git a/test/testHttp.cxx b/test/testHttp.cxx new file mode 100644 index 00000000..0d241af2 --- /dev/null +++ b/test/testHttp.cxx @@ -0,0 +1,27 @@ +#define BOOST_TEST_MODULE Test InfluxDB HTTP +#define BOOST_TEST_DYN_LINK +#include + +#include "../include/InfluxDBFactory.h" + +namespace influxdb { +namespace test { + + + +BOOST_AUTO_TEST_CASE(write1) +{ + auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test"); + influxdb->write(Point{"test"} + .addField("value", 10) + .addTag("host", "localhost") + ); + + influxdb->write(Point{"test"} + .addField("value", 20) + .addTag("host", "localhost") + ); +} + +} // namespace test +} // namespace influxdb diff --git a/test/testQuery.cxx b/test/testQuery.cxx new file mode 100644 index 00000000..dedcef89 --- /dev/null +++ b/test/testQuery.cxx @@ -0,0 +1,23 @@ +#define BOOST_TEST_MODULE Test InfluxDB Query +#define BOOST_TEST_DYN_LINK +#include + +#include "../include/InfluxDBFactory.h" + +namespace influxdb { +namespace test { + + + +BOOST_AUTO_TEST_CASE(query1) +{ + auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test"); + auto points = influxdb->query("SELECT * from test LIMIT 2"); + BOOST_CHECK_EQUAL(points.front().getName(), "test"); + BOOST_CHECK_EQUAL(points.back().getName(), "test"); + BOOST_CHECK_EQUAL(points.front().getFields(), "value=10"); + BOOST_CHECK_EQUAL(points.back().getFields(), "value=20"); +} + +} // namespace test +} // namespace influxdb diff --git a/test/testFactory.cxx b/test/testUdp.cxx similarity index 93% rename from test/testFactory.cxx rename to test/testUdp.cxx index 7f65c895..d5f47d7b 100644 --- a/test/testFactory.cxx +++ b/test/testUdp.cxx @@ -1,4 +1,4 @@ -#define BOOST_TEST_MODULE Test InfluxDB Factory +#define BOOST_TEST_MODULE Test InfluxDB UDP #define BOOST_TEST_DYN_LINK #include