Skip to content

Commit 6093472

Browse files
committed
Add a unit test file for price plan service
1 parent a0bfdb7 commit 6093472

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

rest/domain/PricePlan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#ifndef DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLAN_H
22
#define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLAN_H
33

4+
#include <algorithm>
5+
#include <chrono>
46
#include <string>
57
#include <utility>
68
#include <vector>
7-
#include <chrono>
89

910
class PricePlan {
1011
// todo:

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ target_sources(domain_test PRIVATE domain/PricePlanTest.cpp)
1919
target_link_libraries(domain_test PRIVATE GTest::gmock_main rest)
2020

2121
add_executable(service_test)
22-
target_sources(service_test PRIVATE service/MeterReadingServiceTest.cpp)
22+
target_sources(service_test PRIVATE service/MeterReadingServiceTest.cpp service/PricePlanServiceTest.cpp)
2323
target_link_libraries(service_test PRIVATE GTest::gmock_main rest)
2424

2525
add_test(endpoint_test endpoint_test)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <configuration.h>
2+
#include <date/date.h>
3+
#include <gmock/gmock.h>
4+
#include <rest/service/PricePlanService.h>
5+
6+
#include <sstream>
7+
8+
struct PricePlanServiceTest : public ::testing::Test {
9+
PricePlanServiceTest() : meterAssociatedReadings_(setupReadings()), price_plans_(setupPricePlans()) {}
10+
11+
std::unordered_map<std::string, std::vector<ElectricityReading>> meterAssociatedReadings_;
12+
MeterReadingService meterReadingService_{meterAssociatedReadings_};
13+
14+
std::vector<PricePlan> price_plans_;
15+
PricePlanService pricePlanService_{price_plans_, meterReadingService_};
16+
17+
static std::unordered_map<std::string, std::vector<ElectricityReading>> setupReadings() {
18+
return {
19+
{
20+
"meter-0", {
21+
createElectricityReading("2021-08-18T06:42:15.725202Z", 20),
22+
createElectricityReading("2021-08-20T06:42:15.725202Z", 30)
23+
}
24+
},
25+
{
26+
"meter-1", {
27+
createElectricityReading("2021-12-10T06:42:15Z", 30),
28+
createElectricityReading("2021-12-09T06:42:15Z", 40),
29+
}
30+
}
31+
};
32+
}
33+
34+
static std::vector<PricePlan> setupPricePlans() { return {PricePlan("plan-0", "company-0", 3, {})}; }
35+
36+
using time_point_type = ElectricityReading::time_point_type;
37+
38+
static time_point_type parseRfc3339(std::string const &rfc3339) {
39+
time_point_type tp;
40+
std::istringstream ss(rfc3339);
41+
ss >> date::parse("%FT%TZ", tp);
42+
return tp;
43+
}
44+
45+
static ElectricityReading createElectricityReading(std::string const &rfc3339, std::size_t reading) {
46+
return ElectricityReading(parseRfc3339(rfc3339), reading);
47+
}
48+
};
49+
50+
TEST_F(PricePlanServiceTest, GetConsumptionCostShouldReturnNullGivenMeterIdThatDoesNotExist) {
51+
EXPECT_FALSE(pricePlanService_.getConsumptionCostOfElectricityReadingsForEachPricePlan("unknown-id"));
52+
}
53+
54+
TEST_F(PricePlanServiceTest, GetConsumptionCostShouldReturnValidValuesForEachPlanGivenMeterIdExists) {
55+
auto const &result = pricePlanService_.getConsumptionCostOfElectricityReadingsForEachPricePlan("meter-1");
56+
EXPECT_TRUE(result.has_value());
57+
58+
auto cost = result->at("plan-0");
59+
EXPECT_EQ(cost, 3);
60+
}

0 commit comments

Comments
 (0)