mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Grid Calculator Export: improve test and error handling.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include <fstream>
|
||||
|
||||
#include <tomlplusplus/toml.hpp>
|
||||
#include <utility>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@@ -38,44 +39,62 @@ std::pair<std::vector<RifGridCalculation>, std::string> RifGridCalculationImport
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::pair<std::vector<RifGridCalculation>, std::string> RifGridCalculationImporter::readFromStream( std::istream& stream )
|
||||
{
|
||||
toml::table tbl = toml::parse( stream );
|
||||
|
||||
auto calculationsVector = tbl["grid-calculation"];
|
||||
|
||||
std::vector<RifGridCalculation> calculations;
|
||||
|
||||
if ( toml::array* arr = calculationsVector.as_array() )
|
||||
try
|
||||
{
|
||||
for ( auto&& a : *arr )
|
||||
toml::table tbl = toml::parse( stream );
|
||||
|
||||
auto calculationsVector = tbl["grid-calculation"];
|
||||
|
||||
std::vector<RifGridCalculation> calculations;
|
||||
|
||||
if ( toml::array* arr = calculationsVector.as_array() )
|
||||
{
|
||||
RifGridCalculation calculation;
|
||||
if ( toml::table* calc = a.as_table() )
|
||||
for ( auto&& a : *arr )
|
||||
{
|
||||
calculation.description = calc->at_path( "description" ).as_string()->value_or( "" );
|
||||
calculation.expression = calc->at_path( "expression" ).as_string()->value_or( "" );
|
||||
calculation.unit = calc->at_path( "unit" ).as_string()->value_or( "" );
|
||||
|
||||
if ( toml::array* vars = calc->at_path( "variables" ).as_array() )
|
||||
RifGridCalculation calculation;
|
||||
if ( toml::table* calc = a.as_table() )
|
||||
{
|
||||
std::vector<RifGridCalculationVariable> variables;
|
||||
for ( auto&& v : *vars )
|
||||
{
|
||||
if ( toml::table* var = v.as_table() )
|
||||
{
|
||||
RifGridCalculationVariable variable;
|
||||
variable.name = var->at_path( "name" ).as_string()->value_or( "" );
|
||||
variable.resultType = var->at_path( "type" ).as_string()->value_or( "" );
|
||||
variable.resultVariable = var->at_path( "variable" ).as_string()->value_or( "" );
|
||||
variables.push_back( variable );
|
||||
}
|
||||
}
|
||||
calculation.variables = variables;
|
||||
}
|
||||
calculation.description = calc->at_path( "description" ).value_or<std::string>( "" );
|
||||
if ( calculation.description.empty() ) throw std::runtime_error( "Missing description." );
|
||||
|
||||
calculations.push_back( calculation );
|
||||
calculation.expression = calc->at_path( "expression" ).value_or<std::string>( "" );
|
||||
if ( calculation.expression.empty() ) throw std::runtime_error( "Missing expression." );
|
||||
|
||||
calculation.unit = calc->at_path( "unit" ).value_or<std::string>( "" );
|
||||
|
||||
if ( toml::array* vars = calc->at_path( "variables" ).as_array() )
|
||||
{
|
||||
std::vector<RifGridCalculationVariable> variables;
|
||||
for ( auto&& v : *vars )
|
||||
{
|
||||
if ( toml::table* var = v.as_table() )
|
||||
{
|
||||
RifGridCalculationVariable variable;
|
||||
variable.name = var->at_path( "name" ).value_or<std::string>( "" );
|
||||
variable.resultType = var->at_path( "type" ).value_or<std::string>( "" );
|
||||
variable.resultVariable = var->at_path( "variable" ).value_or<std::string>( "" );
|
||||
if ( variable.name.empty() || variable.resultType.empty() || variable.resultVariable.empty() )
|
||||
throw std::runtime_error( "Incomplete variable: Missing either name, result type or result variable." );
|
||||
variables.push_back( variable );
|
||||
}
|
||||
}
|
||||
calculation.variables = variables;
|
||||
}
|
||||
|
||||
calculations.push_back( calculation );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { calculations, "" };
|
||||
if ( calculations.empty() )
|
||||
{
|
||||
return { calculations, "No calculations imported." };
|
||||
}
|
||||
|
||||
return { calculations, "" };
|
||||
}
|
||||
catch ( const std::runtime_error& error )
|
||||
{
|
||||
return { {}, error.what() };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,3 +65,57 @@ TEST( RifGridCalculationIO, importAndExport )
|
||||
ASSERT_EQ( calculations[0].variables[v].resultVariable, importedCalculations[0].variables[v].resultVariable );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifGridCalculationIO, importEmptyStream )
|
||||
{
|
||||
std::stringstream stream;
|
||||
auto [calculations, errorMessage] = RifGridCalculationImporter::readFromStream( stream );
|
||||
ASSERT_EQ( 0u, calculations.size() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifGridCalculationIO, importNotToml )
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "this is not valid toml";
|
||||
|
||||
auto [calculations, errorMessage] = RifGridCalculationImporter::readFromStream( stream );
|
||||
ASSERT_EQ( 0u, calculations.size() );
|
||||
ASSERT_FALSE( errorMessage.empty() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifGridCalculationIO, importWrongToml )
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "[library]\n"
|
||||
<< "book = \"book name\"\n"
|
||||
<< "authors = [\"Author Name\"]\n"
|
||||
<< "isbn = \"1234567\"\n";
|
||||
|
||||
auto [calculations, errorMessage] = RifGridCalculationImporter::readFromStream( stream );
|
||||
ASSERT_EQ( 0u, calculations.size() );
|
||||
ASSERT_FALSE( errorMessage.empty() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifGridCalculationIO, importMissingDescriptionToml )
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "[[grid-calculation]]\n"
|
||||
<< "description = 'MY_ASNWER ( NORNE_ATW2013_RFTPLT_V2 : PRESSURE, NORNE_ATW2013_RFTPLT_V2 : PORO )'\n"
|
||||
<< "unit = ''\n";
|
||||
|
||||
auto [calculations, errorMessage] = RifGridCalculationImporter::readFromStream( stream );
|
||||
ASSERT_EQ( 0u, calculations.size() );
|
||||
ASSERT_FALSE( errorMessage.empty() );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user