diff --git a/meggie/actions/epochs_delete_from_all/tests/test_epochs_delete_from_all.py b/meggie/actions/epochs_delete_from_all/tests/test_epochs_delete_from_all.py new file mode 100644 index 00000000..f1b677e8 --- /dev/null +++ b/meggie/actions/epochs_delete_from_all/tests/test_epochs_delete_from_all.py @@ -0,0 +1,15 @@ +from meggie.utilities.testing import BaseTestAction +from meggie.actions.epochs_delete_from_all import DeleteEpochsFromAll + + +class TestEpochsDeleteFromAll(BaseTestAction): + def test_epochs_delete_from_all(self): + + data = {"outputs": {"epochs": ["Epochs"]}} + + self.run_action( + action_name="epochs_delete_from_all", + handler=DeleteEpochsFromAll, + data=data, + patch_paths=["meggie.actions.epochs_delete_from_all"], + ) diff --git a/meggie/actions/epochs_info/tests/test_epochs_info.py b/meggie/actions/epochs_info/tests/test_epochs_info.py new file mode 100644 index 00000000..f2dede89 --- /dev/null +++ b/meggie/actions/epochs_info/tests/test_epochs_info.py @@ -0,0 +1,16 @@ +from meggie.utilities.testing import BaseTestAction +from meggie.actions.epochs_info import Info + + +class TestEpochsInfo(BaseTestAction): + def test_epochs_info(self): + + data = {"outputs": {"epochs": ["Epochs"]}} + + content = self.run_action( + action_name="epochs_info", + handler=Info, + data=data, + patch_paths=["meggie.actions.epochs_info"], + ) + assert content diff --git a/meggie/actions/epochs_plot/tests/test_epochs_plot.py b/meggie/actions/epochs_plot/tests/test_epochs_plot.py new file mode 100644 index 00000000..b8230b3c --- /dev/null +++ b/meggie/actions/epochs_plot/tests/test_epochs_plot.py @@ -0,0 +1,15 @@ +from meggie.utilities.testing import BaseTestAction +from meggie.actions.epochs_plot import PlotEpochs + + +class TestPlotEpochs(BaseTestAction): + def test_epochs_plot(self): + + data = {"outputs": {"epochs": ["Epochs"]}} + + self.run_action( + action_name="epochs_plot", + handler=PlotEpochs, + data=data, + patch_paths=["meggie.actions.epochs_plot"], + ) diff --git a/meggie/actions/epochs_plot_image/tests/test_epochs_plot_image.py b/meggie/actions/epochs_plot_image/tests/test_epochs_plot_image.py new file mode 100644 index 00000000..74c02646 --- /dev/null +++ b/meggie/actions/epochs_plot_image/tests/test_epochs_plot_image.py @@ -0,0 +1,15 @@ +from meggie.utilities.testing import BaseTestAction +from meggie.actions.epochs_plot_image import PlotEpochsImage + + +class TestPlotEpochsImage(BaseTestAction): + def test_epochs_plot_image(self): + + data = {"outputs": {"epochs": ["Epochs"]}} + + self.run_action( + action_name="epochs_plot_image", + handler=PlotEpochsImage, + data=data, + patch_paths=["meggie.actions.epochs_plot_image"], + ) diff --git a/meggie/utilities/generate_experiments.py b/meggie/utilities/generate_experiments.py index 91f02080..ec1d70a9 100644 --- a/meggie/utilities/generate_experiments.py +++ b/meggie/utilities/generate_experiments.py @@ -155,10 +155,25 @@ def create_test_experiment(experiment_folder, experiment_name, n_subjects=2): stim_channel = find_stim_channel(raw) events = find_events(raw, stim_channel) - mne_epochs = mne.Epochs(raw, events, {"1": 1, "2": 2}, -0.1, 0.2) + params = { + "tmin": -0.1, + "tmax": 0.2, + "bstart": -0.1, + "bend": 0.2, + } + category = {"1": 1, "2": 2} + + mne_epochs = mne.Epochs( + raw, + events, + category, + tmin=params["tmin"], + tmax=params["tmax"], + baseline=(params["bstart"], params["bend"]), + ) epochs_directory = subject.epochs_directory - epochs = Epochs("Epochs", epochs_directory, {}, content=mne_epochs) + epochs = Epochs("Epochs", epochs_directory, params, content=mne_epochs) epochs.save_content() subject.add(epochs, "epochs") diff --git a/meggie/utilities/testing.py b/meggie/utilities/testing.py index 229b9dab..98a8fa16 100644 --- a/meggie/utilities/testing.py +++ b/meggie/utilities/testing.py @@ -1,4 +1,5 @@ import tempfile +import logging import importlib import pytest import json @@ -40,6 +41,10 @@ def patched_exc_messagebox(parent, exc, exec_=False): raise exc +def patched_logger_exception(msg): + raise Exception(msg) + + class BaseTestAction: @pytest.fixture(autouse=True) def setup_common(self, qtbot, monkeypatch): @@ -58,6 +63,20 @@ def setup_experiment(self): def run_action(self, action_name, handler, data={}, patch_paths=[]): + # patch mne's plt_show to not show plots + utils = importlib.import_module("mne.viz.utils") + epochs = importlib.import_module("mne.viz.epochs") + + def patched_plt_show(*args, **kwargs): + utils.plt_show(show=False, fig=None, **kwargs) + + self.monkeypatch.setattr(epochs, "plt_show", patched_plt_show) + + # patch logger to raise exceptions + logger = logging.getLogger("ui_logger") + self.monkeypatch.setattr(logger, "exception", patched_logger_exception) + + # patch messageboxes to raise exceptions for patch_path in patch_paths: module = importlib.import_module(patch_path) @@ -73,13 +92,14 @@ def run_action(self, action_name, handler, data={}, patch_paths=[]): patched_messagebox, ) + # call the action handler merged_data = {"tab_id": "test_tab"} merged_data.update(data) action_spec = load_action_spec(action_name) self.action_instance = handler( self.experiment, merged_data, self.mock_main_window, action_spec ) - self.action_instance.run() + return self.action_instance.run() def find_dialog(self, dialog_class): dialog = None