otsdaq  v2_05_02_indev
DatabaseInterfaceTest_t.cc
1 #define BOOST_TEST_MODULE (databaseinterface test)
2 
3 #include "boost/test/auto_unit_test.hpp"
4 
5 #include <time.h> /* time */
6 #include <cassert>
7 #include <cstdlib>
8 #include <iostream>
9 #include <memory>
10 #include <string>
11 
12 #include "otsdaq/ConfigurationInterface/DatabaseConfigurationInterface.h"
13 
14 #include "artdaq-database/JsonDocument/JSONDocument.h"
15 
16 #include "artdaq-database/ConfigurationDB/configuration_common.h"
17 #include "artdaq-database/ConfigurationDB/dispatch_common.h"
18 
19 #include "artdaq-database/JsonDocument/JSONDocument.h"
20 #include "artdaq-database/JsonDocument/JSONDocumentBuilder.h"
21 #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb.h"
22 #include "artdaq-database/StorageProviders/MongoDB/provider_mongodb.h"
23 
24 #include "artdaq-database/DataFormats/Json/json_reader.h"
25 //#include "artdaq-database/DataFormats/common/helper_functions.h"
26 //#include "artdaq-database/DataFormats/common/shared_literals.h"
27 
28 namespace ots
29 {
30 struct TableViewEx : public TableView
31 {
32  void printJSON(std::stringstream& ss) const { ss << _json; }
33  int fillFromJSON(std::string const& newjson)
34  {
35  _json = newjson;
36  return 0;
37  }
38  std::string _json = "{ \"testJSON\" : 123 }";
39 };
40 
41 struct TestConfiguration001 final : public TableBase
42 {
43  TestConfiguration001() : TableBase("TestConfiguration001") { init(0); }
44  void init(ConfigurationManager* configManager) { activeTableView_ = &view; }
45  TableViewEx view;
46 };
47 
48 struct TestConfiguration002 final : public TableBase
49 {
50  TestConfiguration002() : TableBase("TestConfiguration002") { init(0); }
51  void init(ConfigurationManager* configManager) { activeTableView_ = &view; }
52  TableViewEx view;
53 };
54 } // namespace ots
55 
56 struct TestData
57 {
58  TestData()
59  {
60  /*
61  artdaq::database::filesystem::debug::enable();
62  artdaq::database::mongo::debug::enable();
63  // artdaq::database::jsonutils::debug::enableJSONDocument();
64  // artdaq::database::jsonutils::debug::enableJSONDocumentBuilder();
65 
66  artdaq::database::configuration::debug::enableFindConfigsOperation();
67  artdaq::database::configuration::debug::enableCreateConfigsOperation();
68 
69  artdaq::database::configuration::debug::options::enableOperationBase();
70  artdaq::database::configuration::debug::options::enableOperationManageConfigs();
71  artdaq::database::configuration::debug::detail::enableCreateConfigsOperation();
72  artdaq::database::configuration::debug::detail::enableFindConfigsOperation();
73 
74  artdaq::database::configuration::debug::enableDBOperationMongo();
75  artdaq::database::configuration::debug::enableDBOperationFileSystem();
76 
77 // debug::registerUngracefullExitHandlers();
78  artdaq::database::dataformats::useFakeTime(true);
79  */
80  std::cout << "setup fixture\n";
81  }
82 
83  ~TestData() { std::cout << "setup fixture\n"; }
84 
85  void updateConfigCount(int count) { _oldConfigCount = count; }
86 
87  int version() { return _version; }
88  int oldConfigCount() { return _oldConfigCount; }
89 
90  const int _version = (srand(time(NULL)), rand() % 99999 + 100000);
91 
92  int _oldConfigCount = 0;
93 };
94 
95 using namespace ots;
96 
97 TestData fixture;
98 
99 BOOST_AUTO_TEST_SUITE(databaseinterface_test)
100 
101 BOOST_AUTO_TEST_CASE(configure_tests)
102 {
103  std::cout << "TestData::version=" << fixture.version() << "\n";
104  return;
105 }
106 
107 BOOST_AUTO_TEST_CASE(store_configuration)
108 {
109  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
110  auto ifc = DatabaseConfigurationInterface();
111 
112  cfg1->getViewP()->setVersion(fixture.version());
113 
114  BOOST_CHECK_EQUAL(cfg1->getViewP()->getVersion(), fixture.version());
115 
116  // std::cout << "Table Version " <<
117  // std::to_string(cfg1->getViewP()->getVersion() ) << "\n";
118 
119  ifc.saveActiveVersion(cfg1.get());
120 
121  return;
122 }
123 
124 BOOST_AUTO_TEST_CASE(load_configuration)
125 {
126  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
127  auto ifc = DatabaseConfigurationInterface();
128 
129  ifc.fill(cfg1.get(), TableVersion(fixture.version()));
130 
131  return;
132 }
133 
134 BOOST_AUTO_TEST_CASE(store_global_configuration)
135 {
136  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
137  std::shared_ptr<TableBase> cfg2 = std::make_shared<TestConfiguration002>();
138 
139  auto ifc = DatabaseConfigurationInterface();
140 
141  cfg1->getViewP()->setVersion(fixture.version() + 1);
142  cfg2->getViewP()->setVersion(fixture.version() + 2);
143 
144  ifc.saveActiveVersion(cfg1.get());
145 
146  ifc.saveActiveVersion(cfg2.get());
147 
148  auto map = DatabaseConfigurationInterface::config_version_map_t{};
149  map[cfg1->getTableName()] = cfg1->getView().getVersion();
150  map[cfg2->getTableName()] = cfg2->getView().getVersion();
151 
152  auto tableName = std::string{"config"} + std::to_string(fixture.version());
153 
154  auto list = ifc.getAllTableGroupNames();
155 
156  fixture.updateConfigCount(list.size());
157 
158  ifc.saveTableGroup(map, tableName);
159 
160  return;
161 }
162 
163 BOOST_AUTO_TEST_CASE(load_global_configuration)
164 {
165  auto ifc = DatabaseConfigurationInterface();
166 
167  auto tableName = std::string{"config"} + std::to_string(fixture.version());
168 
169  auto map = ifc.getTableGroupMembers(tableName);
170 
171  BOOST_CHECK_EQUAL(map.size(), 2);
172 
173  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
174  std::shared_ptr<TableBase> cfg2 = std::make_shared<TestConfiguration002>();
175 
176  BOOST_CHECK_EQUAL(map.at(cfg1->getTableName()), fixture.version() + 1);
177  BOOST_CHECK_EQUAL(map.at(cfg2->getTableName()), fixture.version() + 2);
178 
179  return;
180 }
181 
182 BOOST_AUTO_TEST_CASE(find_all_global_configurations)
183 {
184  auto ifc = DatabaseConfigurationInterface();
185 
186  auto list = ifc.getAllTableGroupNames();
187 
188  BOOST_CHECK_EQUAL(list.size(), fixture.oldConfigCount() + 1);
189 
190  auto tableName = std::string{"config"} + std::to_string(fixture.version());
191 
192  auto found = (std::find(list.begin(), list.end(), tableName) != list.end());
193 
194  BOOST_CHECK_EQUAL(found, true);
195 
196  return;
197 }
198 
199 BOOST_AUTO_TEST_CASE(list_configuration_types)
200 {
201  auto ifc = DatabaseConfigurationInterface();
202 
203  auto list = ifc.getAllTableNames();
204 
205  BOOST_CHECK_EQUAL(list.size(), 2);
206 
207  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
208  std::shared_ptr<TableBase> cfg2 = std::make_shared<TestConfiguration002>();
209 
210  auto found1 = (std::find(list.begin(), list.end(), cfg1->getTableName()) != list.end());
211 
212  BOOST_CHECK_EQUAL(found1, true);
213 
214  auto found2 = (std::find(list.begin(), list.end(), cfg2->getTableName()) != list.end());
215 
216  BOOST_CHECK_EQUAL(found2, true);
217 
218  return;
219 }
220 
221 BOOST_AUTO_TEST_CASE(find_configuration_version)
222 {
223  auto ifc = DatabaseConfigurationInterface();
224 
225  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
226 
227  auto list = ifc.getVersions(cfg1.get());
228 
229  auto found1 = (std::find(list.begin(), list.end(), fixture.version()) != list.end());
230 
231  BOOST_CHECK_EQUAL(found1, true);
232 
233  auto found2 = (std::find(list.begin(), list.end(), fixture.version() + 1) != list.end());
234 
235  BOOST_CHECK_EQUAL(found2, true);
236 
237  return;
238 }
239 
241 {
242  auto ifc = DatabaseConfigurationInterface();
243 
244  std::shared_ptr<TableBase> cfg1 = std::make_shared<TestConfiguration001>();
245 
246  auto version = ifc.findLatestVersion(cfg1.get());
247 
248  auto list = ifc.getVersions(cfg1.get());
249 
250  std::cout << "Found versions\n";
251 
252  for(auto version : list)
253  {
254  std::cout << version << ", ";
255  }
256 
257  std::cout << "\nGot latest version: " << version << "\n";
258 
259  return;
260 }
261 
262 BOOST_AUTO_TEST_SUITE_END()