From f33ec19dc719c6ed5ee758363da889220a7fb811 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Wed, 22 Sep 2021 03:44:30 +0200
Subject: [PATCH] TST: add more tests for ToString

---
 include/caosdb/entity.h          | 24 ++++++-----
 include/caosdb/protobuf_helper.h | 11 +++--
 test/test_entity.cpp             | 73 ++++++++++++++++++++++++++++++++
 test/test_utility.cpp            |  7 +++
 4 files changed, 100 insertions(+), 15 deletions(-)

diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h
index 02151d1..6de939c 100644
--- a/include/caosdb/entity.h
+++ b/include/caosdb/entity.h
@@ -173,18 +173,18 @@ public:
 
   inline auto ToString() const noexcept -> const std::string override {
     if (this->size() == 0) {
-      return "[]";
+      return "[]\n";
     }
-    std::string result("[");
-    for (int i = 0; i < this->size(); i++) {
+    std::string result("[\n");
+    for (int i = 0; i < this->size();) {
       CAOSDB_DEBUG_MESSAGE_STRING(this->wrapped->at(i), next);
       result += next;
-      if (i < this->size() - 1) {
-        result += ",";
+      if (++i < this->size()) {
+        result.replace(result.size() - 1, 1, ",\n");
       }
     }
 
-    return result.append(std::string("]"));
+    return result.append(std::string("]\n"));
   }
 
 protected:
@@ -302,7 +302,6 @@ auto RepeatedPtrFieldWrapper<T, P>::end() const -> const RepeatedPtrFieldWrapper
  */
 class Message : public ScalarProtoMessageWrapper<ProtoMessage> {
 public:
-  // inline Message() {};
   /**
    * Get the code of this message.
    *
@@ -469,11 +468,14 @@ public:
   }
 
   explicit inline Property(ProtoProperty *other)
-    : ScalarProtoMessageWrapper<ProtoProperty>(other), value(this->wrapped->mutable_value()),
-      data_type(this->wrapped->mutable_data_type()){};
+    : ScalarProtoMessageWrapper<ProtoProperty>(other),
+      value(this->wrapped->has_value() ? this->wrapped->mutable_value()
+                                       : static_cast<ProtoValue *>(nullptr)),
+      data_type(this->wrapped->has_data_type() ? this->wrapped->mutable_data_type()
+                                               : static_cast<ProtoDataType *>(nullptr)){};
   inline Property()
-    : ScalarProtoMessageWrapper<ProtoProperty>(), value(this->wrapped->mutable_value()),
-      data_type(this->wrapped->mutable_data_type()){};
+    : ScalarProtoMessageWrapper<ProtoProperty>(), value(static_cast<ProtoValue *>(nullptr)),
+      data_type(static_cast<ProtoDataType *>(nullptr)){};
 
   /**
    * Return the id of this  property
diff --git a/include/caosdb/protobuf_helper.h b/include/caosdb/protobuf_helper.h
index 546bcbd..c82a88f 100644
--- a/include/caosdb/protobuf_helper.h
+++ b/include/caosdb/protobuf_helper.h
@@ -22,10 +22,13 @@
 #ifndef CAOSDB_PROTOBUF_HELPER_H
 #define CAOSDB_PROTOBUF_HELPER_H
 
-#include <google/protobuf/arena.h>                  // for Arena
-#include <google/protobuf/generated_message_util.h> // for Arena
-#include <google/protobuf/util/json_util.h>         // for JsonOptions, MessageToJs...
-#include <string>                                   // for string
+#include <google/protobuf/arena.h> // for Arena
+// IWYU pragma: no_include "google/protobuf/extension_set.h"
+// IWYU pragma: no_include "google/protobuf/generated_message_util.h"
+//#include <google/protobuf/extension_set.h>          // IWYU pragma: keep
+//#include <google/protobuf/generated_message_util.h> // IWYU pragma: keep
+#include <google/protobuf/util/json_util.h> // for JsonOptions, MessageToJs...
+#include <string>                           // for string
 
 #define CAOSDB_DEBUG_MESSAGE_STRING(message, out)                                                  \
   std::string out;                                                                                 \
diff --git a/test/test_entity.cpp b/test/test_entity.cpp
index 0987f6b..8f0a5fd 100644
--- a/test/test_entity.cpp
+++ b/test/test_entity.cpp
@@ -570,4 +570,77 @@ TEST(test_entity, test_entity_to_string) {
   EXPECT_EQ(entity.ToString(), "{}\n");
 }
 
+TEST(test_entity, test_properties_to_string) {
+  Entity entity;
+  EXPECT_EQ(entity.GetProperties().ToString(), "[]\n");
+
+  Property property;
+  property.SetName("Prop1");
+  entity.AppendProperty(property);
+  EXPECT_EQ(entity.GetProperties().ToString(), "[\n{\n \"name\": \"Prop1\"\n}\n]\n");
+
+  Property property2;
+  property2.SetName("Prop2");
+  entity.AppendProperty(property2);
+  EXPECT_EQ(entity.GetProperties().ToString(),
+            "[\n{\n \"name\": \"Prop1\"\n},\n{\n \"name\": \"Prop2\"\n}\n]\n");
+}
+
+TEST(test_entity, test_property_to_string) {
+  Property property;
+  EXPECT_EQ(property.ToString(), "{}\n");
+
+  property.SetDataType(AtomicDataType::DOUBLE);
+  EXPECT_EQ(property.ToString(),
+            "{\n \"dataType\": {\n  \"atomicDataType\": \"ATOMIC_DATA_TYPE_DOUBLE\"\n }\n}\n");
+}
+
+TEST(test_entity, test_parents_to_string) {
+  Parent parent;
+  parent.SetName("the name");
+
+  Entity entity;
+  entity.AppendParent(parent);
+
+  EXPECT_EQ(entity.GetParents().ToString(), "[\n{\n \"name\": \"the name\"\n}\n]\n");
+}
+
+TEST(test_entity, test_parent_to_string) {
+  Parent parent;
+  EXPECT_EQ(parent.ToString(), "{}\n");
+
+  parent.SetName("the name");
+  EXPECT_EQ(parent.ToString(), "{\n \"name\": \"the name\"\n}\n");
+}
+
+TEST(test_entity, test_messages_to_string) {
+  IdResponse idResponse;
+  idResponse.set_id("entity_id");
+  auto *error = idResponse.add_errors();
+  error->set_code(MessageCode::ENTITY_DOES_NOT_EXIST);
+  error->set_description("error_desc");
+
+  Entity entity(&idResponse);
+
+  // Messages are not printed, currently.
+  EXPECT_EQ(entity.ToString(), "{\n \"id\": \"entity_id\",\n \"version\": {}\n}\n");
+  EXPECT_EQ(entity.GetErrors().ToString(),
+            "[\n{\n \"code\": 2,\n \"description\": \"error_desc\"\n}\n]\n");
+}
+
+TEST(test_entity, test_message_to_string) {
+  IdResponse idResponse;
+  idResponse.set_id("entity_id");
+  auto *error = idResponse.add_errors();
+  error->set_code(MessageCode::ENTITY_DOES_NOT_EXIST);
+  error->set_description("error_desc");
+
+  Entity entity(&idResponse);
+
+  // Messages are not printed, currently.
+  EXPECT_EQ(entity.ToString(), "{\n \"id\": \"entity_id\",\n \"version\": {}\n}\n");
+  EXPECT_EQ(entity.GetErrors().at(0).ToString(),
+            "{\n \"code\": 2,\n \"description\": \"error_desc\"\n}\n");
+}
+
 } // namespace caosdb::entity
diff --git a/test/test_utility.cpp b/test/test_utility.cpp
index 875cfbf..3a9a420 100644
--- a/test/test_utility.cpp
+++ b/test/test_utility.cpp
@@ -26,6 +26,7 @@
 #include "boost/json/value.hpp"               // for value
 #include "caosdb/data_type.h"                 // for atomicdatatype_names
 #include "caosdb/entity.h"                    // for importance_names, role...
+#include "caosdb/status_code.h"               // for get_status_description
 #include "caosdb/utility.h"                   // for base64_encode, load_js...
 #include "caosdb_test_utility.h"              // for TEST_DATA_DIR
 #include <gtest/gtest-message.h>              // for Message
@@ -78,4 +79,10 @@ TEST(test_utility, enum_names) {
                        std::out_of_range, "Could not find enum value for string 'Invalid name'.");
 }
 
+TEST(test_utility, test_status_code_description) {
+  EXPECT_EQ(caosdb::get_status_description(12412323), "MISSING DESCRIPTION");
+  EXPECT_EQ(caosdb::get_status_description(static_cast<int>(StatusCode::UNKNOWN)),
+            "Unknown error. This is typically a bug (server or client).");
+}
+
 } // namespace caosdb::utility
-- 
GitLab