Skip to content

Commit

Permalink
Added transaction initalization to tests
Browse files Browse the repository at this point in the history
Signed-off-by: Coury Richards <[email protected]>
  • Loading branch information
couryrr-afs committed Feb 5, 2024
1 parent f23c545 commit 86dabce
Showing 1 changed file with 203 additions and 12 deletions.
215 changes: 203 additions & 12 deletions tests/lib/ocpp/v16/test_smart_charging_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <filesystem>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
namespace fs = std::filesystem;

#include <database_handler_mock.hpp>
Expand Down Expand Up @@ -45,8 +46,13 @@ class ChargepointTestFixture : public testing::Test {
void SetUp() override {
}

void addConnector(int id){
connectors[id] = std::make_shared<Connector>(Connector{id});
void addConnector(int id) {
auto connector = Connector{id};

auto timer = std::unique_ptr<Everest::SteadyTimer>();

connector.transaction = std::make_shared<Transaction>(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer));
connectors[id] = std::make_shared<Connector>(connector);
}

ChargingSchedule createChargeSchedule() {
Expand All @@ -63,9 +69,10 @@ class ChargepointTestFixture : public testing::Test {
}

ChargingProfile createChargingProfile(std::optional<int> id, ChargingSchedule chargingSchedule) {
// Is this working?
auto chargingProfileId = 1;
if (id) {
auto chargingProfileId = id.value();
chargingProfileId = id.value();
}
auto stackLevel = 1;
auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
Expand Down Expand Up @@ -581,20 +588,31 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileId_Che
addConnector(connector_id);

auto handler = createSmartChargingHandler();

auto profile_max = createChargingProfile(9, createChargeSchedule(ChargingRateUnit::A));

auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_1.stackLevel = 1;
profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
profile_1.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A));


profile_2.stackLevel = 2;
profile_2.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
profile_2.chargingProfileKind = ChargingProfileKindType::Absolute;

handler->add_tx_default_profile(profile_1, connector_id);
handler->add_tx_default_profile(profile_2, connector_id);


auto profiles = handler->get_valid_profiles({}, {}, connector_id);

ASSERT_EQ(2, profiles.size());


bool sut = handler->clear_all_profiles_with_filter(1, {}, {}, {}, true);
profiles = handler->get_valid_profiles({}, {}, connector_id);

ASSERT_EQ(1, profiles.size());
ASSERT_TRUE(sut);

}

TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) {
Expand All @@ -603,15 +621,188 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingPro

auto handler = createSmartChargingHandler();

auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A));
auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_1.stackLevel = 1;
profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
profile_1.chargingProfileKind = ChargingProfileKindType::Absolute;

handler->add_tx_default_profile(profile, connector_id);
handler->add_tx_default_profile(profile, connector_id);
auto profile_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A));
profile_2.stackLevel = 2;
profile_2.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
profile_2.chargingProfileKind = ChargingProfileKindType::Absolute;

bool sut = handler->clear_all_profiles_with_filter(2, {}, {}, {}, true);
handler->add_tx_default_profile(profile_1, connector_id);
handler->add_tx_default_profile(profile_2, connector_id);

auto profiles = handler->get_valid_profiles({}, {}, connector_id);

ASSERT_EQ(2, profiles.size());

bool sut = handler->clear_all_profiles_with_filter(3, {}, {}, {}, true);
profiles = handler->get_valid_profiles({}, {}, connector_id);

ASSERT_EQ(2, profiles.size());
ASSERT_FALSE(sut);
}

//FIXME
/*
Bad cases
Data is
connector 100
pid 1
pid 2
connector 200
pid 1
pid 2
if called clear profiles with filter the following happens
pid 1 connector 100
connector 100 should have 1 profile(s) remaining
connector 200 should have 2 profile(s) remaining
currently tests shows
connector 100 having 0
connector 200 having 1
flipped is the same
*/
TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileIdAndConnector_CheckIdOnly__ReturnsTrue) {
const int connector_id_1 = 100;
addConnector(connector_id_1);

const int connector_id_2 = 200;
addConnector(connector_id_2);

auto handler = createSmartChargingHandler();

auto profile_c1_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_c1_1.stackLevel = 1;
profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c1_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A));
profile_c1_2.stackLevel = 2;
profile_c1_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c1_2.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c2_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_c2_1.stackLevel = 1;
profile_c2_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c2_1.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c2_2 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_c2_2.stackLevel = 2;
profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute;

handler->add_tx_profile(profile_c1_1, connector_id_1);
handler->add_tx_profile(profile_c1_2, connector_id_1);

handler->add_tx_profile(profile_c2_1, connector_id_2);
handler->add_tx_profile(profile_c2_2, connector_id_2);

auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1);
auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2);

ASSERT_EQ(2, connector_id_1_profiles.size());
ASSERT_EQ(2, connector_id_2_profiles.size());

auto check_id_only = false;

bool sut = handler->clear_all_profiles_with_filter(1, connector_id_1, std::nullopt, std::nullopt, check_id_only);

connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1);
connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2);

ASSERT_EQ(1, connector_id_1_profiles.size()); // Deleted everything on connector id 100
ASSERT_EQ(2, connector_id_2_profiles.size()); // Deleted everything on connector id 200
ASSERT_TRUE(sut);

}

//FIXME
/*
Bad cases
Broke each profile into a diff variable to confirm that I was not deleting shared ptrs...
Data is
connector 100
pid 1
pid 2
connector 200
pid 3
pid 4
if called clear profiles with filter the following happens
pid 999 connector 100
connector 100 should have 2 profile(s) remaining
connector 200 should have 2 profile(s) remaining
currently tests shows
connector 100 having 0
connector 200 having 2????????????
*/

TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__WithProfileIdMatchingAndConnector_CheckIdOnly__ReturnsTrue) {
const int connector_id_1 = 100;
addConnector(connector_id_1);

const int connector_id_2 = 200;
addConnector(connector_id_2);

auto handler = createSmartChargingHandler();

auto profile_c1_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_c1_1.stackLevel = 1;
profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c1_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A));
profile_c1_2.stackLevel = 2;
profile_c1_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c1_2.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c2_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A));
profile_c2_1.stackLevel = 1;
profile_c2_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c2_1.chargingProfileKind = ChargingProfileKindType::Absolute;

auto profile_c2_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A));
profile_c2_2.stackLevel = 2;
profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute;

handler->add_tx_profile(profile_c1_1, connector_id_1);
handler->add_tx_profile(profile_c1_2, connector_id_1);

handler->add_tx_profile(profile_c2_1, connector_id_2);
handler->add_tx_profile(profile_c2_2, connector_id_2);

auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1);
auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2);

ASSERT_EQ(2, connector_id_1_profiles.size());
ASSERT_EQ(2, connector_id_2_profiles.size());

auto check_id_only = false;

bool sut = handler->clear_all_profiles_with_filter(999, connector_id_1, std::nullopt, std::nullopt, check_id_only);

connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1);
connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2);

ASSERT_EQ(2, connector_id_1_profiles.size()); // Deleted everything on connector id 100
ASSERT_EQ(2, connector_id_2_profiles.size()); // Deleted everything on connector id 100
ASSERT_TRUE(sut);

}


} // namespace v16
} // namespace ocpp

0 comments on commit 86dabce

Please sign in to comment.