From fc4969d3f93441519bd1a9e7ffe4cc2cacacbd69 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 3 Aug 2021 14:01:11 +0200
Subject: [PATCH] TST: Add unit tests for properties and CopyTo

---
 test/test_entity.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/test/test_entity.cpp b/test/test_entity.cpp
index 60177fb..e1d9271 100644
--- a/test/test_entity.cpp
+++ b/test/test_entity.cpp
@@ -3,6 +3,7 @@
  * This file is a part of the CaosDB Project.
  *
  * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
+ * Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
  * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -23,7 +24,9 @@
 #include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionSe...
 #include "caosdb/entity/v1alpha1/main.pb.h"      // for IdResponse, Message
 #include "caosdb/message_code.h"                 // for MessageCode
+#include "caosdb/protobuf_helper.h"              // for get_arena
 #include "caosdb/transaction.h"                  // for Transaction
+#include "google/protobuf/arena.h"               // for Arena
 #include "gtest/gtest-message.h"                 // for Message
 #include "gtest/gtest-test-part.h"               // for TestPartResult, Sui...
 #include "gtest/gtest_pred_impl.h"               // for Test, EXPECT_EQ
@@ -33,6 +36,8 @@
 
 namespace caosdb::entity {
 using caosdb::entity::v1alpha1::IdResponse;
+using ProtoEntity = caosdb::entity::v1alpha1::Entity;
+using caosdb::utility::get_arena;
 
 TEST(test_entity, test_parent_setters) {
   auto parent = Parent();
@@ -56,6 +61,79 @@ TEST(test_entity, test_append_parent) {
   EXPECT_EQ(same_parent.GetId(), "some-id");
 }
 
+TEST(test_entity, test_property_setters) {
+  auto prop = Property();
+  prop.SetName("prop_name");
+  prop.SetId("prop_id");
+  prop.SetImportance("prop_importance");
+  prop.SetValue("prop_value");
+  prop.SetUnit("prop_unit");
+  prop.SetDatatype("prop_dtype");
+
+  EXPECT_EQ(prop.GetName(), "prop_name");
+  EXPECT_EQ(prop.GetId(), "prop_id");
+  EXPECT_EQ(prop.GetImportance(), "prop_importance");
+  EXPECT_EQ(prop.GetValue(), "prop_value");
+  EXPECT_EQ(prop.GetUnit(), "prop_unit");
+  EXPECT_EQ(prop.GetDatatype(), "prop_dtype");
+}
+
+// TODO(fspreck) cognitive complexity > 25 (threshold)
+TEST(test_entity, test_append_property) { // NOLINT
+  auto entity = Entity();
+
+  auto prop = Property();
+  prop.SetName("prop_name");
+  prop.SetId("prop_id");
+  prop.SetImportance("prop_importance");
+  prop.SetValue("prop_value");
+  prop.SetUnit("prop_unit");
+  prop.SetDatatype("prop_dtype");
+
+  EXPECT_EQ(entity.GetProperties().Size(), 0);
+  entity.AppendProperty(prop);
+  EXPECT_EQ(entity.GetProperties().Size(), 1);
+
+  auto same_prop = entity.GetProperties().At(0);
+
+  EXPECT_EQ(prop.GetName(), same_prop.GetName());
+  EXPECT_EQ(prop.GetId(), same_prop.GetId());
+  EXPECT_EQ(prop.GetImportance(), same_prop.GetImportance());
+  EXPECT_EQ(prop.GetValue(), same_prop.GetValue());
+  EXPECT_EQ(prop.GetUnit(), same_prop.GetUnit());
+  EXPECT_EQ(prop.GetDatatype(), same_prop.GetDatatype());
+}
+
+TEST(test_entity, test_copy_to) {
+  auto entity = Entity();
+  entity.SetId("original_id");
+  entity.SetName("orignial_name");
+
+  auto parent = Parent();
+  parent.SetId("parent_id");
+  parent.SetName("parent_name");
+  entity.AppendParent(parent);
+
+  auto prop = Property();
+  prop.SetId("prop_id");
+  prop.SetName("prop_name");
+  entity.AppendProperty(prop);
+
+  // create protobuf entity to which all fields ae copied and then a
+  // CaoosDB entity from that protobuf entity.
+  auto *proto_copy =
+    google::protobuf::Arena::CreateMessage<ProtoEntity>(get_arena());
+  entity.CopyTo(proto_copy);
+  auto copied = Entity(proto_copy);
+
+  EXPECT_EQ(entity.GetId(), copied.GetId());
+  EXPECT_EQ(entity.GetName(), copied.GetName());
+  EXPECT_EQ(copied.GetParents().At(0).GetId(), parent.GetId());
+  EXPECT_EQ(copied.GetParents().At(0).GetName(), parent.GetName());
+  EXPECT_EQ(copied.GetProperties().At(0).GetId(), prop.GetId());
+  EXPECT_EQ(copied.GetProperties().At(0).GetName(), prop.GetName());
+}
+
 TEST(test_entity, test_insert_entity) {
   auto transaction = caosdb::transaction::Transaction(
     std::shared_ptr<transaction::EntityTransactionService::Stub>(nullptr));
-- 
GitLab