BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitembackupstrategy.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testmodel/jsonitembackupstrategy.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "google_test.h"
18 #include "mvvm/model/itemfactory.h"
22 
23 using namespace ModelView;
24 
25 class JsonItemBackupStrategyTest : public ::testing::Test {
26 public:
28  : m_factory(std::make_unique<ItemFactory>(CreateStandardItemCatalogue()))
29  {
30  }
32 
33  std::unique_ptr<JsonItemBackupStrategy> createBackupStrategy()
34  {
35  return std::make_unique<JsonItemBackupStrategy>(m_factory.get());
36  }
37 
38  std::unique_ptr<ItemFactory> m_factory;
39 };
40 
42 
43 //! Saving/restoring PropertyItem.
44 
46 {
47  auto strategy = createBackupStrategy();
48 
49  PropertyItem item;
50  item.setData(42.0);
51 
52  strategy->saveItem(&item);
53  auto restored = strategy->restoreItem();
54 
55  EXPECT_EQ(item.modelType(), restored->modelType());
56  EXPECT_EQ(item.identifier(), restored->identifier());
57  EXPECT_EQ(item.data<QVariant>(), restored->data<QVariant>());
58 }
59 
60 //! Saving/restoring CompoundItem.
61 
63 {
64  auto strategy = createBackupStrategy();
65 
66  CompoundItem item;
67  auto property = item.addProperty("thickness", 42.0);
68 
69  strategy->saveItem(&item);
70  auto restored = strategy->restoreItem();
71 
72  EXPECT_EQ(item.modelType(), restored->modelType());
73  EXPECT_EQ(item.identifier(), restored->identifier());
74  EXPECT_EQ(restored->getItem("thickness")->data<double>(), property->data<double>());
75  EXPECT_EQ(restored->getItem("thickness")->identifier(), property->identifier());
76 }
77 
78 //! Saving/restoring CustomItem.
79 
81 {
82  auto strategy = createBackupStrategy();
83 
84  const std::string model_type(Constants::BaseType);
85 
86  // creating parent with one child
87  auto parent = std::make_unique<SessionItem>(model_type);
88  parent->setDisplayName("parent_name");
89  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
90  auto child = new SessionItem(model_type);
91  child->setDisplayName("child_name");
92  parent->insertItem(child, TagRow::append());
93 
94  // creating copy
95  strategy->saveItem(parent.get());
96  auto reco_parent = strategy->restoreItem();
97 
98  EXPECT_EQ(reco_parent->childrenCount(), 1);
99  EXPECT_EQ(reco_parent->modelType(), model_type);
100  EXPECT_EQ(reco_parent->displayName(), "parent_name");
101  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
102  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
103  EXPECT_EQ(reco_parent->model(), nullptr);
104 
105  // checking child reconstruction
106  auto reco_child = reco_parent->getItem("defaultTag");
107  EXPECT_EQ(reco_child->parent(), reco_parent.get());
108  EXPECT_EQ(reco_child->childrenCount(), 0);
109  EXPECT_EQ(reco_child->modelType(), model_type);
110  EXPECT_EQ(reco_child->displayName(), "child_name");
111  EXPECT_EQ(reco_child->identifier(), child->identifier());
112  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
113 }
std::unique_ptr< JsonItemBackupStrategy > createBackupStrategy()
std::unique_ptr< ItemFactory > m_factory
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
T * addProperty(const std::string &name)
Adds property item of given type.
Definition: compounditem.h:43
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonItemBackupStrategyTest, propertyItem)
Saving/restoring PropertyItem.
const model_type BaseType
Definition: mvvm_types.h:45
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< ItemCatalogue > CreateStandardItemCatalogue()
Creates a catalog of items supported by SessionModel out-of-the-box.
std::string model_type
Definition: types.h:23
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?