Skip to content

Commit a5b9d03

Browse files
committed
Add PricePlanTest
1 parent 909978a commit a5b9d03

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

rest/domain/PricePlan.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,52 @@
22
#define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLAN_H
33

44
#include <string>
5+
#include <utility>
56
#include <vector>
67

78
class PricePlan {
89
// todo:
910
public:
11+
using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
1012
class PeakTimeMultiplier {
13+
public:
1114
enum DayOfWeek {
15+
SUNDAY,
1216
MONDAY,
1317
TUESDAY,
18+
WEDNESDAY,
19+
THURSDAY,
20+
FRIDAY,
21+
SATURDAY
1422
};
1523

24+
PeakTimeMultiplier(DayOfWeek dayOfWeek, int multiplier) : dayOfWeek(dayOfWeek), multiplier(multiplier) {}
25+
1626
DayOfWeek dayOfWeek;
1727
int multiplier;
18-
19-
PeakTimeMultiplier(DayOfWeek dayOfWeek, int multiplier) : dayOfWeek(dayOfWeek), multiplier(multiplier) {}
2028
};
2129

2230
PricePlan(std::string planName, std::string energySupplier, int unitRate, std::vector<PeakTimeMultiplier> peakTimeMultipliers)
23-
: planName(planName), energySupplier(energySupplier), unitRate(unitRate), peakTimeMultipliers(peakTimeMultipliers) {}
31+
: planName(std::move(planName)), energySupplier(std::move(energySupplier)), unitRate(unitRate), peakTimeMultipliers(std::move(peakTimeMultipliers)) {}
2432

2533
std::string getEnergySupplier() const { return energySupplier; }
2634

2735
std::string getPlanName() const { return planName; }
2836

2937
int getUnitRate() const { return unitRate; }
3038

39+
int getPrice(time_point_type dateTime) const {
40+
auto time_t_dateTime = std::chrono::system_clock::to_time_t(dateTime);
41+
auto t = std::localtime(&time_t_dateTime);
42+
auto it = std::find_if(peakTimeMultipliers.begin(), peakTimeMultipliers.end(), [=](auto &p) {
43+
return p.dayOfWeek == t->tm_wday;
44+
});
45+
if (it == peakTimeMultipliers.end()) {
46+
return unitRate;
47+
}
48+
return unitRate * it->multiplier;
49+
}
50+
3151
private:
3252
const std::string energySupplier;
3353
const std::string planName;

test/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ add_executable(controller_test)
88
target_sources(controller_test PRIVATE controller/MeterReadingControllerTest.cpp controller/PricePlanComparatorControllerTest.cpp)
99
target_link_libraries(controller_test PRIVATE GTest::gmock_main nlohmann_json::nlohmann_json rest)
1010

11+
add_executable(domain_test)
12+
target_sources(domain_test PRIVATE domain/PricePlanTest.cpp)
13+
target_link_libraries(domain_test PRIVATE GTest::gmock_main rest)
14+
1115
add_test(endpoint_test endpoint_test)
12-
add_test(controller_test controller_test)
16+
add_test(controller_test controller_test)
17+
add_test(domain_test domain_test)

test/domain/PricePlanTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <gmock/gmock.h>
2+
#include <rest/domain/PricePlan.h>
3+
#include <rest/controller/MeterReadingController.h>
4+
5+
using ::testing::Eq;
6+
7+
TEST(PricePlanTest, GetEnergySupplierShouldReturnTheEnergySupplierGivenInTheConstructor) {
8+
const std::string energy_supplier = "Energy Supplier Name";
9+
10+
PricePlan p("", energy_supplier, 0, {});
11+
12+
EXPECT_THAT(p.getEnergySupplier(), energy_supplier);
13+
}
14+
15+
TEST(PricePlanTest, GetPriceShouldReturnTheBasePriceGivenAnOrdinaryDateTime) {
16+
auto time = detail::fromRfc3339("2021-09-30T06:42:15.725202Z");
17+
PricePlan::PeakTimeMultiplier peak_time_multiplier(PricePlan::PeakTimeMultiplier::DayOfWeek::WEDNESDAY, 10);
18+
PricePlan price_plan("", "", 1, {peak_time_multiplier});
19+
20+
auto price = price_plan.getPrice(time);
21+
22+
EXPECT_THAT(price, 1);
23+
}
24+
25+
TEST(PricePlanTest, GetPriceShouldReturnAnExceptionPriceGivenExceptionalDateTime) {
26+
auto time = detail::fromRfc3339("2021-09-29T06:42:15.725202Z");
27+
PricePlan::PeakTimeMultiplier peak_time_multiplier(PricePlan::PeakTimeMultiplier::DayOfWeek::WEDNESDAY, 10);
28+
PricePlan price_plan("", "", 1, {peak_time_multiplier});
29+
30+
auto price = price_plan.getPrice(time);
31+
32+
EXPECT_THAT(price, 10);
33+
}
34+
35+
TEST(PricePlanTest, GetPriceShouldReceiveMultipleExceptionalDateTimes) {
36+
auto time = detail::fromRfc3339("2021-09-29T06:42:15.725202Z");
37+
PricePlan::PeakTimeMultiplier peak_time_multiplier(PricePlan::PeakTimeMultiplier::DayOfWeek::WEDNESDAY, 10);
38+
PricePlan::PeakTimeMultiplier another_peak_time_multiplier(PricePlan::PeakTimeMultiplier::DayOfWeek::THURSDAY, 10);
39+
PricePlan price_plan("", "", 1, {peak_time_multiplier, another_peak_time_multiplier});
40+
41+
auto price = price_plan.getPrice(time);
42+
43+
EXPECT_THAT(price, 10);
44+
}

0 commit comments

Comments
 (0)