|
| 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