forked from equinor/fmu-dataio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: Add create_case_metadata integration test
This test runs ert with a minimal configuration that just ensure the workflow functions correct and some patched data ends up in the fmu case export. It does not do more rigorous validating of the exported data.
- Loading branch information
Showing
12 changed files
with
155 additions
and
72 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import pathlib | ||
import shutil | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def base_ert_config() -> str: | ||
return dedent( | ||
r""" | ||
DEFINE <USER> user | ||
DEFINE <SCRATCH> $DATAIO_TMP_PATH/scratch | ||
DEFINE <CASE_DIR> snakeoil | ||
DEFINE <SUMO_ENV> dev | ||
DEFINE <SUMO_CASEPATH> <SCRATCH>/<USER>/<CASE_DIR> | ||
NUM_REALIZATIONS 5 | ||
QUEUE_SYSTEM LOCAL | ||
QUEUE_OPTION LOCAL MAX_RUNNING 5 | ||
RANDOM_SEED 123456 | ||
RUNPATH <SCRATCH>/<USER>/<CASE_DIR>/realization-<IENS>/iter-<ITER>/ | ||
""" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def fmu_snakeoil_project(tmp_path, monkeypatch, base_ert_config): | ||
monkeypatch.setenv("DATAIO_TMP_PATH", str(tmp_path)) | ||
os.makedirs(tmp_path / "eclipse/model") | ||
for app in ("ert", "rms"): | ||
os.makedirs(tmp_path / f"{app}/bin") | ||
os.makedirs(tmp_path / f"{app}/input") | ||
os.makedirs(tmp_path / f"{app}/model") | ||
os.makedirs(tmp_path / "rms/model/snakeoil.rms13.1.2") | ||
os.makedirs(tmp_path / "fmuconfig/output") | ||
shutil.copy( | ||
pathlib.Path(".").absolute() | ||
/ "tests/data/drogon/global_config2/global_variables.yml", | ||
tmp_path / "fmuconfig/output/", | ||
) | ||
|
||
os.makedirs(tmp_path / "ert/bin/workflows") | ||
pathlib.Path(tmp_path / "ert/bin/workflows/xhook_create_case_metadata").write_text( | ||
"WF_CREATE_CASE_METADATA " | ||
"<SCRATCH>/<USER>/<CASE_DIR> " # ert case root | ||
"<CONFIG_PATH> " # ert config path | ||
"<CASE_DIR> " # ert case dir | ||
"<USER>", # ert username | ||
encoding="utf-8", | ||
) | ||
pathlib.Path(tmp_path / "ert/model/snakeoil.ert").write_text( | ||
base_ert_config, encoding="utf-8" | ||
) | ||
return tmp_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pathlib | ||
import subprocess | ||
|
||
import yaml | ||
|
||
|
||
def test_create_case_metadata_runs_successfully(fmu_snakeoil_project, monkeypatch): | ||
monkeypatch.chdir(fmu_snakeoil_project / "ert/model") | ||
with open("snakeoil.ert", "a", encoding="utf-8") as f: | ||
f.writelines( | ||
[ | ||
"LOAD_WORKFLOW ../bin/workflows/xhook_create_case_metadata\n" | ||
"HOOK_WORKFLOW xhook_create_case_metadata PRE_SIMULATION\n" | ||
] | ||
) | ||
run_result = subprocess.run( | ||
["ert", "test_run", "snakeoil.ert", "--disable-monitoring"], | ||
) | ||
assert run_result.returncode == 0 | ||
|
||
fmu_case = ( | ||
pathlib.Path(fmu_snakeoil_project) | ||
/ "scratch/user/snakeoil/share/metadata/fmu_case.yml" | ||
) | ||
assert fmu_case.exists() | ||
with open(fmu_case, encoding="utf-8") as f: | ||
fmu_case_yml = yaml.safe_load(f) | ||
|
||
assert fmu_case_yml["fmu"]["case"]["name"] == "snakeoil" | ||
assert fmu_case_yml["fmu"]["case"]["user"]["id"] == "user" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import datetime | ||
from functools import wraps | ||
from pathlib import Path | ||
|
||
import pytest | ||
import yaml | ||
|
||
|
||
def inside_rms(func): | ||
@pytest.mark.usefixtures("set_export_data_inside_rms", "set_environ_inside_rms") | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def _parse_yaml(yaml_path): | ||
"""Parse the filename as json, return data""" | ||
with open(yaml_path, encoding="utf-8") as stream: | ||
data = yaml.safe_load(stream) | ||
|
||
return _isoformat_all_datetimes(data) | ||
|
||
|
||
def _isoformat_all_datetimes(indate): | ||
"""Recursive function to isoformat all datetimes in a dictionary""" | ||
|
||
if isinstance(indate, list): | ||
return [_isoformat_all_datetimes(i) for i in indate] | ||
|
||
if isinstance(indate, dict): | ||
return {key: _isoformat_all_datetimes(indate[key]) for key in indate} | ||
|
||
if isinstance(indate, (datetime.datetime, datetime.date)): | ||
return indate.isoformat() | ||
|
||
return indate | ||
|
||
|
||
def _metadata_examples(): | ||
return { | ||
path.name: _isoformat_all_datetimes(_parse_yaml(path)) | ||
for path in Path(".").absolute().glob("schema/definitions/0.8.0/examples/*.yml") | ||
} |