Skip to content

Commit

Permalink
Refactor the last usage of @tmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Oct 7, 2024
1 parent cc737db commit 7f56c57
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 352 deletions.
3 changes: 0 additions & 3 deletions tests/everest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .utils import tmp

__all__ = ["tmp"]
28 changes: 28 additions & 0 deletions tests/everest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ def copy_math_func_test_data_to_tmp(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_mocked_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data", "mocked_test_case")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_templete_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data", "templating")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_egg_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("..", "..", "test-data", "everest", "egg")
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)
24 changes: 13 additions & 11 deletions tests/everest/entry_points/test_everexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
from everest.config import EverestConfig
from tests.everest.utils import (
create_cached_mocked_test_case,
relpath,
satisfy,
satisfy_callable,
tmpdir,
)

CONFIG_FILE_MINIMAL = "config_minimal.yml"

CONFIG_FILE_MOCKED_TEST_CASE = "mocked_multi_batch.yml"
CONFIG_PATH_MOCKED_TEST_CASE = relpath("test_data", "mocked_test_case")


pytestmark = pytest.mark.xdist_group(name="starts_everest")

Expand Down Expand Up @@ -167,9 +165,10 @@ def test_everexport_entry_empty_export(mocked_func, copy_math_func_test_data_to_


@patch("everest.bin.utils.export")
@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE)
@pytest.mark.fails_on_macos_github_workflow
def test_everexport_entry_no_usr_def_ecl_keys(mocked_func):
def test_everexport_entry_no_usr_def_ecl_keys(
mocked_func, copy_mocked_test_data_to_tmp
):
"""Test running everexport with config file containing only the
keywords label without any list of keys"""

Expand Down Expand Up @@ -200,9 +199,10 @@ def condition(config: EverestConfig):


@patch("everest.bin.utils.export")
@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE)
@pytest.mark.fails_on_macos_github_workflow
def test_everexport_entry_internalized_usr_def_ecl_keys(mocked_func, cache_dir):
def test_everexport_entry_internalized_usr_def_ecl_keys(
mocked_func, cache_dir, copy_mocked_test_data_to_tmp
):
"""Test running everexport with config file containing a key in the
list of user defined ecl keywords, that has been internalized on
a previous run"""
Expand Down Expand Up @@ -242,9 +242,10 @@ def condition(config: EverestConfig):


@patch("everest.bin.utils.export")
@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE)
@pytest.mark.fails_on_macos_github_workflow
def test_everexport_entry_non_int_usr_def_ecl_keys(mocked_func, cache_dir, caplog):
def test_everexport_entry_non_int_usr_def_ecl_keys(
mocked_func, cache_dir, caplog, copy_mocked_test_data_to_tmp
):
"""Test running everexport when config file contains non internalized
ecl keys in the user defined keywords list"""

Expand Down Expand Up @@ -290,9 +291,10 @@ def condition(config: EverestConfig):


@patch("everest.bin.utils.export")
@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE)
@pytest.mark.fails_on_macos_github_workflow
def test_everexport_entry_not_available_batches(mocked_func, cache_dir, caplog):
def test_everexport_entry_not_available_batches(
mocked_func, cache_dir, caplog, copy_mocked_test_data_to_tmp
):
"""Test running everexport when config file contains non existing
batch numbers in the list of user defined batches"""

Expand Down
7 changes: 1 addition & 6 deletions tests/everest/functional/test_main_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from seba_sqlite.snapshot import SebaSnapshot
from tests.everest.utils import (
capture_streams,
relpath,
skipif_no_everest_models,
tmpdir,
)

from everest import __version__ as everest_version
Expand All @@ -22,8 +20,6 @@
)

CONFIG_FILE_MINIMAL = "config_minimal.yml"

EGG_CONFIG_PATH = relpath("..", "..", "test-data", "everest", "egg")
WELL_ORDER = "everest/model/config.yml"

pytestmark = pytest.mark.xdist_group(name="starts_everest")
Expand Down Expand Up @@ -167,11 +163,10 @@ def test_everest_main_lint_entry(copy_math_func_test_data_to_tmp):
assert validation_msg in err.getvalue()


@tmpdir(EGG_CONFIG_PATH)
@pytest.mark.fails_on_macos_github_workflow
@skipif_no_everest_models
@pytest.mark.everest_models_test
def test_everest_main_configdump_entry():
def test_everest_main_configdump_entry(copy_egg_test_data_to_tmp):
# Setup command line arguments
with capture_streams() as (out, _):
start_everest(["everest", "render", WELL_ORDER])
Expand Down
18 changes: 6 additions & 12 deletions tests/everest/test_config_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from everest import ConfigKeys as CK
from everest import config_file_loader as loader
from everest.config import EverestConfig
from tests.everest.utils import relpath, tmpdir
from tests.everest.utils import relpath

mocked_root = relpath(os.path.join("test_data", "mocked_test_case"))

Expand Down Expand Up @@ -57,11 +57,9 @@ def test_load_config_as_yaml():
assert rendered_template is not None


@tmpdir(mocked_root)
def test_configpath_in_defs():
def test_configpath_in_defs(copy_mocked_test_data_to_tmp):
config_file = "mocked_multi_batch.yml"
config = EverestConfig.load_file(config_file)

defs = {
"numeric_key": 1,
"bool_key": True,
Expand All @@ -73,8 +71,7 @@ def test_configpath_in_defs():
assert defs == config.definitions


@tmpdir(mocked_root)
def test_dependent_definitions():
def test_dependent_definitions(copy_mocked_test_data_to_tmp):
config_file = "mocked_multi_batch.yml"
with open(config_file, encoding="utf-8") as f:
raw_config = YAML(typ="safe", pure=True).load(f)
Expand Down Expand Up @@ -103,8 +100,7 @@ def test_dependent_definitions():
assert defs == config.definitions


@tmpdir(mocked_root)
def test_dependent_definitions_value_error():
def test_dependent_definitions_value_error(copy_mocked_test_data_to_tmp):
config_file = "mocked_multi_batch.yml"
with open(config_file, encoding="utf-8") as f:
raw_config = YAML(typ="safe", pure=True).load(f)
Expand All @@ -121,16 +117,14 @@ def test_dependent_definitions_value_error():
EverestConfig.load_file(config_file)


@tmpdir(mocked_root)
def test_load_empty_configuration():
def test_load_empty_configuration(copy_mocked_test_data_to_tmp):
with open("empty_config.yml", mode="w", encoding="utf-8") as fh:
fh.writelines("")
with pytest.raises(ValidationError, match="missing"):
EverestConfig.load_file("empty_config.yml")


@tmpdir(mocked_root)
def test_load_invalid_configuration():
def test_load_invalid_configuration(copy_mocked_test_data_to_tmp):
with open("invalid_config.yml", mode="w", encoding="utf-8") as fh:
fh.writelines("asdf")
with pytest.raises(ValidationError, match="missing"):
Expand Down
46 changes: 22 additions & 24 deletions tests/everest/test_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from everest.config.input_constraint_config import InputConstraintConfig
from everest.config.well_config import WellConfig
from tests.everest.utils import relpath, tmp, tmpdir
from tests.everest.utils import relpath

cfg_dir = relpath("test_data", "mocked_test_case")
mocked_config = relpath(cfg_dir, "mocked_test_case.yml")
Expand Down Expand Up @@ -114,8 +114,7 @@ def _perturb_control_zero(
return exp_var_def


@tmpdir(relpath("test_data"))
def test_variable_name_index_validation():
def test_variable_name_index_validation(copy_test_data_to_tmp):
config = EverestConfig.load_file(
os.path.join("mocked_test_case", "mocked_test_case.yml")
)
Expand Down Expand Up @@ -174,7 +173,7 @@ def test_variable_name_index_validation():


@pytest.mark.integration_test
def test_individual_control_variable_config():
def test_individual_control_variable_config(copy_test_data_to_tmp):
config_file = os.path.join("mocked_test_case", "config_input_constraints.yml")

global_min = (0, 0.7, 1.3, None)
Expand All @@ -184,26 +183,25 @@ def test_individual_control_variable_config():
test_base = (global_min, global_max, global_init, fill_missing)

for gmin, gmax, ginit, fill in itertools.product(*test_base):
with tmp(relpath("test_data")):
config = EverestConfig.load_file(config_file)
exp_var_def = _perturb_control_zero(config, gmin, gmax, ginit, fill)

# Not complete configuration
if None in [gmin, gmax, ginit] and not fill:
with pytest.raises(expected_exception=ValidationError):
EverestConfig.model_validate(config.to_dict())
continue

# Invalid parameters
def valid_control(var: ControlVariableConfig) -> bool:
return var.min <= var.initial_guess <= var.max

if not all(map(valid_control, exp_var_def)):
with pytest.raises(expected_exception=ValidationError):
EverestConfig.model_validate(config.to_dict())
continue

EverestConfig.model_validate(config.to_dict())
config = EverestConfig.load_file(config_file)
exp_var_def = _perturb_control_zero(config, gmin, gmax, ginit, fill)

# Not complete configuration
if None in [gmin, gmax, ginit] and not fill:
with pytest.raises(expected_exception=ValidationError):
EverestConfig.model_validate(config.to_dict())
continue

# Invalid parameters
def valid_control(var: ControlVariableConfig) -> bool:
return var.min <= var.initial_guess <= var.max

if not all(map(valid_control, exp_var_def)):
with pytest.raises(expected_exception=ValidationError):
EverestConfig.model_validate(config.to_dict())
continue

EverestConfig.model_validate(config.to_dict())


def test_control_variable_name():
Expand Down
7 changes: 4 additions & 3 deletions tests/everest/test_detached.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
SIMULATION_DIR,
)
from everest.util import makedirs_if_needed
from tests.everest.utils import relpath, tmpdir


class MockContext:
Expand Down Expand Up @@ -155,8 +154,10 @@ def test_server_status(copy_math_func_test_data_to_tmp):


@patch("everest.detached.server_is_running", return_value=False)
@tmpdir(relpath("test_data", "detached"))
def test_wait_for_server(server_is_running_mock, caplog):
def test_wait_for_server(
server_is_running_mock, caplog, copy_test_data_to_tmp, monkeypatch
):
monkeypatch.chdir("detached")
config = EverestConfig.load_file("valid_yaml_config.yml")

with caplog.at_level(logging.DEBUG), pytest.raises(Exception):
Expand Down
Loading

0 comments on commit 7f56c57

Please sign in to comment.