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

Commit

Permalink
Add Point unit tests (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Jul 30, 2019
1 parent f8b8b59 commit 92c38b0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ if (Boost_FOUND)

set(TEST_SRCS
test/testFactory.cxx
test/testPoint.cxx
)

foreach (test ${TEST_SRCS})
Expand Down
4 changes: 2 additions & 2 deletions include/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class Point

/// Adds filed
Point&& addField(std::string_view name, std::variant<int, std::string, double> value);

/// Generetes current timestamp
static auto getCurrentTimestamp() -> decltype(std::chrono::system_clock::now());

/// Converts point to Influx Line Protocol
std::string toLineProtocol();
std::string toLineProtocol() const;

protected:
/// A value
Expand Down
4 changes: 2 additions & 2 deletions src/HTTP.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CURL* HTTP::initCurl(const std::string& url)
if (globalInitResult != CURLE_OK) {
throw std::runtime_error(std::string("cURL init") + curl_easy_strerror(globalInitResult));
}

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
Expand Down Expand Up @@ -56,7 +56,7 @@ void HTTP::deleteCurl(CURL * curl)

void HTTP::send(std::string&& post)
{
CURLcode response;
CURLcode response;
long responseCode;
curl_easy_setopt(curlHandle.get(), CURLOPT_POSTFIELDS, post.c_str());
curl_easy_setopt(curlHandle.get(), CURLOPT_POSTFIELDSIZE, (long) post.length());
Expand Down
14 changes: 2 additions & 12 deletions src/Point.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
namespace influxdb
{

/*
int Point::getType() const
{
if (std::holds_alternative<int>(mValue)) return 0;
else if (std::holds_alternative<std::string>(mValue)) return 1;
else if (std::holds_alternative<double>(mValue)) return 2;
else return 3;
}
*/

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

Expand All @@ -34,7 +24,7 @@ Point&& Point::addField(std::string_view name, std::variant<int, std::string, do
{
std::stringstream convert;
if (!mFields.empty()) convert << ",";

convert << name << "=";
std::visit(overloaded {
[&convert](int value) { convert << value << 'i'; },
Expand All @@ -59,7 +49,7 @@ auto Point::getCurrentTimestamp() -> decltype(std::chrono::system_clock::now())
return std::chrono::system_clock::now();
}

std::string Point::toLineProtocol()
std::string Point::toLineProtocol() const
{
return mMeasurement + mTags + " " + mFields + " " + std::to_string(
std::chrono::duration_cast <std::chrono::nanoseconds>(mTimestamp.time_since_epoch()).count()
Expand Down
2 changes: 1 addition & 1 deletion test/testFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BOOST_AUTO_TEST_CASE(test)
auto influxdb = influxdb::InfluxDBFactory::Get("udp://localhost:8084");
influxdb->write(Point{"test"}
.addField("value", 10)
.addTag("host", "pcaldadam")
.addTag("host", "adampc")
);
}

Expand Down
56 changes: 56 additions & 0 deletions test/testPoint.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#define BOOST_TEST_MODULE Test InfluxDB Point
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "../include/InfluxDBFactory.h"

namespace influxdb {
namespace test {


std::vector<std::string> getVector(const Point& point)
{
std::istringstream result(point.toLineProtocol());
return std::vector<std::string>{std::istream_iterator<std::string>{result},
std::istream_iterator<std::string>{}};
}

BOOST_AUTO_TEST_CASE(test1)
{
auto point = Point{"test"}
.addField("value", 10);

auto result = getVector(point);

BOOST_CHECK_EQUAL(result[0], "test");
BOOST_CHECK_EQUAL(result[1], "value=10i");
}

BOOST_AUTO_TEST_CASE(test2)
{
auto point = Point{"test"}
.addField("value", 10)
.addField("dvalue", 10.10);

auto result = getVector(point);

BOOST_CHECK_EQUAL(result[0], "test");
BOOST_CHECK_EQUAL(result[1], "value=10i,dvalue=10.1");
}

BOOST_AUTO_TEST_CASE(test3)
{
auto point = Point{"test"}
.addField("value", 10)
.addField("dvalue", 10.10)
.addTag("tag", "tagval");

auto result = getVector(point);

BOOST_CHECK_EQUAL(result[0], "test,tag=tagval");
BOOST_CHECK_EQUAL(result[1], "value=10i,dvalue=10.1");
}


} // namespace test
} // namespace influxdb

0 comments on commit 92c38b0

Please sign in to comment.