BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
project.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/project.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"
19 #include "mvvm/project/project.h"
21 #include "mvvm/utils/fileutils.h"
22 #include "test_utils.h"
23 #include <cctype>
24 
25 using namespace ModelView;
26 
27 namespace {
28 const std::string samplemodel_name = "SampleModel";
29 const std::string materialmodel_name = "MaterialModel";
30 
31 //! Constructs json file name from SessionModel typeName (as it is done internaly by Project).
32 std::string get_json_filename(const std::string& model_name)
33 {
34  std::string result(model_name);
35  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
36  return result + ".json";
37 }
38 
39 } // namespace
40 
41 //! Tests for Project class.
42 
43 class ProjectTest : public FolderBasedTest {
44 public:
46  : FolderBasedTest("test_ProjectTest")
47  , sample_model(std::make_unique<SessionModel>(samplemodel_name))
48  , material_model(std::make_unique<SessionModel>(materialmodel_name))
49  {
50  }
52 
53  std::vector<SessionModel*> models() const
54  {
55  return {sample_model.get(), material_model.get()};
56  };
57 
59  {
60  ProjectContext result;
61  result.m_models_callback = [this]() { return models(); };
62  return result;
63  }
64 
65  std::unique_ptr<SessionModel> sample_model;
66  std::unique_ptr<SessionModel> material_model;
67 };
68 
69 ProjectTest::~ProjectTest() = default;
70 
71 TEST_F(ProjectTest, initialState)
72 {
73  Project project(createContext());
74  EXPECT_TRUE(project.projectDir().empty());
75  EXPECT_FALSE(project.isModified());
76 }
77 
78 //! Testing saveModel.
79 
80 TEST_F(ProjectTest, saveModel)
81 {
82  Project project(createContext());
83 
84  // create project directory and save file
85  auto project_dir = createEmptyDir("Untitled1");
86  project.save(project_dir);
87 
88  EXPECT_EQ(project.projectDir(), project_dir);
89  EXPECT_FALSE(project.isModified());
90 
91  auto sample_json = Utils::join(project_dir, get_json_filename(samplemodel_name));
92  EXPECT_TRUE(Utils::exists(sample_json));
93 
94  auto material_json = Utils::join(project_dir, get_json_filename(materialmodel_name));
95  EXPECT_TRUE(Utils::exists(material_json));
96 }
97 
98 //! Testing loadModel.
99 
100 TEST_F(ProjectTest, loadModel)
101 {
102  Project project(createContext());
103 
104  auto item0 = sample_model->insertItem<PropertyItem>();
105  item0->setData(std::string("sample_model_item"));
106  auto item0_identifier = item0->identifier();
107 
108  auto item1 = material_model->insertItem<PropertyItem>();
109  item1->setData(std::string("material_model_item"));
110  auto item1_identifier = item1->identifier();
111 
112  // create project directory and save file
113  auto project_dir = createEmptyDir("Untitled2");
114 
115  EXPECT_TRUE(project.isModified());
116  project.save(project_dir);
117  EXPECT_FALSE(project.isModified());
118 
119  EXPECT_EQ(project.projectDir(), project_dir);
120 
121  // cleaning models
122  sample_model->clear();
123  material_model->clear();
124  EXPECT_EQ(sample_model->rootItem()->childrenCount(), 0);
125  EXPECT_EQ(material_model->rootItem()->childrenCount(), 0);
126  EXPECT_TRUE(project.isModified());
127 
128  // loading
129  project.load(project_dir);
130  EXPECT_EQ(sample_model->rootItem()->childrenCount(), 1);
131  EXPECT_EQ(material_model->rootItem()->childrenCount(), 1);
132 
133  // checking identifiers
134  EXPECT_EQ(sample_model->rootItem()->children()[0]->identifier(), item0_identifier);
135  EXPECT_EQ(material_model->rootItem()->children()[0]->identifier(), item1_identifier);
136 
137  EXPECT_EQ(project.projectDir(), project_dir);
138  EXPECT_FALSE(project.isModified());
139 }
Convenience class which creates a directory on disk for test content.
Project represents content of all application models in a folder on disk.
Definition: project.h:28
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Tests for Project class.
ProjectContext createContext()
std::unique_ptr< SessionModel > material_model
std::unique_ptr< SessionModel > sample_model
std::vector< SessionModel * > models() const
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT std::string join(const std::string &part1, const std::string &part2)
Joins two path elements into the path.
Definition: fileutils.cpp:37
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
materialitems.h Collection of materials to populate MaterialModel.
@ project
selective copying for saving/loading the project (tags and data created by item, updated from JSON)
Definition: filesystem.h:81
Defines class CLASS?
TEST_F(ProjectTest, initialState)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32
models_callback_t m_models_callback
Definition: project_types.h:42
Defines class CLASS?