BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsondocument.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/jsondocument.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 "folderbasedtest.h"
16 #include "google_test.h"
18 #include "mvvm/model/sessionitem.h"
21 #include "mvvm/model/taginfo.h"
23 #include "test_utils.h"
24 #include <stdexcept>
25 
26 using namespace ModelView;
27 
28 //! Tests JsonDocument class
29 
31 public:
32  JsonDocumentTest() : FolderBasedTest("test_JsonDocument") {}
34 
35  class TestModel1 : public SessionModel {
36  public:
37  TestModel1() : SessionModel("TestModel1") {}
39  };
40 
41  class TestModel2 : public SessionModel {
42  public:
43  TestModel2() : SessionModel("TestModel2") {}
45  };
46 };
47 
51 
52 //! Saving the model with content into document and restoring it after.
53 
54 TEST_F(JsonDocumentTest, saveLoadSingleModel)
55 {
56  auto fileName = TestUtils::TestFileName(testDir(), "saveLoadSingleModel.json");
57  SessionModel model("TestModel");
58  JsonDocument document({&model});
59 
60  // filling model with parent and child
61  auto parent = model.insertItem<SessionItem>();
62  parent->setDisplayName("parent_name");
63  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
64  const auto parent_identifier = parent->identifier();
65 
66  parent->setData(QVariant::fromValue(42));
67  auto child = model.insertItem<PropertyItem>(parent);
68  child->setDisplayName("child_name");
69  const auto child_identifier = child->identifier();
70 
71  // saving model in file
72  document.save(fileName);
73 
74  // modifying model further
75  model.removeItem(model.rootItem(), {"", 0});
76 
77  // loading model from file
78  document.load(fileName);
79 
80  // checking that it is as it was right after the save
81 
82  // accessing reconstructed parent and child
83  auto reco_parent = model.rootItem()->getItem("", 0);
84  auto reco_child = reco_parent->getItem("", 0);
85 
86  // checking parent reconstruction
87  EXPECT_EQ(reco_parent->model(), &model);
88  EXPECT_EQ(reco_parent->modelType(), Constants::BaseType);
89  EXPECT_EQ(reco_parent->parent(), model.rootItem());
90  EXPECT_EQ(reco_parent->displayName(), "SessionItem"); // name changed becase of ProjectConverter
91  EXPECT_EQ(reco_parent->childrenCount(), 1);
92  EXPECT_EQ(reco_parent->identifier(), parent_identifier);
93  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
94  EXPECT_EQ(reco_parent->data<int>(), 42);
95 
96  // checking child reconstruction
97  EXPECT_EQ(reco_child->model(), &model);
98  EXPECT_EQ(reco_child->modelType(), Constants::PropertyType);
99  EXPECT_EQ(reco_child->parent(), reco_parent);
100  EXPECT_EQ(reco_child->displayName(), "Property");
101  EXPECT_EQ(reco_child->childrenCount(), 0);
102  EXPECT_EQ(reco_child->identifier(), child_identifier);
103  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
104 }
105 
106 //! Saving two models with content into document and restoring it after.
107 
108 TEST_F(JsonDocumentTest, saveLoadTwoModels)
109 {
110  auto fileName = TestUtils::TestFileName(testDir(), "saveLoadTwoModels.json");
111  TestModel1 model1;
112  TestModel2 model2;
113  JsonDocument document({&model1, &model2});
114 
115  // filling models
116  auto parent1 = model1.insertItem<SessionItem>();
117  const auto parent_identifier1 = parent1->identifier();
118 
119  auto parent2 = model2.insertItem<SessionItem>();
120  const auto parent_identifier2 = parent2->identifier();
121 
122  // saving models in file
123  document.save(fileName);
124 
125  // modifying model further
126  model1.removeItem(model1.rootItem(), {"", 0});
127  model2.removeItem(model2.rootItem(), {"", 0});
128 
129  // loading model from file
130  document.load(fileName);
131 
132  // checking that it is as it was right after the save
133 
134  // accessing reconstructed parent and child
135  auto reco_parent1 = model1.rootItem()->getItem("", 0);
136  auto reco_parent2 = model2.rootItem()->getItem("", 0);
137 
138  // checking parent reconstruction
139  EXPECT_EQ(reco_parent1->model(), &model1);
140  EXPECT_EQ(reco_parent1->parent(), model1.rootItem());
141  EXPECT_EQ(reco_parent1->identifier(), parent_identifier1);
142 
143  EXPECT_EQ(reco_parent2->model(), &model2);
144  EXPECT_EQ(reco_parent2->parent(), model2.rootItem());
145  EXPECT_EQ(reco_parent2->identifier(), parent_identifier2);
146 }
147 
148 //! Attempt to restore models in wrong order.
149 
150 TEST_F(JsonDocumentTest, loadModelsInWrongOrder)
151 {
152  auto fileName = TestUtils::TestFileName(testDir(), "loadModelsInWrongOrder.json");
153  TestModel1 model1;
154  TestModel2 model2;
155 
156  // filling models
157  auto parent1 = model1.insertItem<SessionItem>();
158  const auto parent_identifier1 = parent1->identifier();
159 
160  auto parent2 = model2.insertItem<SessionItem>();
161  const auto parent_identifier2 = parent2->identifier();
162 
163  // saving models in file
164  {
165  JsonDocument document({&model1, &model2});
166  document.save(fileName);
167  }
168 
169  // modifying model further
170  model1.removeItem(model1.rootItem(), {"", 0});
171  model2.removeItem(model2.rootItem(), {"", 0});
172 
173  JsonDocument document({&model2, &model1}); // intentional wrong order
174 
175  // loading model from file
176  EXPECT_THROW(document.load(fileName), std::runtime_error);
177 }
Convenience class which creates a directory on disk for test content.
Tests JsonDocument class.
Saves and restores list of SessionModel's to/from disk using json format.
Definition: jsondocument.h:29
void save(const std::string &file_name) const override
Saves models on disk.
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
PropertyItem * setDisplayName(const std::string &name) override
Sets display name (fluent interface).
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
virtual SessionItem * setDisplayName(const std::string &name)
Sets display name (fluent interface).
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionItem * rootItem() const
Returns root item of the model.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
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
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonDocumentTest, saveLoadSingleModel)
Saving the model with content into document and restoring it after.
const model_type PropertyType
Definition: mvvm_types.h:59
const model_type BaseType
Definition: mvvm_types.h:45
materialitems.h Collection of materials to populate MaterialModel.
std::string TestFileName(const std::string &test_sub_dir, const std::string &file_name)
Returns full path to the file in test directory.
Definition: test_utils.cpp:52
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?