diff --git a/Source/Vault.hpp b/Source/Vault.hpp index d01b1e1..c33a3d7 100644 --- a/Source/Vault.hpp +++ b/Source/Vault.hpp @@ -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); diff --git a/Source/VaultOperationResult.cpp b/Source/VaultOperationResult.cpp index 15cc52e..0a09f89 100644 --- a/Source/VaultOperationResult.cpp +++ b/Source/VaultOperationResult.cpp @@ -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."; diff --git a/Source/VaultOperationResult.h b/Source/VaultOperationResult.h index 7d5a8c8..eef5004 100644 --- a/Source/VaultOperationResult.h +++ b/Source/VaultOperationResult.h @@ -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 diff --git a/Tests/VaultUnitTest.cpp b/Tests/VaultUnitTest.cpp index b93a333..c4dc6d7 100644 --- a/Tests/VaultUnitTest.cpp +++ b/Tests/VaultUnitTest.cpp @@ -63,15 +63,20 @@ void Vault_UpdateKey_Test() vlt.AddKey("A", -1); vlt.AddKey("B", "none"); + vlt.AddUniqueKey("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("C", 250); + TEST_ASSERT(vrs.ResultCode == VaultOperationResultCode::TryToUpdateUniqueKey, "Cannot update unique key"); } void Vault_IsKeyExist_Test()