Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Run tests against actual InfluxDB instance (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Nov 21, 2019
1 parent 25f06e0 commit e844661
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 18 deletions.
38 changes: 23 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
6 changes: 6 additions & 0 deletions include/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ class Point
/// Sets custom timestamp
Point&& setTimestamp(std::chrono::time_point<std::chrono::system_clock> timestamp);

/// Name getter
std::string getName() const;

/// Timestamp getter
std::chrono::time_point<std::chrono::system_clock> getTimestamp() const;

/// Fields getter
std::string getFields() const;

protected:
/// A value
std::variant<int, std::string, double> mValue;
Expand Down
3 changes: 2 additions & 1 deletion src/InfluxDB.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ void InfluxDB::write(Point&& metric)
std::vector<Point> InfluxDB::query(const std::string& query)
{
auto response = mTransport->query(query);
std::cout << response << std::endl;
std::stringstream ss;
ss << response;
std::vector<Point> points;
boost::property_tree::ptree pt;
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");

Expand Down
15 changes: 15 additions & 0 deletions src/Point.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ std::string Point::toLineProtocol() const
);
}

std::string Point::getName() const
{
return mMeasurement;
}

std::chrono::time_point<std::chrono::system_clock> Point::getTimestamp() const
{
return mTimestamp;
}

std::string Point::getFields() const
{
return mFields;
}

} // namespace influxdb
27 changes: 27 additions & 0 deletions test/testHttp.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#define BOOST_TEST_MODULE Test InfluxDB HTTP
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#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
23 changes: 23 additions & 0 deletions test/testQuery.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define BOOST_TEST_MODULE Test InfluxDB Query
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#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
2 changes: 1 addition & 1 deletion test/testFactory.cxx → test/testUdp.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define BOOST_TEST_MODULE Test InfluxDB Factory
#define BOOST_TEST_MODULE Test InfluxDB UDP
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

Expand Down

0 comments on commit e844661

Please sign in to comment.