From 23f1536eac5e72fb4710bf1dd55168937842a3af Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 15:42:09 +0100 Subject: [PATCH] Add tests --- src/storage/v3/shard_rsm.cpp | 44 ++++-------------------------- tests/simulation/shard_rsm.cpp | 50 +++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index bf66bc463..18e4a6911 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -590,6 +590,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { for (const auto label : vertex.add_labels) { if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) { HandleError(maybe_error.GetError(), "Update Vertex"); + action_successful = false; + break; } } for (const auto label : vertex.remove_labels) { @@ -599,45 +601,11 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { } for (auto &update_prop : vertex.property_updates) { - auto result_schema = - vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); - if (result_schema.HasError()) { - auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Updating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Updating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Updating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Updating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Updating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Updating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Updating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); - + if (const auto result_schema = vertex_to_update->SetPropertyAndValidate( + update_prop.first, ToPropertyValue(std::move(update_prop.second))); + result_schema.HasError()) { + HandleError(result_schema.GetError(), "Update Vertex"); action_successful = false; - break; } } diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 23dfecac1..500649f4c 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -183,19 +183,53 @@ bool AttemptToDeleteVertex(ShardClient &client, int64_t value) { } } -bool AttemptToUpdateVertex(ShardClient &client, int64_t value) { - auto vertex_id = GetValuePrimaryKeysWithValue(value)[0]; +bool AttemptToUpdateVertex(ShardClient &client, int64_t vertex_primary_key, std::vector add_labels = {}, + std::vector remove_labels = {}) { + auto vertex_id = GetValuePrimaryKeysWithValue(vertex_primary_key)[0]; std::vector> property_updates; auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value(static_cast(10000))); - msgs::UpdateVertex vertex_prop; - vertex_prop.primary_key = vertex_id; - vertex_prop.property_updates = {property_update}; + msgs::UpdateVertex update_vertex; + update_vertex.primary_key = vertex_id; + update_vertex.property_updates = {property_update}; + update_vertex.add_labels = add_labels; + update_vertex.remove_labels = remove_labels; msgs::UpdateVerticesRequest update_req; update_req.transaction_id.logical_id = GetTransactionId(); - update_req.update_vertices = {vertex_prop}; + update_req.update_vertices = {update_vertex}; + + while (true) { + auto write_res = client.SendWriteRequest(update_req); + if (write_res.HasError()) { + continue; + } + + auto write_response_result = write_res.GetValue(); + auto write_response = std::get(write_response_result); + + Commit(client, update_req.transaction_id); + return write_response.success; + } +} + +bool AttemptToRemoveVertexProperty(ShardClient &client, int64_t primary_key, std::vector add_labels = {}, + std::vector remove_labels = {}) { + auto vertex_id = GetValuePrimaryKeysWithValue(primary_key)[0]; + + std::vector> property_updates; + auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value()); + + msgs::UpdateVertex update_vertex; + update_vertex.primary_key = vertex_id; + update_vertex.property_updates = {property_update}; + update_vertex.add_labels = add_labels; + update_vertex.remove_labels = remove_labels; + + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + update_req.update_vertices = {update_vertex}; while (true) { auto write_res = client.SendWriteRequest(update_req); @@ -872,7 +906,9 @@ void TestCreateAndUpdateVertices(ShardClient &client) { auto unique_prop_val = GetUniqueInteger(); MG_ASSERT(AttemptToCreateVertex(client, unique_prop_val)); - MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val)); + MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {LabelId::FromInt(3)})); + MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {}, {LabelId::FromInt(3)})); + MG_ASSERT(AttemptToRemoveVertexProperty(client, unique_prop_val)); } void TestCreateEdge(ShardClient &client) {