Skip to content

Commit

Permalink
Add test to check if logger works from embedded Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
lisajulia committed Jan 23, 2025
1 parent 7cae3be commit dbc2b4e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ if(ENABLE_ECL_OUTPUT)
tests/SPE1CASE2_RESTART_SKIPREST.DATA
tests/SPE1CASE2.X0060
tests/PYACTION.DATA
tests/logger.py
tests/0A4_GRCTRL_LRAT_LRAT_GGR_BASE_MODEL2_MSW_ALL.DATA
tests/MOD4_TEST_IGRP-DATA.DATA
tests/2_WLIFT_MODEL5_NOINC.DATA
Expand Down
10 changes: 10 additions & 0 deletions tests/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# The python code in this file does the same as ACT-01 to ACT-05 of opm-tests/actionx/ACTIONX_WCONPROD.DATA
import opm_embedded

opm_embedded.OpmLog.info("Info from logger.py!")
opm_embedded.OpmLog.warning("Warning from logger.py!")
opm_embedded.OpmLog.error("Error from logger.py!")
opm_embedded.OpmLog.problem("Problem from logger.py!")
opm_embedded.OpmLog.bug("Bug from logger.py!")
opm_embedded.OpmLog.debug("Debug from logger.py!")
opm_embedded.OpmLog.note("Note from logger.py!")
35 changes: 35 additions & 0 deletions tests/parser/PYACTION.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include "config.h"
#include <memory>
#include <sstream>

#define BOOST_TEST_MODULE PY_ACTION_TESTER
#include <boost/test/unit_test.hpp>
Expand All @@ -30,6 +31,9 @@
#include <opm/input/eclipse/Schedule/Schedule.hpp>
#include <opm/input/eclipse/Schedule/SummaryState.hpp>

#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/OpmLog/StreamLog.hpp>

using namespace Opm;

BOOST_AUTO_TEST_CASE(ParsePYACTION) {
Expand Down Expand Up @@ -86,5 +90,36 @@ BOOST_AUTO_TEST_CASE(ParsePYACTION_ModuleMissing) {
BOOST_CHECK_THROW(Action::PyAction(python , "ACT2", run_count, missing_module), std::invalid_argument);
}

BOOST_AUTO_TEST_CASE(PYACTION_Log) {
std::ostringstream log_stream; // Custom stream for capturing log messages
std::shared_ptr<Opm::StreamLog> stream_log = std::make_shared<Opm::StreamLog>(log_stream, Opm::Log::DefaultMessageTypes);

OpmLog::addBackend( "STREAM" , stream_log);

Parser parser;
auto deck = parser.parseFile("PYACTION.DATA");
const std::string& logger_module = deck.makeDeckPath("logger.py");

auto python = std::make_shared<Python>();
Action::PyAction pyaction(python , "ACT3", Action::PyAction::RunCount::unlimited, logger_module);

const std::function<void(const std::string&, const std::vector<std::string>&)> actionx_callback;
EclipseState state;
Schedule schedule;
SummaryState summary_state;

pyaction.run(state, schedule, 0, summary_state, actionx_callback);
std::string log_output = log_stream.str();

BOOST_CHECK(log_output.find("Info from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Warning from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Error from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Problem from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Bug from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Debug from logger.py!") != std::string::npos);
BOOST_CHECK(log_output.find("Note from logger.py!") != std::string::npos);

}

#endif

0 comments on commit dbc2b4e

Please sign in to comment.