From f4ed997de9c71db56cb6875e4cfc9d0488e9e679 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Thu, 19 Aug 2021 15:15:43 +0200 Subject: [PATCH] ENH: Convenience list argument for SetDataType(..., bool list_type). --- include/caosdb/entity.h | 8 ++++---- src/caosdb/entity.cpp | 16 ++++++++-------- test/test_entity.cpp | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index 0734a3c..e861e9f 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 e1fcc5f..3ec6da0 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 0726bec..78d9001 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) { -- GitLab