BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonmodelconverter.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for jsonmodelconverter.test.cpp:

Go to the source code of this file.

Classes

class  JsonModelConverterTest
 Checks JsonModel class and its ability to convert SessionModel to json and back. More...
 

Functions

 TEST_F (JsonModelConverterTest, emptyModel)
 Creation of json object: empty model. More...
 
 TEST_F (JsonModelConverterTest, emptyModelToJsonAndBack)
 Empty model to json and back. More...
 
 TEST_F (JsonModelConverterTest, identifiers)
 Item in a model to json and back: how persistent are identifiers. More...
 
 TEST_F (JsonModelConverterTest, parentAndChildToFileAndBack)
 Filling model from json: parent and child in a model to json and back. More...
 
 TEST_F (JsonModelConverterTest, parentAndChildToJsonAndBack)
 Filling model from json: parent and child in a model to json and back. More...
 
 TEST_F (JsonModelConverterTest, singleItemToJsonAndBack)
 Creation of json object: single item in a model. More...
 
 TEST_F (JsonModelConverterTest, singleItemToJsonAndBackToSameModel)
 Creation of json object (single item in a model), then writing same json object back to model without emptying it. More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file jsonmodelconverter.test.cpp.

Function Documentation

◆ TEST_F() [1/7]

TEST_F ( JsonModelConverterTest  ,
emptyModel   
)

Creation of json object: empty model.

Definition at line 46 of file jsonmodelconverter.test.cpp.

47 {
48  JsonModelConverter converter(ConverterMode::project);
49  SessionModel model("TestModel");
50 
51  QJsonObject object = converter.to_json(model);
52 
53  EXPECT_EQ(object[JsonItemFormatAssistant::sessionModelKey], "TestModel");
54  EXPECT_EQ(object[JsonItemFormatAssistant::itemsKey].toArray().size(), 0);
55 
56  JsonItemFormatAssistant assistant;
57  EXPECT_TRUE(assistant.isSessionModel(object));
58 }
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionModel(const QJsonObject &object) const
Returns true if given json object represents SessionModel.
Converter of SessionModel to/from json object with posibility to select one of convertion modes.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37

References ModelView::JsonItemFormatAssistant::isSessionModel(), ModelView::JsonItemFormatAssistant::itemsKey, ModelView::project, ModelView::JsonItemFormatAssistant::sessionModelKey, and ModelView::JsonModelConverter::to_json().

Here is the call graph for this function:

◆ TEST_F() [2/7]

TEST_F ( JsonModelConverterTest  ,
emptyModelToJsonAndBack   
)

Empty model to json and back.

Definition at line 62 of file jsonmodelconverter.test.cpp.

63 {
64  JsonModelConverter converter(ConverterMode::project);
65  SessionModel model("TestModel");
66 
67  QJsonObject object = converter.to_json(model);
68 
69  // attempt to reconstruct model of different type.
70  SessionModel target1("NewModel");
71  EXPECT_THROW(converter.from_json(object, target1), std::runtime_error);
72 
73  // attempt to reconstruct non-empty model
74  SessionModel target2("TestModel");
75  target2.insertItem<SessionItem>();
76  EXPECT_NO_THROW(converter.from_json(object, target2));
77 
78  // succesfull reconstruction
79  SessionModel target3("TestModel");
80  EXPECT_NO_THROW(converter.from_json(object, target3));
81  EXPECT_EQ(target3.rootItem()->childrenCount(), 0u);
82 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38

References ModelView::SessionItem::childrenCount(), ModelView::JsonModelConverter::from_json(), ModelView::SessionModel::insertItem(), ModelView::project, ModelView::SessionModel::rootItem(), and ModelView::JsonModelConverter::to_json().

Here is the call graph for this function:

◆ TEST_F() [3/7]

TEST_F ( JsonModelConverterTest  ,
identifiers   
)

Item in a model to json and back: how persistent are identifiers.

Definition at line 154 of file jsonmodelconverter.test.cpp.

155 {
156  JsonModelConverter converter(ConverterMode::project);
157  auto pool1 = std::make_shared<ItemPool>();
158 
159  // creating model and converting it to json
160  SessionModel source("SourceModel", pool1);
161  auto parent1 = source.insertItem<SessionItem>();
162  QJsonObject json_source = converter.to_json(source);
163 
164  // creating source and filling it from json
165  auto pool2 = std::make_shared<ItemPool>();
166  SessionModel target("SourceModel", pool2);
167  converter.from_json(json_source, target);
168  auto reco_parent = target.rootItem()->getItem("", 0);
169 
170  // comparing identifiers of two items from different models
171  auto id1 = parent1->identifier();
172  auto id2 = reco_parent->identifier();
173  EXPECT_EQ(id1, id2);
174 
175  // saving target in its own json
176  QJsonObject json_target = converter.to_json(target);
177 
178  // comparing text representations of two json
179  EXPECT_EQ(TestUtils::JsonToString(json_source), TestUtils::JsonToString(json_target));
180 
181  // checking item registrations
182  EXPECT_EQ(pool1->item_for_key(id1), parent1);
183  EXPECT_EQ(pool2->item_for_key(id2), reco_parent);
184 }
QString JsonToString(const QJsonObject &object)
Definition: test_utils.cpp:70

References ModelView::JsonModelConverter::from_json(), ModelView::SessionItem::getItem(), ModelView::SessionItem::identifier(), ModelView::SessionModel::insertItem(), TestUtils::JsonToString(), ModelView::project, ModelView::SessionModel::rootItem(), and ModelView::JsonModelConverter::to_json().

Here is the call graph for this function:

◆ TEST_F() [4/7]

TEST_F ( JsonModelConverterTest  ,
parentAndChildToFileAndBack   
)

Filling model from json: parent and child in a model to json and back.

Definition at line 188 of file jsonmodelconverter.test.cpp.

189 {
190  JsonModelConverter converter(ConverterMode::project);
191  SessionModel model("TestModel");
192 
193  // filling original model with content
194  auto parent = model.insertItem<SessionItem>();
195  parent->setDisplayName("parent_name");
196  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
197 
198  parent->setData(QVariant::fromValue(42));
199  auto child = model.insertItem<PropertyItem>(parent);
200  child->setDisplayName("child_name");
201 
202  // writing model to json
203  auto object = converter.to_json(model);
204 
205  // saving object to file
206  auto fileName = TestUtils::TestFileName(testDir(), "model.json");
207  TestUtils::SaveJson(object, fileName);
208 
209  // converting document back to item
210  auto document = TestUtils::LoadJson(fileName);
211  SessionModel target("TestModel");
212  converter.from_json(document.object(), target);
213 
214  // accessing reconstructed parent and child
215  auto reco_parent = target.rootItem()->getItem("", 0);
216  auto reco_child = reco_parent->getItem("", 0);
217 
218  // checking parent reconstruction
219  EXPECT_EQ(reco_parent->model(), &target);
220  EXPECT_EQ(reco_parent->modelType(), Constants::BaseType);
221  EXPECT_EQ(reco_parent->parent(), target.rootItem());
222  EXPECT_EQ(reco_parent->displayName(), "SessionItem");
223  EXPECT_EQ(reco_parent->childrenCount(), 1);
224  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
225  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
226  EXPECT_EQ(reco_parent->data<int>(), 42);
227 
228  // checking child reconstruction
229  EXPECT_EQ(reco_child->model(), &target);
230  EXPECT_EQ(reco_child->modelType(), Constants::PropertyType);
231  EXPECT_EQ(reco_child->parent(), reco_parent);
232  EXPECT_EQ(reco_child->displayName(), "Property");
233  EXPECT_EQ(reco_child->childrenCount(), 0);
234  EXPECT_EQ(reco_child->identifier(), child->identifier());
235  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
236 }
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
PropertyItem * setDisplayName(const std::string &name) override
Sets display name (fluent interface).
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).
const model_type PropertyType
Definition: mvvm_types.h:59
const model_type BaseType
Definition: mvvm_types.h:45
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
void SaveJson(const QJsonObject &object, const std::string &fileName)
Definition: test_utils.cpp:58
QJsonDocument LoadJson(const std::string &fileName)
Definition: test_utils.cpp:81

References ModelView::Constants::BaseType, ModelView::JsonModelConverter::from_json(), ModelView::SessionItem::getItem(), ModelView::SessionModel::insertItem(), TestUtils::LoadJson(), ModelView::project, ModelView::Constants::PropertyType, ModelView::SessionItem::registerTag(), ModelView::SessionModel::rootItem(), TestUtils::SaveJson(), ModelView::SessionItem::setDisplayName(), ModelView::PropertyItem::setDisplayName(), TestUtils::TestFileName(), ModelView::JsonModelConverter::to_json(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [5/7]

TEST_F ( JsonModelConverterTest  ,
parentAndChildToJsonAndBack   
)

Filling model from json: parent and child in a model to json and back.

Definition at line 106 of file jsonmodelconverter.test.cpp.

107 {
108  JsonModelConverter converter(ConverterMode::project);
109  SessionModel model("TestModel");
110 
111  // filling original model with content
112  auto parent = model.insertItem<SessionItem>();
113  parent->setDisplayName("parent_name");
114  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
115 
116  parent->setData(QVariant::fromValue(42));
117  auto child = model.insertItem<PropertyItem>(parent);
118  child->setDisplayName("child_name");
119 
120  // writing model to json
121  QJsonObject object = converter.to_json(model);
122 
123  // reading model from json
124  SessionModel target("TestModel");
125  converter.from_json(object, target);
126 
127  // accessing reconstructed parent and child
128  auto reco_parent = target.rootItem()->getItem("", 0);
129  auto reco_child = reco_parent->getItem("", 0);
130 
131  // checking parent reconstruction
132  EXPECT_EQ(reco_parent->model(), &target);
133  EXPECT_EQ(reco_parent->modelType(), Constants::BaseType);
134  EXPECT_EQ(reco_parent->parent(), target.rootItem());
135  EXPECT_EQ(reco_parent->displayName(),
136  "SessionItem"); // Name changed because of ProjectConverter
137  EXPECT_EQ(reco_parent->childrenCount(), 1);
138  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
139  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
140  EXPECT_EQ(reco_parent->data<int>(), 42);
141 
142  // checking child reconstruction
143  EXPECT_EQ(reco_child->model(), &target);
144  EXPECT_EQ(reco_child->modelType(), Constants::PropertyType);
145  EXPECT_EQ(reco_child->parent(), reco_parent);
146  EXPECT_EQ(reco_child->displayName(), "Property"); // // Name changed because of ProjectConverter
147  EXPECT_EQ(reco_child->childrenCount(), 0);
148  EXPECT_EQ(reco_child->identifier(), child->identifier());
149  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
150 }

References ModelView::Constants::BaseType, ModelView::JsonModelConverter::from_json(), ModelView::SessionItem::getItem(), ModelView::SessionModel::insertItem(), ModelView::project, ModelView::Constants::PropertyType, ModelView::SessionItem::registerTag(), ModelView::SessionModel::rootItem(), ModelView::SessionItem::setDisplayName(), ModelView::PropertyItem::setDisplayName(), ModelView::JsonModelConverter::to_json(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [6/7]

TEST_F ( JsonModelConverterTest  ,
singleItemToJsonAndBack   
)

Creation of json object: single item in a model.

Definition at line 86 of file jsonmodelconverter.test.cpp.

87 {
88  JsonModelConverter converter(ConverterMode::project);
89  SessionModel model("TestModel");
90 
91  auto item = model.insertItem<SessionItem>();
92 
93  QJsonObject object = converter.to_json(model);
94 
95  // filling new model
96  SessionModel target("TestModel");
97  converter.from_json(object, target);
98  EXPECT_EQ(target.rootItem()->childrenCount(), 1u);
99  auto reco_item = target.rootItem()->getItem("", 0);
100  EXPECT_EQ(reco_item->parent(), target.rootItem());
101  EXPECT_EQ(reco_item->modelType(), item->modelType());
102 }

References ModelView::SessionItem::childrenCount(), ModelView::JsonModelConverter::from_json(), ModelView::SessionItem::getItem(), ModelView::SessionModel::insertItem(), ModelView::project, ModelView::SessionModel::rootItem(), and ModelView::JsonModelConverter::to_json().

Here is the call graph for this function:

◆ TEST_F() [7/7]

TEST_F ( JsonModelConverterTest  ,
singleItemToJsonAndBackToSameModel   
)

Creation of json object (single item in a model), then writing same json object back to model without emptying it.

Real bug case: check if unsubscribtion mechanism works.

Definition at line 241 of file jsonmodelconverter.test.cpp.

242 {
243  auto pool = std::make_shared<ItemPool>();
244 
245  JsonModelConverter converter(ConverterMode::project);
246  SessionModel model("TestModel", pool);
247  auto item = model.insertItem<SessionItem>();
248 
249  auto root_item = model.rootItem();
250  auto root_id = root_item->identifier();
251  auto item_id = item->identifier();
252 
253  QJsonObject object = converter.to_json(model);
254 
255  // filling new model
256  converter.from_json(object, model);
257 
258  EXPECT_EQ(pool->size(), 2);
259  EXPECT_FALSE(pool->item_for_key(root_id) == model.rootItem()); // old root identifier has gone
260  EXPECT_TRUE(model.rootItem() != root_item); // old root item gone
261 
262  auto new_item = model.rootItem()->children().at(0);
263  EXPECT_EQ(pool->item_for_key(item_id), new_item);
264 }
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87

References ModelView::SessionItem::children(), ModelView::JsonModelConverter::from_json(), ModelView::SessionItem::identifier(), ModelView::SessionModel::insertItem(), ModelView::project, ModelView::SessionModel::rootItem(), and ModelView::JsonModelConverter::to_json().

Here is the call graph for this function: