BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsontaginfoconverter.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/jsontaginfoconverter.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"
17 #include "mvvm/model/taginfo.h"
19 #include "test_utils.h"
20 #include <QJsonArray>
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <string>
24 
25 using namespace ModelView;
26 
27 //! Test convertion of TagInfo from/to QJsonObject.
28 
30 public:
31  JsonTagInfoConverterTest() : FolderBasedTest("test_JsonTagInfoConverter") {}
33 };
34 
36 
37 //! Checks if json object is correctly identified as representing TagInfo.
38 
40 {
41  JsonTagInfoConverter converter;
42 
43  // valid json object representing DataRole
44  QJsonObject object;
45  object[JsonTagInfoConverter::nameKey] = QString::fromStdString("tag1");
46  object[JsonTagInfoConverter::minKey] = 0;
47  object[JsonTagInfoConverter::maxKey] = 1;
48  object[JsonTagInfoConverter::modelsKey] = QJsonArray();
49 
50  EXPECT_TRUE(converter.isTagInfo(object));
51 
52  // invalid (not fully constructed) json object which can't represent TagInfo
53  QJsonObject object2;
54  object2[JsonTagInfoConverter::minKey] = 42;
55  EXPECT_FALSE(converter.isTagInfo(object2));
56 }
57 
58 //! Creating QJsonArray from TagInfo.
59 
61 {
62  JsonTagInfoConverter converter;
63 
64  TagInfo tag("tag1", 0, -1, std::vector<std::string>() = {});
65  auto object = converter.to_json(tag);
66 
67  // this object represents TagInfo
68  EXPECT_TRUE(converter.isTagInfo(object));
69 }
70 
71 //! From TagInfo to json and back.
72 
73 TEST_F(JsonTagInfoConverterTest, tagInfoToJsonAndBack)
74 {
75  JsonTagInfoConverter converter;
76 
77  TagInfo tag("tag", 0, 42, std::vector<std::string>() = {"aaa", "bbb"});
78  auto object = converter.to_json(tag);
79 
80  TagInfo reco_tag = converter.from_json(object);
81 
82  EXPECT_EQ(reco_tag.name(), tag.name());
83  EXPECT_EQ(reco_tag.min(), tag.min());
84  EXPECT_EQ(reco_tag.max(), tag.max());
85  EXPECT_EQ(reco_tag.modelTypes(), tag.modelTypes());
86 }
87 
88 //! To file and back.
89 
90 TEST_F(JsonTagInfoConverterTest, tagInfoToFileAndBack)
91 {
92  const std::string tag_name("tag");
93  const std::string model_type("model");
94  JsonTagInfoConverter converter;
95 
96  TagInfo tag = TagInfo::propertyTag(tag_name, model_type);
97  auto object = converter.to_json(tag);
98 
99  // saving object to file
100  auto fileName = TestUtils::TestFileName(testDir(), "taginfo.json");
101  TestUtils::SaveJson(object, fileName);
102 
103  auto document = TestUtils::LoadJson(fileName);
104  TagInfo reco_tag = converter.from_json(document.object());
105 
106  EXPECT_EQ(reco_tag.name(), tag_name);
107  EXPECT_EQ(reco_tag.min(), 1);
108  EXPECT_EQ(reco_tag.max(), 1);
109  EXPECT_EQ(reco_tag.modelTypes(), std::vector<std::string>() = {model_type});
110 }
Convenience class which creates a directory on disk for test content.
Test convertion of TagInfo from/to QJsonObject.
Default converter between TagInfo and json object.
QJsonObject to_json(const TagInfo &tag) override
TagInfo from_json(const QJsonObject &object) override
bool isTagInfo(const QJsonObject &object)
Returns true if given json object represents TagInfo object.
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
std::string name() const
Definition: taginfo.cpp:45
int max() const
Definition: taginfo.cpp:55
std::vector< std::string > modelTypes() const
Definition: taginfo.cpp:60
int min() const
Definition: taginfo.cpp:50
static TagInfo propertyTag(std::string name, std::string model_type)
Constructs tag intended for single property.
Definition: taginfo.cpp:40
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonTagInfoConverterTest, isItemTag)
Checks if json object is correctly identified as representing TagInfo.
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
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
Defines class CLASS?
Defines class CLASS?