From b3af680ea55f2ed031771459e542416624da3d8c Mon Sep 17 00:00:00 2001 From: mferrera Date: Thu, 19 Dec 2024 09:17:09 +0100 Subject: [PATCH] ENH: validate inplace volumes with Pydantic --- src/fmu/dataio/export/rms/inplace_volumes.py | 12 +++++++----- .../test_export_rms_volumetrics.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/fmu/dataio/export/rms/inplace_volumes.py b/src/fmu/dataio/export/rms/inplace_volumes.py index 60ad4619d..6ec6ed18d 100644 --- a/src/fmu/dataio/export/rms/inplace_volumes.py +++ b/src/fmu/dataio/export/rms/inplace_volumes.py @@ -12,6 +12,7 @@ import fmu.dataio as dio from fmu.dataio._logging import null_logger from fmu.dataio._model.enums import Classification +from fmu.dataio._products.inplace_volumes import InplaceVolumesResult from fmu.dataio.export import _enums from fmu.dataio.export._decorators import experimental from fmu.dataio.export._export_result import ExportResult, ExportResultItem @@ -210,7 +211,7 @@ def _transform_and_add_fluid_column_to_table( ) # add the fluid as column entry instead - fluid_table[_enums.InplaceVolumes.FLUID_COLUMN] = fluid + fluid_table[_enums.InplaceVolumes.FLUID_COLUMN.value] = fluid tables.append(fluid_table) @@ -244,12 +245,10 @@ def _validate_table(self) -> None: _logger.debug("Validating the dataframe...") has_oil = ( - _enums.InplaceVolumes.Fluid.oil.value - in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values + "oil" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values ) has_gas = ( - _enums.InplaceVolumes.Fluid.gas.value - in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values + "gas" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values ) # check that one of oil and gas fluids are present @@ -285,6 +284,9 @@ def _validate_table(self) -> None: "rerun the volumetric job before export." ) + df = self._dataframe.replace(np.nan, None).to_dict(orient="records") + InplaceVolumesResult.model_validate(df) + def _export_volume_table(self) -> ExportResult: """Do the actual volume table export using dataio setup.""" diff --git a/tests/test_export_rms/test_export_rms_volumetrics.py b/tests/test_export_rms/test_export_rms_volumetrics.py index ef90d4718..018b02b98 100644 --- a/tests/test_export_rms/test_export_rms_volumetrics.py +++ b/tests/test_export_rms/test_export_rms_volumetrics.py @@ -8,6 +8,7 @@ import pandas as pd import pyarrow.parquet as pq import pytest +from pydantic import ValidationError import fmu.dataio as dataio from fmu.dataio._logging import null_logger @@ -425,6 +426,22 @@ def test_validate_table_has_gas_and_giip(exportvolumetrics, voltable_standard): exportvolumetrics._validate_table() +def test_validate_table_against_pydantic_model_before_export( + exportvolumetrics, voltable_standard +): + """Test that the validation fails if the volumes table does not conform to the + Pydantic model specifying the result.""" + + df = voltable_standard.copy() + exportvolumetrics._dataframe = df + exportvolumetrics._validate_table() + + df["PORV"] = df["PORV"].replace(0.0, "a") + exportvolumetrics._dataframe = df + with pytest.raises(ValidationError, match="Input should be a valid number"): + exportvolumetrics._validate_table() + + @inside_rms def test_rms_volumetrics_export_config_missing( mock_project_variable,