Skip to content

Commit

Permalink
more tests written
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Dec 10, 2024
1 parent 4b4f864 commit 9b2377a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
14 changes: 9 additions & 5 deletions src/ert/resources/forward_models/run_reservoirsimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections import namedtuple
from pathlib import Path
from random import random
from typing import List, Literal, Optional, Union
from typing import List, Literal, Optional, Union, get_args

import resfo

Expand Down Expand Up @@ -43,6 +43,7 @@ def failed_due_to_license_problems(self) -> bool:
return False


Simulators = Literal["flow", "eclipse", "e300"]
EclipseResult = namedtuple("EclipseResult", "errors bugs")
body_sub_pattern = r"(\s^\s@.+$)*"
date_sub_pattern = r"\s+AT TIME\s+(?P<Days>\d+\.\d+)\s+DAYS\s+\((?P<Date>(.+)):\s*$"
Expand Down Expand Up @@ -131,13 +132,17 @@ class RunReservoirSimulator:

def __init__(
self,
simulator: Literal["flow", "eclipse", "e300"],
simulator: Simulators,
version: Optional[str],
ecl_case: Union[Path, str],
num_cpu: int = 1,
check_status: bool = True,
summary_conversion: bool = False,
):
if simulator not in get_args(Simulators):
raise ValueError(
f"Unknown simulator '{simulator}', pick from {get_args(Simulators)}"
)
self.simulator = simulator
self.version: Optional[str] = version

Expand Down Expand Up @@ -169,7 +174,6 @@ def __init__(
self.runner_abspath: str = str(_runner_abspath)

data_file = ecl_case_to_data_file(Path(ecl_case))

if not Path(data_file).exists():
raise IOError(f"No such file: {data_file}")

Expand Down Expand Up @@ -396,15 +400,15 @@ def run_reservoirsimulator(args: List[str]) -> None:
num_cpu=options.num_cpu,
check_status=not options.ignore_errors,
summary_conversion=options.summary_conversion,
).runEclipseX00()
).run_eclipseX00()
else:
RunReservoirSimulator(
"flow",
options.version,
options.ecl_case,
num_cpu=options.num_cpu,
check_status=not options.ignore_errors,
).runFlow()
).run_flow()

except EclError as msg:
print(msg, file=sys.stderr)
Expand Down
24 changes: 24 additions & 0 deletions tests/ert/unit_tests/resources/test_run_eclipse_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ def test_ecl100_binary_can_produce_output(source_root):
assert not Path("SPE1.h5").exists(), "HDF conversion should not be run by default"


@pytest.mark.integration_test
@pytest.mark.usefixtures("use_tmpdir", "e100_env")
@pytest.mark.requires_eclipse
def test_ecl100_binary_can_handle_extra_dots_in_casename(source_root):
"""There is code dealing with file extensions in the Eclipse runner
so it better be tested to work as expected."""
shutil.copy(
source_root / "test-data/ert/eclipse/SPE1.DATA",
"SPE1.DOT.DATA",
)

erun = run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "2019.3", "SPE1.DOT.DATA"
)
erun.run_eclipseX00()

ok_path = Path("SPE1.DOT.OK")
prt_path = Path("SPE1.DOT.PRT")

assert ok_path.exists()
assert prt_path.stat().st_size > 0
assert len(erun.parseErrors()) == 0


@pytest.mark.integration_test
@pytest.mark.requires_eclipse
@pytest.mark.usefixtures("use_tmpdir", "e100_env")
Expand Down
80 changes: 68 additions & 12 deletions tests/ert/unit_tests/resources/test_run_reservoirsimulator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
import stat
import sys
import threading
import time
from pathlib import Path

import numpy as np
import pytest
Expand All @@ -25,25 +29,77 @@
)


def test_runners_are_found_from_path():
# assert different runners are looked for given wanted
# simulator
# assert runtimeoerror if no runner found
pass
@pytest.fixture(name="mocked_eclrun")
def fixture_mocked_eclrun(use_tmpdir, monkeypatch):
"""This puts a eclrun binary in path that cannot do anything."""
eclrun_bin = Path("bin/eclrun")
eclrun_bin.parent.mkdir()
eclrun_bin.write_text("", encoding="utf-8")
eclrun_bin.chmod(eclrun_bin.stat().st_mode | stat.S_IEXEC)
monkeypatch.setenv("PATH", f"bin:{os.environ['PATH']}")


def test_flowrun_can_be_bypassed():
# if flow is in path, then we can bypass
# assert an error if num_cpu is more than 1., not suppported yet.
pass
def test_unknown_simulator():
with pytest.raises(ValueError, match="Unknown simulator"):
run_reservoirsimulator.RunReservoirSimulator(
"bogus_flow", "mocked_version", "bogus_deck.DATA"
)


@pytest.mark.usefixtures("mocked_eclrun")
def test_runner_fails_on_missing_data_file():
pass
with pytest.raises(OSError, match="No such file: NOTEXISTING.DATA"):
run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "mocked_version", "NOTEXISTING.DATA"
)


@pytest.mark.usefixtures("mocked_eclrun")
def test_runner_can_find_deck_without_extension():
Path("DECK.DATA").write_text("FOO", encoding="utf-8")
runner = run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "mocked_version", "DECK"
)
assert runner.data_file == "DECK.DATA"


@pytest.mark.usefixtures("mocked_eclrun")
def test_runner_can_find_lowercase_deck_without_extension():
Path("deck.data").write_text("FOO", encoding="utf-8")
runner = run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "mocked_version", "deck"
)
assert runner.data_file == "deck.data"


@pytest.mark.skipif(
sys.platform.startswith("darwin"), reason="Case insensitive filesystem on MacOS"
)
@pytest.mark.usefixtures("mocked_eclrun")
def test_runner_cannot_find_mixed_case_decks():
Path("deck.DATA").write_text("FOO", encoding="utf-8")
with pytest.raises(OSError, match="No such file: deck.data"):
run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "mocked_version", "deck"
)

def test_ecl_case_from_data_file():
pass

@pytest.mark.usefixtures("mocked_eclrun")
@pytest.mark.parametrize(
"data_path, expected",
[
("DECK.DATA", "DECK"),
("foo/DECK.DATA", "DECK"),
("foo/deck.data", "deck"),
],
)
def test_runner_can_extract_base_name(data_path: str, expected: str):
Path(data_path).parent.mkdir(exist_ok=True)
Path(data_path).write_text("FOO", encoding="utf-8")
runner = run_reservoirsimulator.RunReservoirSimulator(
"eclipse", "mocked_version", data_path
)
assert runner.base_name == expected


@pytest.mark.usefixtures("use_tmpdir")
Expand Down

0 comments on commit 9b2377a

Please sign in to comment.