Skip to content
Snippets Groups Projects

F consolidation

Merged Timm Fitschen requested to merge f-consolidation into dev
1 file
+ 3
0
Compare changes
  • Side-by-side
  • Inline
+ 82
41
@@ -21,6 +21,7 @@
*
*/
#include "caosdb_test_utility.h"
#include "caosdb/data_type.h" // for DataType, AtomicDat...
#include "caosdb/entity.h" // for Entity, Parent, Par...
#include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionSe...
#include "caosdb/entity/v1alpha1/main.pb.h" // for IdResponse, Message
@@ -28,12 +29,15 @@
#include "caosdb/protobuf_helper.h" // for get_arena
#include "caosdb/status_code.h" // for StatusCode, FILE_DO...
#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
#include "caosdb/value.h" // for Value
#include <exception>
#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
#include <iostream>
#include <memory> // for allocator, shared_ptr
#include <stdexcept>
#include <string> // for operator+, string
namespace caosdb::entity {
@@ -68,17 +72,42 @@ TEST(test_entity, test_property_setters) {
auto prop = Property();
prop.SetName("prop_name");
prop.SetId("prop_id");
prop.SetImportance("prop_importance");
prop.SetImportance(Importance::OBLIGATORY);
prop.SetValue("prop_value");
prop.SetUnit("prop_unit");
prop.SetDatatype("prop_dtype");
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.GetImportance(), Importance::OBLIGATORY);
EXPECT_TRUE(prop.GetValue().IsString());
EXPECT_EQ(prop.GetValue().AsString(), "prop_value");
EXPECT_EQ(prop.GetUnit(), "prop_unit");
EXPECT_EQ(prop.GetDatatype(), "prop_dtype");
EXPECT_TRUE(prop.GetDataType().IsReference());
EXPECT_EQ(prop.GetDataType().AsReference().GetName(), "prop_dtype");
EXPECT_FALSE(prop.GetDataType().IsList());
prop.SetDataType(AtomicDataType::DATETIME);
EXPECT_TRUE(prop.GetDataType().IsAtomic());
EXPECT_EQ(prop.GetDataType().AsAtomic(), AtomicDataType::DATETIME);
EXPECT_FALSE(prop.GetDataType().IsList());
}
TEST(test_entity, test_list_property_setters) {
auto prop = Property();
prop.SetDataType(AtomicDataType::DATETIME); // Set as atomic first.
EXPECT_TRUE(prop.GetDataType().IsAtomic());
EXPECT_EQ(prop.GetDataType().AsAtomic(), AtomicDataType::DATETIME);
prop.SetDataType(AtomicDataType::DOUBLE, true);
auto const &dtype = prop.GetDataType();
EXPECT_FALSE(dtype.IsAtomic()); // Should not be true anymore.
EXPECT_FALSE(dtype.IsReference());
EXPECT_TRUE(dtype.IsList());
EXPECT_NE(dtype.AsAtomic(), AtomicDataType::DATETIME); // Should be overwritten.
EXPECT_TRUE(dtype.AsList().IsListOfAtomic());
EXPECT_EQ(dtype.AsList().GetAtomicDataType(), AtomicDataType::DOUBLE);
}
TEST(test_entity, test_append_property) {
@@ -87,28 +116,31 @@ TEST(test_entity, test_append_property) {
auto prop = Property();
prop.SetName("prop_name");
prop.SetId("prop_id");
prop.SetImportance("prop_importance");
prop.SetImportance(Importance::RECOMMENDED);
prop.SetValue("prop_value");
prop.SetUnit("prop_unit");
prop.SetDatatype("prop_dtype");
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);
// also test RepeatedPtrFieldWrapper.at()
const auto &same_prop = entity.GetProperties().at(0);
EXPECT_THROW((void)entity.GetProperties().at(2), std::out_of_range);
EXPECT_THROW((void)entity.GetProperties().at(-1), std::out_of_range);
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());
EXPECT_EQ(prop.GetDataType(), same_prop.GetDataType());
}
TEST(test_entity, test_copy_to) {
auto entity = Entity();
entity.SetRole("original_role");
entity.SetRole(Role::RECORD);
entity.SetName("orignial_name");
auto parent = Parent();
@@ -123,8 +155,7 @@ TEST(test_entity, test_copy_to) {
// 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());
auto *proto_copy = google::protobuf::Arena::CreateMessage<ProtoEntity>(get_arena());
entity.CopyTo(proto_copy);
auto copied = Entity(proto_copy);
@@ -142,15 +173,15 @@ TEST(test_entity, test_insert_entity) {
std::shared_ptr<transaction::FileTransmissionService::Stub>(nullptr));
auto entity = Entity();
entity.SetRole("entity_role");
entity.SetRole(Role::RECORD_TYPE);
entity.SetName("entity_name");
EXPECT_EQ(entity.GetRole(), "entity_role");
EXPECT_EQ(entity.GetRole(), Role::RECORD_TYPE);
EXPECT_EQ(entity.GetName(), "entity_name");
transaction.InsertEntity(&entity);
EXPECT_EQ(entity.GetRole(), "entity_role");
EXPECT_EQ(entity.GetRole(), Role::RECORD_TYPE);
EXPECT_EQ(entity.GetName(), "entity_name");
}
@@ -160,19 +191,21 @@ TEST(test_entity, test_insert_with_role) {
std::shared_ptr<transaction::FileTransmissionService::Stub>(nullptr));
auto entity = Entity();
entity.SetRole("Property");
entity.SetDatatype("DOUBLE");
entity.SetRole(Role::PROPERTY);
entity.SetDataType(AtomicDataType::DOUBLE);
entity.SetName("Length");
entity.SetUnit("m");
entity.SetValue("5.5");
entity.SetValue(5.5);
transaction.InsertEntity(&entity);
EXPECT_EQ(entity.GetRole(), "Property");
EXPECT_EQ(entity.GetDatatype(), "DOUBLE");
EXPECT_EQ(entity.GetRole(), Role::PROPERTY);
EXPECT_TRUE(entity.GetDataType().IsAtomic());
EXPECT_EQ(entity.GetDataType().AsAtomic(), AtomicDataType::DOUBLE);
EXPECT_EQ(entity.GetName(), "Length");
EXPECT_EQ(entity.GetUnit(), "m");
EXPECT_EQ(entity.GetValue(), "5.5");
EXPECT_TRUE(entity.GetValue().IsDouble());
EXPECT_DOUBLE_EQ(entity.GetValue().AsDouble(), 5.5);
}
TEST(test_entity, test_insert_with_parent) {
@@ -211,10 +244,10 @@ TEST(test_entity, test_insert_with_property) {
auto prop = Property();
prop.SetName("prop_name");
prop.SetId("prop_id");
prop.SetImportance("prop_importance");
prop.SetValue("prop_value");
prop.SetImportance(Importance::FIX);
prop.SetValue(Value("prop_value"));
prop.SetUnit("prop_unit");
prop.SetDatatype("prop_dtype");
prop.SetDataType("prop_dtype");
entity.AppendProperty(prop);
@@ -222,14 +255,15 @@ TEST(test_entity, test_insert_with_property) {
EXPECT_EQ(entity.GetProperties().size(), 1);
auto inserted_prop = entity.GetProperties().at(0);
const auto &inserted_prop = entity.GetProperties().at(0);
EXPECT_EQ(prop.GetName(), inserted_prop.GetName());
EXPECT_EQ(prop.GetId(), inserted_prop.GetId());
EXPECT_EQ(prop.GetImportance(), inserted_prop.GetImportance());
EXPECT_EQ(prop.GetValue().ToString(), inserted_prop.GetValue().ToString());
EXPECT_EQ(prop.GetValue(), inserted_prop.GetValue());
EXPECT_EQ(prop.GetUnit(), inserted_prop.GetUnit());
EXPECT_EQ(prop.GetDatatype(), inserted_prop.GetDatatype());
EXPECT_EQ(prop.GetDataType(), inserted_prop.GetDataType());
}
TEST(test_entity, test_from_id_response) {
@@ -245,8 +279,7 @@ TEST(test_entity, test_from_id_response) {
EXPECT_TRUE(entity.HasErrors());
EXPECT_EQ(entity.GetErrors().size(), 1);
EXPECT_EQ(entity.GetErrors().at(0).GetDescription(), "error_desc");
EXPECT_EQ(entity.GetErrors().at(0).GetCode(),
MessageCode::ENTITY_DOES_NOT_EXIST);
EXPECT_EQ(entity.GetErrors().at(0).GetCode(), MessageCode::ENTITY_DOES_NOT_EXIST);
IdResponse idr_warnings_and_infos;
idr_warnings_and_infos.set_id("other_entity_id");
@@ -263,8 +296,7 @@ TEST(test_entity, test_from_id_response) {
EXPECT_EQ(other_ent.GetWarnings().size(), 1);
EXPECT_TRUE(other_ent.HasWarnings());
EXPECT_EQ(other_ent.GetWarnings().at(0).GetDescription(), "warning_desc");
EXPECT_EQ(other_ent.GetWarnings().at(0).GetCode(),
MessageCode::ENTITY_HAS_NO_PROPERTIES);
EXPECT_EQ(other_ent.GetWarnings().at(0).GetCode(), MessageCode::ENTITY_HAS_NO_PROPERTIES);
EXPECT_EQ(other_ent.GetInfos().size(), 1);
EXPECT_EQ(other_ent.GetInfos().at(0).GetDescription(), "info_desc");
EXPECT_EQ(other_ent.GetInfos().at(0).GetCode(), MessageCode::UNSPECIFIED);
@@ -277,14 +309,13 @@ TEST(test_entity, test_add_file_to_non_file_entity) {
TEST(test_entity, test_add_non_existing_file) {
Entity entity;
entity.SetRole("File");
EXPECT_EQ(entity.SetLocalPath("non-existing/path"),
StatusCode::FILE_DOES_NOT_EXIST_LOCALLY);
entity.SetRole(Role::FILE);
EXPECT_EQ(entity.SetLocalPath("non-existing/path"), StatusCode::FILE_DOES_NOT_EXIST_LOCALLY);
}
TEST(test_entity, test_add_directory_path) {
Entity entity;
entity.SetRole("File");
entity.SetRole(Role::FILE);
EXPECT_EQ(entity.SetLocalPath("./"), StatusCode::PATH_IS_A_DIRECTORY);
}
@@ -386,8 +417,11 @@ TEST(test_entity, test_property_iterator) {
ASSERT_EQ(entity.GetProperties().size(), 5);
int counter = 0;
for (auto &property : entity.GetProperties()) {
// TODO(tf) Copy constructor was deleted
// auto nonref_property = entity.GetProperties().at(counter);
auto name = "PROPERTY-" + std::to_string(counter);
EXPECT_EQ(property.GetName(), name);
// EXPECT_EQ(nonref_property.GetName(), name);
counter++;
}
EXPECT_EQ(counter, 5);
@@ -414,10 +448,17 @@ TEST(test_entity, test_description) {
EXPECT_EQ(parent.GetDescription(), "desc parent");
}
TEST(test_entity, test_role) {
Entity entity;
entity.SetRole(Role::RECORD_TYPE);
EXPECT_EQ(entity.GetRole(), Role::RECORD_TYPE);
}
TEST(test_entity, test_add_file) {
Entity entity;
entity.SetRole("File");
EXPECT_EQ(entity.SetLocalPath(TEST_DATA_DIR + "/test.json"),
StatusCode::SUCCESS);
entity.SetRole(Role::FILE);
EXPECT_EQ(entity.SetLocalPath(TEST_DATA_DIR + "/test.json"), StatusCode::SUCCESS);
}
} // namespace caosdb::entity
Loading