Skip to content

Commit

Permalink
TST: Fix inproper environment framework (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt authored May 8, 2024
1 parent 5c12cb4 commit 9a08f4e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ def _fmu_run1_env_variables(monkeypatch, usepath="", case_only=False):
logger.debug("Setting env %s as %s", key, env_value)


def remove_ert_env(monkeypatch):
for key in ERTRUN_ENV_FULLRUN:
monkeypatch.delenv(key, raising=False)


def set_ert_env_forward(monkeypatch):
for key, val in ERTRUN_ENV_FORWARD.items():
monkeypatch.setenv(key, val)


def set_ert_env_prehook(monkeypatch):
for key, val in ERTRUN_ENV_PREHOOK.items():
monkeypatch.setenv(key, val)


@pytest.fixture(scope="function")
def fmurun(tmp_path_factory, monkeypatch, rootpath):
"""A tmp folder structure for testing; here a new fmurun without case metadata."""
Expand Down
13 changes: 8 additions & 5 deletions tests/test_units/test_objectdataprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from fmu.dataio.providers.objectdata._xtgeo import RegularSurfaceDataProvider

from ..conftest import remove_ert_env, set_ert_env_prehook
from ..utils import inside_rms


Expand Down Expand Up @@ -128,7 +129,7 @@ def test_objectdata_provider_factory_raises_on_unknown(edataobj1):


def test_regsurf_preprocessed_observation(
fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf
fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch
):
"""Test generating pre-realization surfaces that comes to share/preprocessed.
Expand All @@ -150,7 +151,7 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):
)
return edata, edata.export(regsurf)

def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath):
"""Run FMU workflow, using the preprocessed data as case data.
When re-using metadata, the input object to dataio shall not be a XTGeo or
Expand All @@ -161,9 +162,9 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
But it requires that valid metadata for that file is found. The rule for
merging is currently defaulted to "preprocessed".
"""
os.chdir(fmurun_w_casemetadata)
os.chdir(fmurun_prehook)

casepath = fmurun_w_casemetadata.parent.parent
casepath = fmurun_prehook
edata = dataio.ExportData(
config=rmsglobalconfig,
fmu_context="case",
Expand All @@ -174,6 +175,8 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
return edata.generate_metadata(surfacepath)

# run two stage process
remove_ert_env(monkeypatch)
edata, mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
case_meta = _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf)
set_ert_env_prehook(monkeypatch)
case_meta = _run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf)
assert edata._metadata["data"] == case_meta["data"]
65 changes: 40 additions & 25 deletions tests/test_units/test_prerealization_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@
import pytest
from fmu.dataio import _utils as utils

from ..conftest import remove_ert_env, set_ert_env_prehook
from ..utils import inside_rms

logger = logging.getLogger(__name__)


def test_regsurf_case_observation(fmurun_w_casemetadata, rmsglobalconfig, regsurf):
def test_regsurf_case_observation(fmurun_prehook, rmsglobalconfig, regsurf):
"""Test generating pre-realization surfaces that comes right to case.
Notice the difference between this use-case and the 'preprocessed' example later!
"""
logger.info("Active folder is %s", fmurun_w_casemetadata)
logger.info("Active folder is %s", fmurun_prehook)

os.chdir(fmurun_w_casemetadata)
os.chdir(fmurun_prehook)

edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
fmu_context="case",
casepath=fmurun_prehook,
name="mymap",
content="depth",
is_observation=True,
Expand All @@ -50,7 +52,7 @@ def test_regsurf_case_observation(fmurun_w_casemetadata, rmsglobalconfig, regsur


def test_regsurf_preprocessed_observation(
fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf
fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch
):
"""Test generating pre-realization surfaces that comes to share/preprocessed.
Expand Down Expand Up @@ -84,7 +86,7 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):

return edata.export(regsurf)

def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath):
"""Run FMU workflow, using the preprocessed data as case data.
When re-using metadata, the input object to dataio shall not be a XTGeo or
Expand All @@ -95,10 +97,10 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
But it requires that valid metadata for that file is found. The rule for
merging is currently defaulted to "preprocessed".
"""
os.chdir(fmurun_w_casemetadata)
logger.info("Active folder is %s", fmurun_w_casemetadata)
os.chdir(fmurun_prehook)
logger.info("Active folder is %s", fmurun_prehook)

casepath = fmurun_w_casemetadata.parent.parent
casepath = fmurun_prehook

edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
Expand Down Expand Up @@ -132,8 +134,10 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
).exists()

# run two stage process
remove_ert_env(monkeypatch)
mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
_run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf)
set_ert_env_prehook(monkeypatch)
_run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf)

logger.info("Preprocessed surface is %s", mysurf)

Expand All @@ -156,7 +160,7 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
],
)
def test_regsurf_preprocessed_obs_vary_name_tagname(
fmurun_w_casemetadata,
fmurun_prehook,
rmssetup,
rmsglobalconfig,
regsurf,
Expand All @@ -166,6 +170,7 @@ def test_regsurf_preprocessed_obs_vary_name_tagname(
tagname_merge,
exproot1,
exproot2,
monkeypatch,
):
"""Check that current name and/or tagname are propegated or updated."""

Expand Down Expand Up @@ -203,7 +208,7 @@ def _export_data_from_rms(
return edata.export(regsurf)

def _run_case_fmu(
fmurun_w_casemetadata,
fmurun_prehook,
rmsglobalconfig,
surfacepath,
name_merge,
Expand All @@ -212,13 +217,14 @@ def _run_case_fmu(
):
"""Run FMU workflow, using the preprocessed data on a subfolder."""

os.chdir(fmurun_w_casemetadata)
logger.info("Active folder is %s", fmurun_w_casemetadata)
os.chdir(fmurun_prehook)
logger.info("Active folder is %s", fmurun_prehook)

edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
fmu_context="case",
content="depth",
casepath=fmurun_prehook,
is_observation=True,
name=name_merge,
tagname=tagname_merge,
Expand All @@ -229,11 +235,13 @@ def _run_case_fmu(
metadata = edata.generate_metadata(surfacepath)
assert metadata["file"]["relative_path"] == f"{prefix}/{exproot2}--{dates}.gri"

remove_ert_env(monkeypatch)
mysurf = _export_data_from_rms(
rmssetup, rmsglobalconfig, regsurf, name_pre, tagname_pre, exproot1
)
set_ert_env_prehook(monkeypatch)
_run_case_fmu(
fmurun_w_casemetadata,
fmurun_prehook,
rmsglobalconfig,
mysurf,
name_merge,
Expand All @@ -243,7 +251,7 @@ def _run_case_fmu(


def test_regsurf_preprocessed_observation_subfolder(
fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf
fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch
):
"""As previous test, but with data using subfolder option.
Expand Down Expand Up @@ -279,15 +287,16 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):

return edata.export(regsurf)

def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath, subf=None):
def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath, subf=None):
"""Run FMU workflow, using the preprocessed data on a subfolder."""

os.chdir(fmurun_w_casemetadata)
logger.info("Active folder is %s", fmurun_w_casemetadata)
os.chdir(fmurun_prehook)
logger.info("Active folder is %s", fmurun_prehook)

edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
fmu_context="case",
casepath=fmurun_prehook,
content="depth",
name="pre_v3",
is_observation=True,
Expand All @@ -307,9 +316,12 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath, subf=None
assert "merged" in metadata["tracklog"][-1]["event"]

# run two stage process
remove_ert_env(monkeypatch)
mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
_run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf)
_run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf, subf="xxxx")

set_ert_env_prehook(monkeypatch)
_run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf)
_run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf, subf="xxxx")


@inside_rms
Expand Down Expand Up @@ -356,7 +368,7 @@ def test_preprocessed_with_rel_forcefolder_ok(rmssetup, rmsglobalconfig, regsurf


def test_access_settings_retained(
fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf
fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch
):
"""Test that access level put on pre-processed data are retained when the
metadata is being completed during later FMU run.
Expand Down Expand Up @@ -388,15 +400,16 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):

return edata.export(regsurf)

def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath):
"""Run FMU workflow, test that access is retained from preprocessed."""

os.chdir(fmurun_w_casemetadata)
logger.info("Active folder is %s", fmurun_w_casemetadata)
os.chdir(fmurun_prehook)
logger.info("Active folder is %s", fmurun_prehook)

edata = dataio.ExportData(
config=rmsglobalconfig,
fmu_context="case",
casepath=fmurun_prehook,
content="depth",
name="MyName",
)
Expand All @@ -406,5 +419,7 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
assert metadata["access"]["classification"] == "restricted"

# run two stage process
remove_ert_env(monkeypatch)
surfacepath = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
_run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath)
set_ert_env_prehook(monkeypatch)
_run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath)
6 changes: 3 additions & 3 deletions tests/test_units/test_rms_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fmu.dataio.readers as readers
import pandas as pd
import pytest
from fmu.dataio._definitions import FmuContext
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio.dataio import ValidationError

Expand Down Expand Up @@ -373,7 +374,7 @@ def test_cube_export_file_set_name_as_observation_forcefolder(
def test_cube_export_as_case(rmssetup, rmsglobalconfig, cube):
"""Export the cube to file with correct metadata and name, is_observation.
In addition, try fmu_conext=case
In addition try fmu_context=case; when inside rms this should be reset to "non-fmu"
"""
logger.info("Active folder is %s", rmssetup)
os.chdir(rmssetup)
Expand All @@ -390,7 +391,7 @@ def test_cube_export_as_case(rmssetup, rmsglobalconfig, cube):
is_observation=True,
)
logger.info("Output %s", output)

assert edata.fmu_context == FmuContext.NON_FMU
assert str(output) == str(
(edata._rootpath / "share/observations/cubes/mycube.segy").resolve()
)
Expand Down Expand Up @@ -503,7 +504,6 @@ def test_cube_export_as_observation_forcefolder_w_subfolder_case(
cube,
name="MyCube",
is_observation=True,
fmu_context="case",
forcefolder="seismic/xxx",
)
logger.info("Output after force is %s", output)
Expand Down

0 comments on commit 9a08f4e

Please sign in to comment.