Skip to content

Commit

Permalink
added columns to database and updated equality checks
Browse files Browse the repository at this point in the history
Signed-off-by: Coury Richards <[email protected]>
  • Loading branch information
couryrr-afs committed Jul 30, 2024
1 parent 2045241 commit 881ecef
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 39 deletions.
2 changes: 2 additions & 0 deletions config/v201/core_migrations/5_up-charging_profiles_db.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CREATE TABLE CHARGING_PROFILES (

Check failure on line 1 in config/v201/core_migrations/5_up-charging_profiles_db.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

config/v201/core_migrations/5_up-charging_profiles_db.sql#L1

Expected SET ANSI_NULLS ON near top of file

Check failure on line 1 in config/v201/core_migrations/5_up-charging_profiles_db.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

config/v201/core_migrations/5_up-charging_profiles_db.sql#L1

Expected SET QUOTED_IDENTIFIER ON near top of file

Check failure on line 1 in config/v201/core_migrations/5_up-charging_profiles_db.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

config/v201/core_migrations/5_up-charging_profiles_db.sql#L1

Expected SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED near top of file

Check failure on line 1 in config/v201/core_migrations/5_up-charging_profiles_db.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

config/v201/core_migrations/5_up-charging_profiles_db.sql#L1

Expected table to use data compression
ID INT PRIMARY KEY NOT NULL,
EVSE_ID INT NOT NULL,
STACK_LEVEL INT NOT NULL,
CHARGING_PROFILE_PURPOSE TEXT NOT NULL,
PROFILE TEXT NOT NULL
);
6 changes: 6 additions & 0 deletions include/ocpp/v201/profile_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "ocpp/v201/ocpp_types.hpp"
namespace ocpp::v201 {

bool operator==(const ChargingProfile& lhs, const ChargingProfile& rhs);

} // namespace ocpp::v201
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ if(LIBOCPP_ENABLE_V201)
ocpp/v201/types.cpp
ocpp/v201/utils.cpp
ocpp/v201/component_state_manager.cpp
ocpp/v201/profile_utils.cpp
)
add_subdirectory(ocpp/v201/messages)
endif()
Expand Down
8 changes: 6 additions & 2 deletions lib/ocpp/v201/database_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,18 @@ void DatabaseHandler::transaction_delete(const std::string& transaction_id) {

void DatabaseHandler::insert_or_update_charging_profile(const int evse_id, const v201::ChargingProfile& profile) {
// add or replace
std::string sql = "INSERT OR REPLACE INTO CHARGING_PROFILES (ID, EVSE_ID, PROFILE) VALUES "
"(@id, @evse_id, @profile)";
std::string sql =
"INSERT OR REPLACE INTO CHARGING_PROFILES (ID, EVSE_ID, STACK_LEVEL, CHARGING_PROFILE_PURPOSE, PROFILE) VALUES "
"(@id, @evse_id, @stack_level, @charging_profile_purpose, @profile)";
auto stmt = this->database->new_statement(sql);

json json_profile(profile);

stmt->bind_int("@id", profile.id);
stmt->bind_int("@evse_id", evse_id);
stmt->bind_int("@stack_level", profile.stackLevel);
stmt->bind_text("@charging_profile_purpose",
conversions::charging_profile_purpose_enum_to_string(profile.chargingProfilePurpose));
stmt->bind_text("@profile", json_profile.dump(), SQLiteString::Transient);

if (stmt->step() != SQLITE_DONE) {
Expand Down
11 changes: 11 additions & 0 deletions lib/ocpp/v201/profile_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "ocpp/v201/profile_utils.hpp"
#include "ocpp/v201/ocpp_types.hpp"

namespace ocpp::v201 {

bool operator==(const ChargingProfile& lhs, const ChargingProfile& rhs) {
return lhs.chargingProfileKind == rhs.chargingProfileKind &&
lhs.chargingProfilePurpose == rhs.chargingProfilePurpose && lhs.id == rhs.id &&
lhs.stackLevel == rhs.stackLevel;
}
} // namespace ocpp::v201
106 changes: 75 additions & 31 deletions tests/lib/ocpp/v201/test_database_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright 2020 - 2024 Pionix GmbH and Contributors to EVerest

#include "database_testing_utils.hpp"
#include "ocpp/v201/enums.hpp"
#include "ocpp/v201/profile_utils.hpp"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <ocpp/v201/database_handler.hpp>
Expand Down Expand Up @@ -147,7 +149,9 @@ TEST_F(DatabaseHandlerTest, TransactionDeleteNotFound) {
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithNoData_InsertProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

auto sut = this->database_handler.get_all_charging_profiles_by_evse();

Expand All @@ -158,8 +162,12 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithNoData_InsertProfile) {
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_UpdateProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 2});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 2, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 2, .stackLevel = 2, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

std::string sql = "SELECT COUNT(*) FROM CHARGING_PROFILES";
auto select_stmt = this->database->new_statement(sql);
Expand All @@ -171,8 +179,12 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_UpdateProfile) {
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_InsertNewProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 2, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

std::string sql = "SELECT COUNT(*) FROM CHARGING_PROFILES";
auto select_stmt = this->database->new_statement(sql);
Expand All @@ -184,8 +196,12 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_InsertNewProfile) {
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_DeleteRemovesSpecifiedProfiles) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 2, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

auto sql = "SELECT COUNT(*) FROM CHARGING_PROFILES";

Expand All @@ -208,8 +224,12 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_DeleteRemovesSpecif
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithProfileData_DeleteAllRemovesAllProfiles) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 2, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

auto sql = "SELECT COUNT(*) FROM CHARGING_PROFILES";

Expand Down Expand Up @@ -250,7 +270,9 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithNoProfileData_DeleteAllDoesNotF
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithSingleProfileData_LoadsCharingProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(
1, ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile});

auto sut = this->database_handler.get_all_charging_profiles_by_evse();

Expand All @@ -265,9 +287,18 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithSingleProfileData_LoadsCharingP
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithMultipleProfileSameEvse_LoadsCharingProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 2});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 3, .stackLevel = 3});
auto p1 = ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};

this->database_handler.insert_or_update_charging_profile(1, p1);

auto p2 = ChargingProfile{
.id = 2, .stackLevel = 2, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};
this->database_handler.insert_or_update_charging_profile(1, p2);

auto p3 = ChargingProfile{
.id = 3, .stackLevel = 3, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};
this->database_handler.insert_or_update_charging_profile(1, p3);

auto sut = this->database_handler.get_all_charging_profiles_by_evse();

Expand All @@ -279,15 +310,34 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithMultipleProfileSameEvse_LoadsCh
auto profiles = sut[1];

EXPECT_EQ(profiles.size(), 3);
EXPECT_EQ(profiles[0], p1);
EXPECT_EQ(profiles[1], p2);
EXPECT_EQ(profiles[2], p3);
}

TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithMultipleProfileDiffEvse_LoadsCharingProfile) {
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 1, .stackLevel = 1});
this->database_handler.insert_or_update_charging_profile(1, ChargingProfile{.id = 2, .stackLevel = 2});
this->database_handler.insert_or_update_charging_profile(2, ChargingProfile{.id = 3, .stackLevel = 3});
this->database_handler.insert_or_update_charging_profile(2, ChargingProfile{.id = 4, .stackLevel = 4});
this->database_handler.insert_or_update_charging_profile(3, ChargingProfile{.id = 5, .stackLevel = 5});
this->database_handler.insert_or_update_charging_profile(3, ChargingProfile{.id = 6, .stackLevel = 6});
auto p1 = ChargingProfile{
.id = 1, .stackLevel = 1, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};
this->database_handler.insert_or_update_charging_profile(1, p1);

auto p2 =
ChargingProfile{.id = 2, .stackLevel = 2, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxProfile};
this->database_handler.insert_or_update_charging_profile(1, p2);

auto p3 = ChargingProfile{
.id = 3, .stackLevel = 3, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};
this->database_handler.insert_or_update_charging_profile(2, p3);
auto p4 =
ChargingProfile{.id = 4, .stackLevel = 4, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxProfile};
this->database_handler.insert_or_update_charging_profile(2, p4);

auto p5 = ChargingProfile{
.id = 5, .stackLevel = 5, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxDefaultProfile};
this->database_handler.insert_or_update_charging_profile(3, p5);

auto p6 =
ChargingProfile{.id = 6, .stackLevel = 6, .chargingProfilePurpose = ChargingProfilePurposeEnum::TxProfile};
this->database_handler.insert_or_update_charging_profile(3, p6);

auto sut = this->database_handler.get_all_charging_profiles_by_evse();

Expand All @@ -302,20 +352,14 @@ TEST_F(DatabaseHandlerTest, KO1_FR27_DatabaseWithMultipleProfileDiffEvse_LoadsCh
auto profiles3 = sut[3];

EXPECT_EQ(profiles1.size(), 2);
EXPECT_EQ(profiles1[0].id, 1);
EXPECT_EQ(profiles1[0].stackLevel, 1);
EXPECT_EQ(profiles1[1].id, 2);
EXPECT_EQ(profiles1[1].stackLevel, 2);
EXPECT_EQ(profiles1[0], p1);
EXPECT_EQ(profiles1[1], p2);

EXPECT_EQ(profiles2.size(), 2);
EXPECT_EQ(profiles2[0].id, 3);
EXPECT_EQ(profiles2[0].stackLevel, 3);
EXPECT_EQ(profiles2[1].id, 4);
EXPECT_EQ(profiles2[1].stackLevel, 4);
EXPECT_EQ(profiles2[0], p3);
EXPECT_EQ(profiles2[1], p4);

EXPECT_EQ(profiles3.size(), 2);
EXPECT_EQ(profiles3[0].id, 5);
EXPECT_EQ(profiles3[0].stackLevel, 5);
EXPECT_EQ(profiles3[1].id, 6);
EXPECT_EQ(profiles3[1].stackLevel, 6);
EXPECT_EQ(profiles3[0], p5);
EXPECT_EQ(profiles3[1], p6);
}
7 changes: 1 addition & 6 deletions tests/lib/ocpp/v201/test_smart_charging_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@
#include <ocpp/v201/smart_charging.hpp>
#include <optional>

#include "ocpp/v201/profile_utils.hpp"
#include <sstream>
#include <vector>

namespace ocpp::v201 {

bool operator==(const ChargingProfile& lhs, const ChargingProfile& rhs) {
return lhs.chargingProfileKind == rhs.chargingProfileKind &&
lhs.chargingProfilePurpose == rhs.chargingProfilePurpose && lhs.id == rhs.id &&
lhs.stackLevel == rhs.stackLevel;
}

static const int NR_OF_EVSES = 1;
static const int STATION_WIDE_ID = 0;
static const int DEFAULT_EVSE_ID = 1;
Expand Down

0 comments on commit 881ecef

Please sign in to comment.