Skip to content

Commit

Permalink
Fix everest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Sep 11, 2024
1 parent f0912d4 commit de74293
Show file tree
Hide file tree
Showing 76 changed files with 322 additions and 228 deletions.
24 changes: 23 additions & 1 deletion tests/everest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import shutil
import sys
from copy import deepcopy
from pathlib import Path
from typing import Callable, Dict, Iterator, Optional, Union

import pytest

from everest.config.control_config import ControlConfig


Expand Down Expand Up @@ -82,3 +82,25 @@ def control_config(
config = deepcopy(control_data_no_variables)
config["variables"] = request.param
return ControlConfig.model_validate(config)


@pytest.fixture(scope="session", autouse=True)
def hide_window(request):
if request.config.getoption("--show-gui"):
yield
return

old_value = os.environ.get("QT_QPA_PLATFORM")
if sys.platform == "darwin":
os.environ["QT_QPA_PLATFORM"] = "offscreen"
else:
os.environ["QT_QPA_PLATFORM"] = "minimal"
yield
if old_value is None:
del os.environ["QT_QPA_PLATFORM"]
else:
os.environ["QT_QPA_PLATFORM"] = old_value


def pytest_addoption(parser):
parser.addoption("--show-gui", action="store_true", default=False)
4 changes: 2 additions & 2 deletions tests/everest/entry_points/test_config_branch_entry.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from os.path import exists
from unittest.mock import PropertyMock, patch

from seba_sqlite.snapshot import SebaSnapshot

from everest.bin.config_branch_script import config_branch_entry
from everest.config import EverestConfig
from everest.config_file_loader import load_yaml
from everest.config_keys import ConfigKeys as CK
from seba_sqlite.snapshot import SebaSnapshot

from tests.everest.utils import relpath, tmpdir

CONFIG_PATH = relpath("..", "..", "examples", "math_func")
Expand Down
17 changes: 7 additions & 10 deletions tests/everest/entry_points/test_everest_entry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import logging.config
import os
from functools import partial
from unittest.mock import PropertyMock, patch

import pytest

from everest.bin.everest_script import everest_entry
from everest.bin.kill_script import kill_entry
from everest.bin.monitor_script import monitor_entry
Expand All @@ -18,7 +16,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_logger, capture_streams, relpath, tmpdir

CONFIG_PATH = relpath("..", "..", "examples", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"
Expand Down Expand Up @@ -85,10 +84,9 @@ def test_everest_entry_debug(
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
caplog,
):
"""Test running everest with --debug"""
with caplog.at_level(logging.DEBUG):
with capture_logger() as logstream:
everest_entry([CONFIG_FILE_MINIMAL, "--debug"])

start_server_mock.assert_called_once()
Expand All @@ -97,11 +95,10 @@ def test_everest_entry_debug(
everserver_status_mock.assert_called()

# the config file itself is dumped at DEBUG level
logstream = "\n".join(caplog.messages)
assert '"controls"' in logstream
assert '"objective_functions"' in logstream
assert '"name": "distance"' in logstream
assert f'"config_path": "{os.getcwd()}/config_minimal.yml"' in logstream
assert '"controls"' in logstream.getvalue()
assert '"objective_functions"' in logstream.getvalue()
assert '"name": "distance"' in logstream.getvalue()
assert f'"config_path": "{os.getcwd()}/config_minimal.yml"' in logstream.getvalue()


@patch("everest.bin.everest_script.run_detached_monitor")
Expand Down
2 changes: 1 addition & 1 deletion tests/everest/entry_points/test_everexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import pandas as pd
import pytest

from everest import ConfigKeys as CK
from everest import MetaDataColumnNames as MDCN
from everest.bin.everexport_script import everexport_entry
from everest.bin.utils import ProgressBar
from everest.config import EverestConfig

from tests.everest.utils import (
capture_logger,
create_cached_mocked_test_case,
Expand Down
2 changes: 1 addition & 1 deletion tests/everest/entry_points/test_visualization_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from unittest.mock import PropertyMock, patch

import pytest

from everest.bin.visualization_script import visualization_entry
from everest.config import EverestConfig
from everest.detached import ServerStatus

from tests.everest.utils import capture_streams, relpath

CONFIG_PATH = relpath("..", "..", "examples", "math_func", "config_advanced.yml")
Expand Down
23 changes: 11 additions & 12 deletions tests/everest/functional/test_main_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
from textwrap import dedent

import pytest
from ruamel.yaml import YAML
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
from everest.bin.main import start_everest
from everest.config import EverestConfig
Expand All @@ -20,6 +11,14 @@
everserver_status,
wait_for_context,
)
from ruamel.yaml import YAML
from seba_sqlite.snapshot import SebaSnapshot
from tests.everest.utils import (
capture_streams,
relpath,
skipif_no_everest_models,
tmpdir,
)

CONFIG_PATH = relpath("..", "..", "examples", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"
Expand Down Expand Up @@ -100,9 +99,9 @@ def test_everest_entry_run():
snapshot = SebaSnapshot(config.optimization_output_dir).get_snapshot()

best_settings = snapshot.optimization_data[-1]
assert best_settings.controls["point_0_x"] == pytest.approx(0.5, abs=0.05)
assert best_settings.controls["point_0_y"] == pytest.approx(0.5, abs=0.05)
assert best_settings.controls["point_0_z"] == pytest.approx(0.5, abs=0.05)
assert best_settings.controls["point_x"] == pytest.approx(0.5, abs=0.05)
assert best_settings.controls["point_y"] == pytest.approx(0.5, abs=0.05)
assert best_settings.controls["point_z"] == pytest.approx(0.5, abs=0.05)

assert best_settings.objective_value == pytest.approx(0.0, abs=0.0005)

Expand Down
17 changes: 8 additions & 9 deletions tests/everest/functional/test_ui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import pytest
from PyQt5.QtWidgets import QAction, QPushButton, QWidget
from qtpy.QtCore import Qt
from seba_sqlite.snapshot import SebaSnapshot
from tests.everest.dialogs_mocker import mock_dialogs_all
from tests.everest.utils import relpath, tmpdir

from everest.config import EverestConfig
from everest.detached import (
ServerStatus,
Expand All @@ -13,6 +7,11 @@
wait_for_context,
)
from ieverest import IEverest
from PyQt5.QtWidgets import QAction, QPushButton, QWidget
from qtpy.QtCore import Qt
from seba_sqlite.snapshot import SebaSnapshot
from tests.everest.dialogs_mocker import mock_dialogs_all
from tests.everest.utils import relpath, tmpdir

CONFIG_PATH = relpath("..", "..", "examples", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"
Expand Down Expand Up @@ -58,9 +57,9 @@ def test_ui_optimization(qapp, qtbot, mocker):
snapshot = SebaSnapshot(config.optimization_output_dir).get_snapshot()

best_settings = snapshot.optimization_data[-1]
assert abs(best_settings.controls["point_0_x"] - 0.5) <= 0.05
assert abs(best_settings.controls["point_0_y"] - 0.5) <= 0.05
assert abs(best_settings.controls["point_0_z"] - 0.5) <= 0.05
assert abs(best_settings.controls["point_x"] - 0.5) <= 0.05
assert abs(best_settings.controls["point_y"] - 0.5) <= 0.05
assert abs(best_settings.controls["point_z"] - 0.5) <= 0.05

assert abs(best_settings.objective_value) <= 0.0005

Expand Down
Loading

0 comments on commit de74293

Please sign in to comment.