Skip to content

Commit

Permalink
Fix UpdateKey errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrognor committed Nov 30, 2024
1 parent 7e59c62 commit ceb6cc1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Source/Vault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,21 @@ namespace mvlt
// Check it is trying to set coorect type
if (res.RequestedType != keyTypeIt->second)
{
res.RequestedType = keyTypeIt->second;
res.SavedType = keyTypeIt->second;
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::WrongType;
return res;
}

// Check if it is unique key
if (UniqueKeys.find(key) != UniqueKeys.end())
{
res.SavedType = keyTypeIt->second;
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::TryToUpdateUniqueKey;
return res;
}

// Change data in template
RecordTemplate.SetData(key, defaultKeyValue);

Expand Down
3 changes: 3 additions & 0 deletions Source/VaultOperationResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace mvlt
switch (ResultCode)
{
default:
case VaultOperationResultCode::TryToUpdateUniqueKey:
return "The key is unique and you can not update it default value.";

case VaultOperationResultCode::DataRecordNotValid:
return "The record referenced by VaultRecordRef is not valid.";

Expand Down
1 change: 1 addition & 0 deletions Source/VaultOperationResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace mvlt
/// \brief Enum with all error handling codes
enum class VaultOperationResultCode
{
TryToUpdateUniqueKey = -7, ///< This code is returned when trying to update default value in an unique key
RecordAlredyInSet = -6, ///< This code is returned when trying to add an record to the set when it is already in it
ParentVaultNotMatch = -5, ///< This code is returned during operations on VaultRecordSet when objects depend on different Vaults
ParentVaultNotValid = -4, ///< This code is returned when calling VaultRecordSet operations when the parent Vault is not valid
Expand Down
7 changes: 6 additions & 1 deletion Tests/VaultUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,20 @@ void Vault_UpdateKey_Test()

vlt.AddKey("A", -1);
vlt.AddKey<std::string>("B", "none");
vlt.AddUniqueKey<std::size_t>("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> bool { return counter; }});

// Correct key update
TEST_ASSERT(vlt.UpdateKey("A", 0).IsOperationSuccess == true, "Failed to update key");

// Wrong key update
TEST_ASSERT(vlt.UpdateKey("C", -1).ResultCode == VaultOperationResultCode::WrongKey, "Cannot update not existing key");
TEST_ASSERT(vlt.UpdateKey("D", -1).ResultCode == VaultOperationResultCode::WrongKey, "Cannot update not existing key");

// Wrong key type
TEST_ASSERT(vlt.UpdateKey("B", -1).ResultCode == VaultOperationResultCode::WrongType, "Cannot set incorrect type to new key value");

// Try to update unique key
VaultOperationResult vrs = vlt.UpdateKey<std::size_t>("C", 250);
TEST_ASSERT(vrs.ResultCode == VaultOperationResultCode::TryToUpdateUniqueKey, "Cannot update unique key");
}

void Vault_IsKeyExist_Test()
Expand Down

0 comments on commit ceb6cc1

Please sign in to comment.