LCOV - code coverage report
Current view: top level - opmonlib/unittest - schema_conversion_test.cxx (source / functions) Coverage Total Hit
Test: code.result Lines: 100.0 % 61 61
Test Date: 2025-12-21 13:07:08 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  * @file schema_conversion_test.cxx Test application that tests and demonstrates
       3              :  * the conversion from a generic protobuf message to an opmon entry.
       4              :  *
       5              :  * This is part of the DUNE DAQ Application Framework, copyright 2020.
       6              :  * Licensing/copyright details are in the COPYING file that you should have
       7              :  * received with this code.
       8              :  */
       9              : 
      10              : #include "opmonlib/Utils.hpp"
      11              : #include "opmonlib/opmon/test.pb.h"
      12              : 
      13              : #define BOOST_TEST_MODULE schema_conversion_test // NOLINT
      14              : 
      15              : 
      16              : 
      17              : #include "boost/test/unit_test.hpp"
      18              : 
      19              : #include <google/protobuf/util/json_util.h>
      20              : 
      21              : using namespace dunedaq::opmonlib;
      22              : using namespace dunedaq::opmon;
      23              : 
      24              : BOOST_AUTO_TEST_SUITE(Schema_Conversion_Test)
      25              : 
      26            2 : BOOST_AUTO_TEST_CASE(conversion) {
      27              : 
      28            1 :   const int64_t int_value = 394;
      29            1 :   const float float_value = 3.14;
      30            1 :   const double double_value = 6.28;
      31            1 :   const std::string string_value = "my_test" ;
      32            1 :   const bool bool_value = true;
      33            1 :   const auto tag_name = "channel";
      34            1 :   const auto tag_value = "365";
      35              :   
      36            1 :   dunedaq::opmon::TestInfo ti;
      37            1 :   ti.set_int_example( int_value );
      38            1 :   ti.set_float_example( double_value );
      39            1 :   ti.set_string_example( string_value );
      40            1 :   ti.set_bool_example( bool_value );
      41              : 
      42            1 :   dunedaq::opmon::ComplexInfo ci;
      43            1 :   ci.set_another_float(float_value);
      44            1 :   *ci.mutable_sub_message() = ti;
      45            1 :   ci.mutable_r_field() -> Add(42);   
      46              : 
      47            1 :   auto test_entry = to_entry( ti, {} );
      48            2 :   auto complex_entry = to_entry( ci, {{tag_name, tag_value}} );
      49              : 
      50              :   // std::string res;
      51              :   // google::protobuf::util::MessageToJsonString( test_entry, & res );
      52              :   // std::cout << res << std::endl;
      53              : 
      54              :   // res.clear();
      55              :   // google::protobuf::util::MessageToJsonString( complex_entry, & res );
      56              :   // std::cout << res << std::endl;
      57              : 
      58            1 :   BOOST_TEST( test_entry.data().size() == 4 );   //check that all the entry of the simple schema are in
      59            1 :   BOOST_TEST( complex_entry.data().size() == 5 );   // check that repeated entries are not added,
      60              :                                                     // but nested structures are
      61              : 
      62              :   // checks that the values are preserved correctly
      63            1 :   auto value = test_entry.data().find("int_example");  
      64            1 :   auto final_int =  value->second.int8_value();
      65            1 :   BOOST_TEST( final_int == int_value );
      66              : 
      67            1 :   value = test_entry.data().find("float_example");
      68            1 :   auto final_double = value -> second.double_value();
      69            1 :   BOOST_TEST( final_double == double_value );
      70              : 
      71            1 :   value = test_entry.data().find("string_example");
      72            1 :   auto final_string = value -> second.string_value();
      73            1 :   BOOST_TEST( final_string == string_value );
      74              : 
      75            1 :   value = test_entry.data().find("bool_example");
      76            1 :   auto final_bool = value -> second.boolean_value();
      77            1 :   BOOST_TEST( final_bool == bool_value );
      78              : 
      79            1 :   value = complex_entry.data().find("another_float");
      80            1 :   auto final_float = value -> second.float_value();
      81            1 :   BOOST_TEST( final_float == float_value );
      82              : 
      83            1 :   value = complex_entry.data().find("sub_message.string_example");
      84            1 :   final_string = value -> second.string_value();
      85            1 :   BOOST_TEST( final_string == string_value );
      86              : 
      87            1 :   BOOST_TEST( complex_entry.custom_origin().begin()->first  == tag_name);
      88            1 :   BOOST_TEST( complex_entry.custom_origin().begin()->second == tag_value);
      89              : 
      90              :   // inverse
      91            1 :   auto reco_ti = from_entry<dunedaq::opmon::TestInfo>( test_entry );
      92            1 :   BOOST_TEST( reco_ti.int_example() == int_value );
      93            2 :   BOOST_CHECK_THROW( auto failed_reco_ti = from_entry<dunedaq::opmon::TestInfo>(complex_entry),
      94              :                      dunedaq::opmonlib::NameMismatch );
      95            2 :   BOOST_CHECK_NO_THROW( auto succ_reco_ci = from_entry<dunedaq::opmon::ComplexInfo>(complex_entry) );
      96              :   
      97              :  
      98            2 : }
      99              : 
     100              : 
     101            2 : BOOST_AUTO_TEST_CASE(name_setters) {
     102              : 
     103            1 :   const int64_t int_value = 394;
     104            1 :   const float float_value = 3.14;
     105            1 :   const double double_value = 6.28;
     106            1 :   const std::string string_value = "my_test" ;
     107            1 :   const bool bool_value = true;
     108              : 
     109            1 :   dunedaq::opmon::TestInfo ti;
     110              : 
     111            3 :   BOOST_CHECK_THROW( set_value( ti, "non_existing_field", double_value ), 
     112              :                      dunedaq::opmonlib::MissingField );
     113              : 
     114            1 :   set_value( ti, "string_example", string_value );
     115            1 :   BOOST_TEST( string_value == ti.string_example() );
     116              : 
     117            1 :   set_value( ti, "float_example", double_value );
     118            1 :   BOOST_TEST( double_value == ti.float_example() );
     119              :   
     120            1 :   set_value( ti, "int_example", int_value );
     121            1 :   BOOST_TEST( int_value == ti.int_example() );
     122              :   
     123            1 : }
     124              : 
     125              : 
     126              : 
     127              : BOOST_AUTO_TEST_SUITE_END()
     128              : 
        

Generated by: LCOV version 2.0-1