LCOV - code coverage report
Current view: top level - /opt/artdaq/srcs/artdaq-database/test/JsonDocument/JSONDocumentBuilder - JSONDocumentBuilder_t.cc (source / functions) Coverage Total Hit
Test: artdaq.info.cleaned Lines: 67.9 % 249 169
Test Date: 2026-10-29 00:35:58 Functions: 100.0 % 12 12

            Line data    Source code
       1              : #include <fstream>
       2              : #include <iostream>
       3              : #include <memory>
       4              : #include <streambuf>
       5              : #include <string>
       6              : 
       7              : #include "artdaq-database/JsonDocument/JSONDocument.h"
       8              : #include "artdaq-database/Overlay/JSONDocumentOverlay.h"
       9              : 
      10              : #include <boost/program_options.hpp>
      11              : #include "artdaq-database/JsonDocument/JSONDocumentBuilder.h"
      12              : 
      13              : namespace bpo = boost::program_options;
      14              : using artdaq::database::docrecord::JSONDocument;
      15              : using artdaq::database::docrecord::JSONDocumentBuilder;
      16              : namespace ovl = artdaq::database::overlay;
      17              : namespace adb = artdaq::database;
      18              : 
      19              : using test_case = bool (*)(const std::string&);
      20              : 
      21              : bool test_buildDocument(std::string const& conf);
      22              : 
      23              : bool test_addAlias(std::string const& conf);
      24              : bool test_setVersion(std::string const& conf);
      25              : bool test_removeAlias(std::string const& conf);
      26              : bool test_markDeleted(std::string const& conf);
      27              : bool test_markReadonly(std::string const& conf);
      28              : 
      29              : bool test_addToGlobalConfig(std::string const& conf);
      30              : 
      31              : bool test_addEntity(std::string const& conf);
      32              : bool test_removeEntity(std::string const& conf);
      33              : 
      34           11 : int main(int argc, char* argv[]) {
      35           11 :   artdaq::database::docrecord::debug::JSONDocument();
      36           11 :   artdaq::database::docrecord::debug::JSONDocumentUtils();
      37              : 
      38           11 :   artdaq::database::docrecord::debug::JSONDocumentBuilder();
      39           11 :   debug::registerUngracefullExitHandlers();
      40              : 
      41           11 :   std::ostringstream descstr;
      42           11 :   descstr << argv[0] << " <-c <config-file>> <other-options>";
      43              : 
      44           11 :   bpo::options_description desc = descstr.str();
      45              : 
      46           11 :   desc.add_options()("config,c", bpo::value<std::string>(), "Configuration file.")("help,h", "produce help message");
      47              : 
      48           11 :   bpo::variables_map vm;
      49              : 
      50              :   try {
      51           11 :     bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
      52           11 :     bpo::notify(vm);
      53            0 :   } catch (bpo::error const& e) {
      54            0 :     std::cerr << "Exception from command line processing in " << argv[0] << ": " << e.what() << "\n";
      55            0 :     return -1;
      56            0 :   }
      57              : 
      58           22 :   if (vm.count("help") != 0u) {
      59            0 :     std::cout << desc << std::endl;
      60            0 :     return 1;
      61              :   }
      62           22 :   if (vm.count("config") == 0u) {
      63              :     std::cerr << "Exception from command line processing in " << argv[0] << ": no configuration file given.\n"
      64              :               << "For usage and an options list, please do '" << argv[0] << " --help"
      65            0 :               << "'.\n";
      66            0 :     return 2;
      67              :   }
      68              : 
      69           11 :   auto file_name = vm["config"].as<std::string>();
      70              : 
      71           11 :   std::ifstream is(file_name);
      72              : 
      73           11 :   std::string conf((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
      74              : 
      75           33 :   auto name = [](auto const& conf) { return JSONDocument(conf).value_as<std::string>("operation"); };
      76              : 
      77           11 :   auto runTest = [](std::string const& name) {
      78              :     auto tests = std::map<std::string, test_case>{{"buildDocument", test_buildDocument}, {"addAlias", test_addAlias},
      79              :                                                   {"addEntity", test_addEntity},         {"removeAlias", test_removeAlias},
      80              :                                                   {"removeEntity", test_removeEntity},   {"markDeleted", test_markDeleted},
      81              :                                                   {"markReadonly", test_markReadonly},   {"addToGlobalConfig", test_addToGlobalConfig},
      82          121 :                                                   {"setVersion", test_setVersion}};
      83              : 
      84           11 :     std::cout << "Running test:<" << name << ">\n";
      85              : 
      86           22 :     return tests.at(name);
      87           22 :   };
      88              : 
      89           11 :   auto testResult = runTest(name(conf))(conf);
      90              : 
      91           11 :   return static_cast<int>(!testResult);
      92           11 : }
      93              : 
      94              : namespace literal {
      95              : constexpr auto operation [[gnu::unused]] = "operation";
      96              : constexpr auto path [[gnu::unused]] = "path";
      97              : constexpr auto beginstate [[gnu::unused]] = "begin-state";
      98              : constexpr auto delta [[gnu::unused]] = "delta";
      99              : constexpr auto endstate [[gnu::unused]] = "end-state";
     100              : constexpr auto returnedvalue [[gnu::unused]] = "returned-value";
     101              : constexpr auto mustsucceed [[gnu::unused]] = "must-succeed";
     102              : }  // namespace literal
     103              : 
     104            3 : bool test_buildDocument(std::string const& conf) {
     105            3 :   confirm(!conf.empty());
     106              : 
     107            3 :   auto opts = JSONDocument(conf);
     108            6 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     109            6 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     110              : 
     111            3 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     112              : 
     113              :   try {
     114              :     // createFromData(begin)
     115            3 :     JSONDocumentBuilder returned{};
     116            3 :     returned.createFromData(begin);
     117            3 :     JSONDocumentBuilder expected{end};
     118              : 
     119              :     using namespace artdaq::database::overlay;
     120            3 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     121              : 
     122            3 :     auto result = returned == expected;
     123              : 
     124            3 :     std::cerr << "returned:\n" << returned << "\n";
     125            3 :     std::cerr << "expected:\n" << expected << "\n";
     126              : 
     127            3 :     if (!result.first && mustsucceed) {
     128            0 :       std::cout << "Error returned!=expected.\n";
     129            0 :       std::cerr << "returned:\n" << returned << "\n";
     130            0 :       std::cerr << "expected:\n" << expected << "\n";
     131            0 :       std::cerr << "error:\n" << result.second << "\n";
     132            0 :       return false;
     133              :     }
     134              : 
     135            3 :   } catch (adb::exception const& e) {
     136            0 :     if (mustsucceed) {
     137            0 :       throw;
     138              :     }
     139            0 :   }
     140              : 
     141            3 :   return true;
     142            3 : }
     143              : 
     144            1 : bool test_addAlias(std::string const& conf) {
     145            1 :   confirm(!conf.empty());
     146              : 
     147            1 :   auto opts = JSONDocument(conf);
     148            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     149            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     150            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     151              : 
     152            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     153              : 
     154              :   try {
     155            1 :     JSONDocumentBuilder returned{begin};
     156            1 :     returned.addAlias(delta);
     157            1 :     JSONDocumentBuilder expected{end};
     158              : 
     159              :     using namespace artdaq::database::overlay;
     160            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     161              : 
     162            1 :     auto result = returned == expected;
     163              : 
     164            1 :     if (!result.first && mustsucceed) {
     165            0 :       std::cout << "Error returned!=expected.\n";
     166            0 :       std::cerr << "returned:\n" << returned << "\n";
     167            0 :       std::cerr << "expected:\n" << expected << "\n";
     168            0 :       std::cerr << "error:\n" << result.second << "\n";
     169            0 :       return false;
     170              :     }
     171              : 
     172            1 :   } catch (adb::exception const& e) {
     173            0 :     if (mustsucceed) {
     174            0 :       throw;
     175              :     }
     176            0 :   }
     177              : 
     178            1 :   return true;
     179            1 : }
     180              : 
     181            1 : bool test_removeAlias(std::string const& conf) {
     182            1 :   confirm(!conf.empty());
     183              : 
     184            1 :   auto opts = JSONDocument(conf);
     185            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     186            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     187            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     188              : 
     189            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     190              : 
     191              :   try {
     192            1 :     JSONDocumentBuilder returned{begin};
     193            1 :     returned.removeAlias(delta);
     194            1 :     JSONDocumentBuilder expected{end};
     195              : 
     196              :     using namespace artdaq::database::overlay;
     197            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     198              : 
     199            1 :     auto result = returned == expected;
     200              : 
     201            1 :     if (!result.first && mustsucceed) {
     202            0 :       std::cout << "Error returned!=expected.\n";
     203            0 :       std::cerr << "returned:\n" << returned << "\n";
     204            0 :       std::cerr << "expected:\n" << expected << "\n";
     205            0 :       std::cerr << "error:\n" << result.second << "\n";
     206            0 :       return false;
     207              :     }
     208              : 
     209            1 :   } catch (adb::exception const& e) {
     210            0 :     if (mustsucceed) {
     211            0 :       throw;
     212              :     }
     213            0 :   }
     214              : 
     215            1 :   return true;
     216            1 : }
     217              : 
     218            1 : bool test_addToGlobalConfig(std::string const& conf) {
     219            1 :   confirm(!conf.empty());
     220              : 
     221            1 :   auto opts = JSONDocument(conf);
     222            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     223            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     224            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     225              : 
     226            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     227              : 
     228              :   try {
     229            1 :     JSONDocumentBuilder returned{begin};
     230            1 :     returned.addConfiguration(delta);
     231            1 :     JSONDocumentBuilder expected{end};
     232              : 
     233              :     using namespace artdaq::database::overlay;
     234            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     235              : 
     236            1 :     auto result = returned == expected;
     237              : 
     238            1 :     if (!result.first && mustsucceed) {
     239            0 :       std::cout << "Error returned!=expected.\n";
     240            0 :       std::cerr << "returned:\n" << returned << "\n";
     241            0 :       std::cerr << "expected:\n" << expected << "\n";
     242            0 :       std::cerr << "error:\n" << result.second << "\n";
     243            0 :       return false;
     244              :     }
     245              : 
     246            1 :   } catch (adb::exception const& e) {
     247            0 :     if (mustsucceed) {
     248            0 :       throw;
     249              :     }
     250            0 :   }
     251              : 
     252            1 :   return true;
     253            1 : }
     254              : 
     255            1 : bool test_setVersion(std::string const& conf) {
     256            1 :   confirm(!conf.empty());
     257              : 
     258            1 :   auto opts = JSONDocument(conf);
     259            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     260            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     261            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     262              : 
     263            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     264              : 
     265              :   try {
     266            1 :     JSONDocumentBuilder returned{begin};
     267            1 :     returned.setVersion(delta);
     268            1 :     JSONDocumentBuilder expected{end};
     269              : 
     270              :     using namespace artdaq::database::overlay;
     271            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     272              : 
     273            1 :     auto result = returned == expected;
     274              : 
     275            1 :     if (!result.first && mustsucceed) {
     276            0 :       std::cout << "Error returned!=expected.\n";
     277            0 :       std::cerr << "returned:\n" << returned << "\n";
     278            0 :       std::cerr << "expected:\n" << expected << "\n";
     279            0 :       std::cerr << "error:\n" << result.second << "\n";
     280            0 :       return false;
     281              :     }
     282              : 
     283            1 :   } catch (adb::exception const& e) {
     284            0 :     if (mustsucceed) {
     285            0 :       throw;
     286              :     }
     287            0 :   }
     288              : 
     289            1 :   return true;
     290            1 : }
     291              : 
     292            1 : bool test_markReadonly(std::string const& conf) {
     293            1 :   confirm(!conf.empty());
     294              : 
     295            1 :   auto opts = JSONDocument(conf);
     296            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     297            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     298              : 
     299            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     300              : 
     301              :   try {
     302            1 :     JSONDocumentBuilder returned{begin};
     303            1 :     returned.markReadonly();
     304            1 :     JSONDocumentBuilder expected{end};
     305              : 
     306              :     using namespace artdaq::database::overlay;
     307            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     308              : 
     309            1 :     auto result = returned == expected;
     310              : 
     311            1 :     if (!result.first && mustsucceed) {
     312            0 :       std::cout << "Error returned!=expected.\n";
     313            0 :       std::cerr << "returned:\n" << returned << "\n";
     314            0 :       std::cerr << "expected:\n" << expected << "\n";
     315            0 :       std::cerr << "error:\n" << result.second << "\n";
     316            0 :       return false;
     317              :     }
     318              : 
     319            1 :   } catch (adb::exception const& e) {
     320            0 :     if (mustsucceed) {
     321            0 :       throw;
     322              :     }
     323            0 :   }
     324              : 
     325            1 :   return true;
     326            1 : }
     327              : 
     328            1 : bool test_markDeleted(std::string const& conf) {
     329            1 :   confirm(!conf.empty());
     330              : 
     331            1 :   auto opts = JSONDocument(conf);
     332            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     333            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     334              : 
     335            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     336              : 
     337              :   try {
     338            1 :     JSONDocumentBuilder returned{begin};
     339            1 :     returned.markDeleted();
     340            1 :     JSONDocumentBuilder expected{end};
     341              : 
     342              :     using namespace artdaq::database::overlay;
     343            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     344              : 
     345            1 :     auto result = returned == expected;
     346              : 
     347            1 :     if (!result.first && mustsucceed) {
     348            0 :       std::cout << "Error returned!=expected.\n";
     349            0 :       std::cerr << "returned:\n" << returned << "\n";
     350            0 :       std::cerr << "expected:\n" << expected << "\n";
     351            0 :       std::cerr << "error:\n" << result.second << "\n";
     352            0 :       return false;
     353              :     }
     354              : 
     355            1 :   } catch (adb::exception const& e) {
     356            0 :     if (mustsucceed) {
     357            0 :       throw;
     358              :     }
     359            0 :   }
     360              : 
     361            1 :   return true;
     362            1 : }
     363              : 
     364            1 : bool test_addEntity(std::string const& conf) {
     365            1 :   confirm(!conf.empty());
     366              : 
     367            1 :   auto opts = JSONDocument(conf);
     368            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     369            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     370            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     371              : 
     372            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     373              : 
     374              :   try {
     375            1 :     JSONDocumentBuilder returned{begin};
     376            1 :     returned.addEntity(delta);
     377            1 :     JSONDocumentBuilder expected{end};
     378              : 
     379              :     using namespace artdaq::database::overlay;
     380            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     381              : 
     382            1 :     auto result = returned == expected;
     383              : 
     384            1 :     if (!result.first && mustsucceed) {
     385            0 :       std::cout << "Error returned!=expected.\n";
     386            0 :       std::cerr << "returned:\n" << returned << "\n";
     387            0 :       std::cerr << "expected:\n" << expected << "\n";
     388            0 :       std::cerr << "error:\n" << result.second << "\n";
     389            0 :       return false;
     390              :     }
     391              : 
     392            1 :   } catch (adb::exception const& e) {
     393            0 :     if (mustsucceed) {
     394            0 :       throw;
     395              :     }
     396            0 :   }
     397              : 
     398            1 :   return true;
     399            1 : }
     400              : 
     401            1 : bool test_removeEntity(std::string const& conf) {
     402            1 :   confirm(!conf.empty());
     403              : 
     404            1 :   auto opts = JSONDocument(conf);
     405            2 :   auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
     406            2 :   auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
     407            2 :   auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
     408              : 
     409            1 :   auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
     410              : 
     411              :   try {
     412            1 :     JSONDocumentBuilder returned{begin};
     413            1 :     returned.removeEntity(delta);
     414            1 :     JSONDocumentBuilder expected{end};
     415              : 
     416              :     using namespace artdaq::database::overlay;
     417            1 :     ovl::useCompareMask(DOCUMENT_COMPARE_MUTE_TIMESTAMPS | DOCUMENT_COMPARE_MUTE_OUIDS);
     418              : 
     419            1 :     auto result = returned == expected;
     420              : 
     421            1 :     if (!result.first && mustsucceed) {
     422            0 :       std::cout << "Error returned!=expected.\n";
     423            0 :       std::cerr << "returned:\n" << returned << "\n";
     424            0 :       std::cerr << "expected:\n" << expected << "\n";
     425            0 :       std::cerr << "error:\n" << result.second << "\n";
     426            0 :       return false;
     427              :     }
     428              : 
     429            1 :   } catch (adb::exception const& e) {
     430            0 :     if (mustsucceed) {
     431            0 :       throw;
     432              :     }
     433            0 :   }
     434              : 
     435            1 :   return true;
     436            1 : }
        

Generated by: LCOV version 2.0-1