Skip to content

Commit

Permalink
test - introduce unit tests for telemetry::AggMethodJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
SiskaPavel committed Apr 29, 2024
1 parent 990b054 commit a5b4420
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/telemetry/aggregator/aggJoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ Content AggMethodJoin::aggregate(const std::vector<Content>& contents)
}

} // namespace telemetry

#ifdef TELEMETRY_ENABLE_TESTS
#include "tests/testAggJoin.cpp"
#endif
107 changes: 107 additions & 0 deletions src/telemetry/aggregator/tests/testAggJoin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* @file
* @author Pavel Siska <[email protected]>
* @brief Unit tests of Telemetry::AggMethodJoin
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <telemetry/directory.hpp>

#include <gtest/gtest.h>

namespace telemetry {

TEST(AggJoinTest, TestAggregateGatheredValues)
{
{
std::vector<AggContent> values = {Scalar {5.0}, Scalar {10.0}, Scalar {15.0}};
ResultType result = aggregateGatheredValues(values);
EXPECT_EQ(result.size(), 3);
EXPECT_EQ(std::get<double>(result[0]), 5.0);
EXPECT_EQ(std::get<double>(result[1]), 10.0);
EXPECT_EQ(std::get<double>(result[2]), 15.0);
}

{
std::vector<AggContent> values
= {Array {Scalar {5.0}, Scalar {-5.0}}, Array {Scalar {10.0}}, Array {Scalar {15.0}}};
ResultType result = aggregateGatheredValues(values);
EXPECT_EQ(result.size(), 4);
EXPECT_EQ(std::get<double>(result[0]), 5.0);
EXPECT_EQ(std::get<double>(result[1]), -5.0);
EXPECT_EQ(std::get<double>(result[2]), 10.0);
EXPECT_EQ(std::get<double>(result[3]), 15.0);
}
}

TEST(AggJoinTest, TestAggregate)
{
{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents = {Scalar {5.0}, Scalar {10.0}, Scalar {15.0}};
Content content = aggMethodJoin.aggregate(contents);
EXPECT_TRUE(std::holds_alternative<Array>(content));
Array& array = std::get<Array>(content);
EXPECT_EQ(array.size(), 3);
EXPECT_EQ(std::get<double>(array[0]), 5.0);
EXPECT_EQ(std::get<double>(array[1]), 10.0);
EXPECT_EQ(std::get<double>(array[2]), 15.0);
}

{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents
= {ScalarWithUnit {5.0, "unit"}, ScalarWithUnit {5.0, "unit1"}};
EXPECT_THROW(aggMethodJoin.aggregate(contents), TelemetryException);
}

{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents = {ScalarWithUnit {5.0, "unit"}, Scalar {5.0}};
EXPECT_THROW(aggMethodJoin.aggregate(contents), TelemetryException);
}

{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents = {Scalar {true}, Scalar {5.0}};
EXPECT_THROW(aggMethodJoin.aggregate(contents), TelemetryException);
}

{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents = {Scalar {uint64_t(20)}, Scalar {5.0}};
EXPECT_THROW(aggMethodJoin.aggregate(contents), TelemetryException);
}

{
AggMethodJoin aggMethodJoin;
std::vector<Content> contents = {uint64_t(20), uint64_t {5}};
Content content = aggMethodJoin.aggregate(contents);
EXPECT_TRUE(std::holds_alternative<Array>(content));
Array& array = std::get<Array>(content);
EXPECT_EQ(array.size(), 2);
EXPECT_EQ(std::get<uint64_t>(array[0]), 20.0);
EXPECT_EQ(std::get<uint64_t>(array[1]), 5.0);
}

{
AggMethodJoin aggMethodJoin;
aggMethodJoin.setDictField("packets", "packetsSum");
std::vector<Content> contents
= {Dict({{"packets", Scalar {uint64_t(1)}}}),
Dict({{"packets", Scalar {uint64_t(5)}}})};
Content content = aggMethodJoin.aggregate(contents);
EXPECT_TRUE(std::holds_alternative<Dict>(content));

const Dict& dict = std::get<Dict>(content);
EXPECT_EQ(1, dict.size());

const Array& array = std::get<Array>(dict.at("packetsSum"));
EXPECT_EQ(array.size(), 2);
EXPECT_EQ(std::get<uint64_t>(array[0]), 1);
EXPECT_EQ(std::get<uint64_t>(array[1]), 5);
}
}

} // namespace telemetry

0 comments on commit a5b4420

Please sign in to comment.