Skip to content

Commit

Permalink
Merge pull request #36 from cibr-jyu/test-actions
Browse files Browse the repository at this point in the history
Test actions
  • Loading branch information
teekuningas authored Feb 23, 2024
2 parents b1dda82 + f04d7af commit 0f533ae
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ jobs:
pip install -r requirements.txt
pip install -r dev-requirements.txt
- name: Install meggie for tests
run: |
pip install .
- name: Check styles
run: |
make check
- name: Run tests
run: |
nosetests
make test
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ check:
black --check -t py39 meggie
pylama meggie

.PHONY: test
test:
pytest -s

.PHONY: update_docs
update_docs:
rm -fr docs_updated
Expand Down
3 changes: 2 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
black
pylama
nose
pytest
pytest-qt
mkdocs
32 changes: 32 additions & 0 deletions meggie/actions/epochs_create/tests/test_epochs_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from PyQt5 import QtWidgets

from meggie.utilities.testing import BaseTestAction
from meggie.actions.epochs_create import CreateEpochs
from meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain import (
CreateEpochsFromEventsDialog,
)


class TestEpochsCreate(BaseTestAction):
def test_create_epochs_dialog(self):
def patched_exc_messagebox(parent, exc, exec_=False):
raise exc

self.monkeypatch.setattr(
"meggie.actions.epochs_create.dialogs.createEpochsFromEventsDialogMain.exc_messagebox",
patched_exc_messagebox,
)

self.run_action(
tab_id="epochs", action_name="epochs_create", handler=CreateEpochs
)
dialog = self.find_dialog(CreateEpochsFromEventsDialog)

event = {"event_id": 1, "mask": 0}
item = QtWidgets.QListWidgetItem(
"%s, %s" % ("ID " + str(event["event_id"]), "mask=" + str(event["mask"]))
)
dialog.ui.listWidgetEvents.addItem(item)
dialog.events = [event]

dialog.accept()
1 change: 0 additions & 1 deletion meggie/actions/tfr_plot_tse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


class PlotTSE(Action):

def run(self):
"""Plots a TSE from TFR object."""
try:
Expand Down
67 changes: 67 additions & 0 deletions meggie/utilities/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import tempfile
import pytest
import json
import os
import pkg_resources

from PyQt5.QtWidgets import QApplication, QMainWindow

from meggie.utilities.generate_experiments import create_evoked_conditions_experiment

os.environ["QT_QPA_PLATFORM"] = "offscreen"


class MockMainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
self.ui = None
QMainWindow.__init__(self)

def update_ui(self):
pass

def initialize_ui(self):
pass


def load_action_spec(action_name):
action_path = pkg_resources.resource_filename("meggie", "actions")
config_path = os.path.join(action_path, action_name, "configuration.json")
with open(config_path, "r") as f:
action_spec = json.load(f)
return action_spec


class BaseTestAction:
@pytest.fixture(autouse=True)
def setup_common(self, qtbot, monkeypatch):
os.environ["QT_QPA_PLATFORM"] = "offscreen"
self.qtbot = qtbot
self.monkeypatch = monkeypatch
self.mock_main_window = MockMainWindow()
with tempfile.TemporaryDirectory() as tmpdirname:
self.dirpath = tmpdirname
self.setup_experiment()
yield

def setup_experiment(self):
self.experiment = create_evoked_conditions_experiment(
self.dirpath, "test_experiment", n_subjects=1
)
self.experiment.activate_subject("sample_01-raw")

def run_action(self, tab_id, action_name, handler):
data = {"tab_id": tab_id}
action_spec = load_action_spec(action_name)
self.action_instance = handler(
self.experiment, data, self.mock_main_window, action_spec
)
self.action_instance.run()

def find_dialog(self, dialog_class):
dialog = None
for widget in QApplication.topLevelWidgets():
if isinstance(widget, dialog_class):
dialog = widget
break
assert dialog is not None
return dialog

0 comments on commit 0f533ae

Please sign in to comment.