diff --git a/meggie/actions/epochs_create/tests/test_epochs_create.py b/meggie/actions/epochs_create/tests/test_epochs_create.py index 1f303bcc..eccafca1 100644 --- a/meggie/actions/epochs_create/tests/test_epochs_create.py +++ b/meggie/actions/epochs_create/tests/test_epochs_create.py @@ -8,13 +8,14 @@ class TestEpochsCreate(BaseTestAction): - def test_create_epochs_dialog(self): + def test_epochs_create(self): self.run_action( - tab_id="epochs", action_name="epochs_create", handler=CreateEpochs, - dialog_path="meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain", + patch_paths=[ + "meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain" + ], ) dialog = self.find_dialog(CreateEpochsFromEventsDialog) diff --git a/meggie/actions/epochs_delete/tests/test_epochs_delete.py b/meggie/actions/epochs_delete/tests/test_epochs_delete.py new file mode 100644 index 00000000..61c6bede --- /dev/null +++ b/meggie/actions/epochs_delete/tests/test_epochs_delete.py @@ -0,0 +1,15 @@ +from meggie.utilities.testing import BaseTestAction +from meggie.actions.epochs_delete import DeleteEpochs + + +class TestEpochsDelete(BaseTestAction): + def test_epochs_delete(self): + + data = {"outputs": {"epochs": ["Epochs"]}} + + self.run_action( + action_name="epochs_delete", + handler=DeleteEpochs, + data=data, + patch_paths=["meggie.actions.epochs_delete"], + ) diff --git a/meggie/utilities/generate_experiments.py b/meggie/utilities/generate_experiments.py index 49f2449e..91f02080 100644 --- a/meggie/utilities/generate_experiments.py +++ b/meggie/utilities/generate_experiments.py @@ -12,6 +12,9 @@ from meggie.mainwindow.preferences import PreferencesHandler from meggie.experiment import initialize_new_experiment +from meggie.utilities.events import find_events +from meggie.utilities.events import find_stim_channel +from meggie.datatypes.epochs.epochs import Epochs def create_experiment( @@ -139,9 +142,35 @@ def create_evoked_conditions_experiment( ) +def create_test_experiment(experiment_folder, experiment_name, n_subjects=2): + """Generate experiment with data for testing.""" + + experiment = create_evoked_conditions_experiment( + experiment_folder, experiment_name, n_subjects=n_subjects + ) + + # create trivial epochs + for subject in experiment.subjects.values(): + raw = subject.get_raw() + 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) + + epochs_directory = subject.epochs_directory + epochs = Epochs("Epochs", epochs_directory, {}, content=mne_epochs) + epochs.save_content() + subject.add(epochs, "epochs") + + return experiment + + # allow creating experiment from the command line if __name__ == "__main__": type_, experiment_folder, experiment_name = sys.argv[1:] + if type_ == "test_experiment": + create_test_experiment(experiment_folder, experiment_name) + if type_ == "evoked_conditions": create_evoked_conditions_experiment(experiment_folder, experiment_name) diff --git a/meggie/utilities/testing.py b/meggie/utilities/testing.py index 6711b414..229b9dab 100644 --- a/meggie/utilities/testing.py +++ b/meggie/utilities/testing.py @@ -1,4 +1,5 @@ import tempfile +import importlib import pytest import json import os @@ -6,7 +7,7 @@ from PyQt5.QtWidgets import QApplication, QMainWindow -from meggie.utilities.generate_experiments import create_evoked_conditions_experiment +from meggie.utilities.generate_experiments import create_test_experiment os.environ["QT_QPA_PLATFORM"] = "offscreen" @@ -52,25 +53,31 @@ def setup_common(self, qtbot, monkeypatch): yield def setup_experiment(self): - self.experiment = create_evoked_conditions_experiment( - self.dirpath, "test_experiment", n_subjects=1 - ) + self.experiment = create_test_experiment(self.dirpath, "test_experiment") self.experiment.activate_subject("sample_01-raw") - def run_action(self, tab_id, action_name, handler, dialog_path): + def run_action(self, action_name, handler, data={}, patch_paths=[]): - self.monkeypatch.setattr( - ".".join([dialog_path, "exc_messagebox"]), - patched_exc_messagebox, - ) - self.monkeypatch.setattr( - ".".join([dialog_path, "messagebox"]), - patched_messagebox, - ) - data = {"tab_id": tab_id} + for patch_path in patch_paths: + module = importlib.import_module(patch_path) + + if getattr(module, "exc_messagebox", None): + self.monkeypatch.setattr( + ".".join([patch_path, "exc_messagebox"]), + patched_exc_messagebox, + ) + + if getattr(module, "messagebox", None): + self.monkeypatch.setattr( + ".".join([patch_path, "messagebox"]), + patched_messagebox, + ) + + merged_data = {"tab_id": "test_tab"} + merged_data.update(data) action_spec = load_action_spec(action_name) self.action_instance = handler( - self.experiment, data, self.mock_main_window, action_spec + self.experiment, merged_data, self.mock_main_window, action_spec ) self.action_instance.run()