Skip to content

Commit

Permalink
Fix UpdateKey and GetKeyValue errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrognor committed Nov 30, 2024
1 parent 7e59c62 commit 9de22b7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
35 changes: 33 additions & 2 deletions Source/Vault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ namespace mvlt
// Set proper key order
vaultRecordSet.KeysOrder = KeysOrder;

// Set unique keys
vaultRecordSet.UniqueKeys = UniqueKeys;

res = RequestRecordsSet(requestType, key, beginKeyValue, endKeyValue, vaultRecordSet.RecordsSet, isIncludeBeginKeyValue, isIncludeEndKeyValue, amountOfRecords, requestPredicat);

for (VaultRecord* record : vaultRecordSet.RecordsSet)
Expand Down Expand Up @@ -527,7 +530,10 @@ namespace mvlt
{
for (VaultRecordSet* set : RecordSetsSet)
{
set->AddUniqueKey(key, uniqueKeyFunction);
if (isUniqueKey)
set->AddUniqueKey(key, uniqueKeyFunction);
else
set->AddKey(key, defaultKeyValue);
set->KeysOrder.emplace_back(key);
}
}
Expand Down Expand Up @@ -569,12 +575,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 Expand Up @@ -607,6 +622,19 @@ namespace mvlt
return res;
}

for (auto& it : UniqueKeys)
{
std::cout << it << std::endl;
}

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

RecordTemplate.GetData(key, defaultKeyValue);

res.IsOperationSuccess = true;
Expand Down Expand Up @@ -800,6 +828,9 @@ namespace mvlt
// Set key proper key order
vaultRecordSet.KeysOrder = KeysOrder;

// Set unique keys
vaultRecordSet.UniqueKeys = UniqueKeys;

// Copy keys from this to vaultRecordSet
for (auto& keyCopierIt : VaultKeyCopiers)
keyCopierIt.second(vaultRecordSet);
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
3 changes: 2 additions & 1 deletion Tests/VaultRecordSetUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ void VaultRecordSet_GetKeyValue_Test()
// Dynamic key add
vlt.AddKey("B", -1);

TEST_ASSERT(vrs.GetKeyValue("B", keyValue).ResultCode == VaultOperationResultCode::Success, "Failed to get not existed key value");
VaultOperationResult vrss = vrs.GetKeyValue("B", keyValue);
TEST_ASSERT(vrss.ResultCode == VaultOperationResultCode::Success, "Failed to get not existed key value");
}

void VaultRecordSet_GetKeyType_Test()
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 9de22b7

Please sign in to comment.