diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index 0734a3c1a9257e57f04a9e3829bf43d1992f8801..e861e9fafafa955be636a1d6fdec131d45a7473c 100644 --- a/include/caosdb/entity.h +++ b/include/caosdb/entity.h @@ -541,8 +541,8 @@ public: * Set the datatype of this property. */ auto SetDataType(const DataType &new_data_type) -> StatusCode; - auto SetDataType(const AtomicDataType new_data_type) -> StatusCode; - auto SetDataType(const std::string &new_data_type) -> StatusCode; + auto SetDataType(const AtomicDataType new_data_type, bool list_type = false) -> StatusCode; + auto SetDataType(const std::string &new_data_type, bool list_type = false) -> StatusCode; /** * Return a json string representing this property. @@ -712,8 +712,8 @@ public: auto SetUnit(const std::string &unit) -> void; auto SetDataType(const DataType &new_data_type) -> StatusCode; - auto SetDataType(const AtomicDataType new_data_type) -> StatusCode; - auto SetDataType(const std::string &new_data_type) -> StatusCode; + auto SetDataType(const AtomicDataType new_data_type, bool list_type = false) -> StatusCode; + auto SetDataType(const std::string &new_data_type, bool list_type = false) -> StatusCode; auto AppendProperty(const Property &property) -> void; auto RemoveProperty(int index) -> void; diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp index e1fcc5f3c148414ca57718ecbd7201ee0b2323a4..3ec6da0f92a01922aee2c1e1901197a4fb4aa2ee 100644 --- a/src/caosdb/entity.cpp +++ b/src/caosdb/entity.cpp @@ -142,12 +142,12 @@ auto Property::SetDataType(const DataType &new_data_type) -> StatusCode { return this->data_type.CopyFrom(new_data_type); } -auto Property::SetDataType(const AtomicDataType new_data_type) -> StatusCode { - return SetDataType(DataType(new_data_type)); +auto Property::SetDataType(const AtomicDataType new_data_type, bool list_type) -> StatusCode { + return SetDataType(DataType(new_data_type, list_type)); } -auto Property::SetDataType(const std::string &new_data_type) -> StatusCode { - return SetDataType(DataType(new_data_type)); +auto Property::SetDataType(const std::string &new_data_type, bool list_type) -> StatusCode { + return SetDataType(DataType(new_data_type, list_type)); } [[nodiscard]] auto Entity::GetParents() const -> const Parents & { @@ -238,12 +238,12 @@ auto Entity::SetDataType(const DataType &new_data_type) -> StatusCode { return this->data_type.CopyFrom(new_data_type); } -auto Entity::SetDataType(const AtomicDataType new_data_type) -> StatusCode { - return SetDataType(DataType(new_data_type)); +auto Entity::SetDataType(const AtomicDataType new_data_type, bool list_type) -> StatusCode { + return SetDataType(DataType(new_data_type, list_type)); } -auto Entity::SetDataType(const std::string &new_data_type) -> StatusCode { - return SetDataType(DataType(new_data_type)); +auto Entity::SetDataType(const std::string &new_data_type, bool list_type) -> StatusCode { + return SetDataType(DataType(new_data_type, list_type)); } auto Entity::SetFilePath(const std::string &path) -> void { diff --git a/test/test_entity.cpp b/test/test_entity.cpp index 0726bece7105d8778ae3d8b0816530e3a9de9395..78d90012268aea8fc9587f175eba828e96ad726a 100644 --- a/test/test_entity.cpp +++ b/test/test_entity.cpp @@ -85,6 +85,29 @@ TEST(test_entity, test_property_setters) { EXPECT_EQ(prop.GetUnit(), "prop_unit"); 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) {