Skip to content

Commit

Permalink
Refactor everest math func fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Oct 4, 2024
1 parent d1c3a88 commit 43bac81
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 214 deletions.
13 changes: 13 additions & 0 deletions tests/everest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from everest.config.control_config import ControlConfig
from tests.everest.utils import relpath


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -82,3 +83,15 @@ def control_config(
config = deepcopy(control_data_no_variables)
config["variables"] = request.param
return ControlConfig.model_validate(config)


@pytest.fixture
def copy_math_func_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("..", "..", "test-data", "everest", "math_func")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def change_to_tmpdir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
11 changes: 5 additions & 6 deletions tests/everest/entry_points/test_config_branch_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from everest.config import EverestConfig
from everest.config_file_loader import load_yaml
from everest.config_keys import ConfigKeys as CK
from tests.everest.utils import relpath, tmpdir
from tests.everest.utils import relpath

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE = "config_advanced.yml"
CACHED_SEBA_FOLDER = relpath("test_data", "cached_results_config_advanced")

Expand All @@ -22,8 +21,7 @@
new_callable=PropertyMock,
return_value=CACHED_SEBA_FOLDER,
)
@tmpdir(CONFIG_PATH)
def test_config_branch_entry(get_opt_output_dir_mock):
def test_config_branch_entry(get_opt_output_dir_mock, copy_math_func_test_data_to_tmp):
new_config_file_name = "new_restart_config.yml"
batch_id = 1

Expand Down Expand Up @@ -65,8 +63,9 @@ def test_config_branch_entry(get_opt_output_dir_mock):
new_callable=PropertyMock,
return_value=CACHED_SEBA_FOLDER,
)
@tmpdir(CONFIG_PATH)
def test_config_branch_preserves_config_section_order(get_opt_output_dir_mock):
def test_config_branch_preserves_config_section_order(
get_opt_output_dir_mock, copy_math_func_test_data_to_tmp
):
new_config_file_name = "new_restart_config.yml"
batch_id = 1

Expand Down
43 changes: 22 additions & 21 deletions tests/everest/entry_points/test_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
from everest.jobs import shell_commands
from everest.simulator import JOB_SUCCESS
from ieverest.bin.ieverest_script import ieverest_entry
from tests.everest.utils import capture_streams, relpath, tmpdir
from tests.everest.utils import capture_streams

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"


Expand Down Expand Up @@ -80,13 +79,13 @@ def run_detached_monitor_mock(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_debug(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
caplog,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with --debug"""
with caplog.at_level(logging.DEBUG):
Expand All @@ -111,12 +110,12 @@ def test_everest_entry_debug(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest in detached mode"""
everest_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -134,13 +133,13 @@ def test_everest_entry(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_already_run(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, when an optimization has already run"""
# optimization already run, notify the user
Expand Down Expand Up @@ -171,11 +170,11 @@ def test_everest_entry_detached_already_run(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_already_run_monitor(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, when an optimization has already run"""
# optimization already run, notify the user
Expand All @@ -199,7 +198,6 @@ def test_everest_entry_detached_already_run_monitor(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_running(
everserver_status_mock,
wait_for_server_to_stop_mock,
Expand All @@ -209,6 +207,7 @@ def test_everest_entry_detached_running(
start_monitor_mock,
server_is_running_mock_kill_script,
server_is_running_mock_everest_script,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running"""
# can't start a new run if one is already running
Expand Down Expand Up @@ -248,11 +247,11 @@ def test_everest_entry_detached_running(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_running_monitor(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running, monitoring"""
# Attach to a running optimization.
Expand All @@ -269,11 +268,11 @@ def test_everest_entry_detached_running_monitor(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_monitor_no_run(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running, monitoring"""
# Attach to a running optimization.
Expand All @@ -300,7 +299,6 @@ def test_everest_entry_monitor_no_run(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
Expand All @@ -309,6 +307,7 @@ def test_everest_entry_show_all_jobs(
start_server_mock,
wait_for_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with --show-all-jobs"""

Expand All @@ -335,7 +334,6 @@ def test_everest_entry_show_all_jobs(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_no_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
Expand All @@ -344,6 +342,7 @@ def test_everest_entry_no_show_all_jobs(
start_server_mock,
wait_for_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest without --show-all-jobs"""

Expand Down Expand Up @@ -372,13 +371,13 @@ def test_everest_entry_no_show_all_jobs(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_monitor_entry_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
query_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with and without --show-all-jobs"""

Expand All @@ -404,13 +403,13 @@ def test_monitor_entry_show_all_jobs(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_monitor_entry_no_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
query_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest without --show-all-jobs"""

Expand All @@ -436,9 +435,11 @@ def test_monitor_entry_no_show_all_jobs(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@tmpdir(CONFIG_PATH)
def test_exception_raised_when_server_run_fails(
start_server_mock, wait_for_server_mock, start_monitor_mock
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
with pytest.raises(SystemExit, match="Reality was ripped to shreds!"):
everest_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -453,9 +454,8 @@ def test_exception_raised_when_server_run_fails(
error="Reality was ripped to shreds!",
),
)
@tmpdir(CONFIG_PATH)
def test_exception_raised_when_server_run_fails_monitor(
start_monitor_mock, server_is_running_mock
start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp
):
with pytest.raises(SystemExit, match="Reality was ripped to shreds!"):
monitor_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -467,9 +467,11 @@ def test_exception_raised_when_server_run_fails_monitor(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@tmpdir(CONFIG_PATH)
def test_complete_status_for_normal_run(
start_server_mock, wait_for_server_mock, start_monitor_mock
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
everest_entry([CONFIG_FILE_MINIMAL])
config = EverestConfig.load_file(CONFIG_FILE_MINIMAL)
Expand All @@ -486,9 +488,8 @@ def test_complete_status_for_normal_run(
"everest.bin.monitor_script.run_detached_monitor",
side_effect=run_detached_monitor_mock,
)
@tmpdir(CONFIG_PATH)
def test_complete_status_for_normal_run_monitor(
start_monitor_mock, server_is_running_mock
start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp
):
monitor_entry([CONFIG_FILE_MINIMAL])
config = EverestConfig.load_file(CONFIG_FILE_MINIMAL)
Expand Down
18 changes: 7 additions & 11 deletions tests/everest/entry_points/test_everexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
tmpdir,
)

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"

CONFIG_FILE_MOCKED_TEST_CASE = "mocked_multi_batch.yml"
Expand Down Expand Up @@ -71,8 +70,7 @@ def cache_dir(request, monkeypatch):


@patch("everest.bin.utils.export_with_progress", side_effect=export_mock)
@tmpdir(CONFIG_PATH)
def test_everexport_entry_run(mocked_func):
def test_everexport_entry_run(mocked_func, copy_math_func_test_data_to_tmp):
"""Test running everexport with not flags"""
# NOTE: there is probably a bug concerning output folders. Everexport
# seems to assume that the folder where the file will be saved exists.
Expand All @@ -88,8 +86,7 @@ def test_everexport_entry_run(mocked_func):


@patch("everest.bin.utils.export_with_progress", side_effect=empty_mock)
@tmpdir(CONFIG_PATH)
def test_everexport_entry_empty(mocked_func):
def test_everexport_entry_empty(mocked_func, copy_math_func_test_data_to_tmp):
"""Test running everexport with no data"""
# NOTE: When there is no data (ie, the optimization has not yet run)
# the current behavior is to create an empty .csv file. It is arguable
Expand All @@ -112,9 +109,10 @@ def test_everexport_entry_empty(mocked_func):
side_effect=validate_export_mock,
)
@patch("everest.bin.utils.export")
@tmpdir(CONFIG_PATH)
@pytest.mark.fails_on_macos_github_workflow
def test_everexport_entry_batches(mocked_func, validate_export_mock):
def test_everexport_entry_batches(
mocked_func, validate_export_mock, copy_math_func_test_data_to_tmp
):
"""Test running everexport with the --batches flag"""
everexport_entry([CONFIG_FILE_MINIMAL, "--batches", "0", "2"])

Expand All @@ -135,8 +133,7 @@ def check_export_batches(config: EverestConfig):


@patch("everest.bin.everexport_script.export_to_csv")
@tmpdir(CONFIG_PATH)
def test_everexport_entry_no_export(mocked_func):
def test_everexport_entry_no_export(mocked_func, copy_math_func_test_data_to_tmp):
"""Test running everexport on config file with skip_export flag
set to true"""

Expand All @@ -157,8 +154,7 @@ def test_everexport_entry_no_export(mocked_func):


@patch("everest.bin.everexport_script.export_to_csv")
@tmpdir(CONFIG_PATH)
def test_everexport_entry_empty_export(mocked_func):
def test_everexport_entry_empty_export(mocked_func, copy_math_func_test_data_to_tmp):
"""Test running everexport on config file with empty export section"""

# Add empty export section to config file
Expand Down
13 changes: 4 additions & 9 deletions tests/everest/functional/test_main_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
wait_for_context,
)

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"

EGG_CONFIG_PATH = relpath("..", "..", "test-data", "everest", "egg")
Expand Down Expand Up @@ -84,9 +83,8 @@ def test_everest_main_entry_bad_command():


@pytest.mark.flaky(reruns=5)
@tmpdir(CONFIG_PATH)
@pytest.mark.fails_on_macos_github_workflow
def test_everest_entry_run():
def test_everest_entry_run(copy_math_func_test_data_to_tmp):
wait_for_context()
# Setup command line arguments
with capture_streams():
Expand Down Expand Up @@ -119,8 +117,7 @@ def test_everest_entry_run():
context_stop_and_wait()


@tmpdir(CONFIG_PATH)
def test_everest_entry_monitor_no_run():
def test_everest_entry_monitor_no_run(copy_math_func_test_data_to_tmp):
with capture_streams():
start_everest(["everest", "monitor", CONFIG_FILE_MINIMAL])

Expand All @@ -132,16 +129,14 @@ def test_everest_entry_monitor_no_run():
context_stop_and_wait()


@tmpdir(CONFIG_PATH)
def test_everest_main_export_entry():
def test_everest_main_export_entry(copy_math_func_test_data_to_tmp):
# Setup command line arguments
with capture_streams():
start_everest(["everest", "export", CONFIG_FILE_MINIMAL])
assert os.path.exists(os.path.join("everest_output", "config_minimal.csv"))


@tmpdir(CONFIG_PATH)
def test_everest_main_lint_entry():
def test_everest_main_lint_entry(copy_math_func_test_data_to_tmp):
# Setup command line arguments
with capture_streams() as (out, err):
start_everest(["everest", "lint", CONFIG_FILE_MINIMAL])
Expand Down
Loading

0 comments on commit 43bac81

Please sign in to comment.