From e31d2414dc42f8eb0d026998076962213226f629 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 | 14 +++++++++++--- .../test_export_rms_volumetrics.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/fmu/dataio/export/rms/inplace_volumes.py b/src/fmu/dataio/export/rms/inplace_volumes.py index 77bd63d02..ecc3e1500 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 @@ -172,7 +173,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) @@ -204,8 +205,12 @@ def _validate_table(self) -> None: """ _logger.debug("Validating the dataframe...") - has_oil = "oil" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values - has_gas = "gas" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values + has_oil = ( + "oil" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values + ) + has_gas = ( + "gas" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values + ) # check that one of oil and gas fluids are present if not (has_oil or has_gas): @@ -234,6 +239,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 4a396499f..a5e43ef91 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 @@ -268,6 +269,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,