Skip to content

Commit

Permalink
tests: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jun 16, 2021
1 parent d18375d commit 670fed1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 35 deletions.
56 changes: 28 additions & 28 deletions pyjibe/head/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ def __init__(self, *args, **kwargs):
self.activateWindow()
self.setWindowState(QtCore.Qt.WindowState.WindowActive)

def load_data(self, files, retry_open=None, separate_analysis=False):
"""Load AFM data"""
# expand directories
data_files = []
for ff in files:
data_files += afmformats.find_data(ff)

if not data_files:
ret = QtWidgets.QMessageBox.warning(
self,
"No AFM data found!",
"No AFM data files could be found in the location specified.",
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Retry
)
if retry_open is not None and ret == QtWidgets.QMessageBox.Retry:
retry_open()
else:
# Make sure there are no duplicate files (#12)
data_files = sorted(set(data_files))
if separate_analysis:
# open each file in one analysis
usable = [[ss] for ss in data_files]
else:
usable = [data_files]
for flist in usable:
aclass = registry.fd.UiForceDistance
self.add_subwindow(aclass, flist)

def add_subwindow(self, aclass, flist):
"""Add a subwindow, register data set and add to menu"""
sub = PyJibeQMdiSubWindow()
Expand Down Expand Up @@ -204,34 +232,6 @@ def on_tool_convert(self):
dlg = ConvertDialog(self)
dlg.show()

def load_data(self, files, retry_open=None, separate_analysis=False):
"""Load AFM data"""
# expand directories
data_files = []
for ff in files:
data_files += afmformats.find_data(ff)

if not data_files:
ret = QtWidgets.QMessageBox.warning(
self,
"No AFM data found!",
"No AFM data files could be found in the location specified.",
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Retry
)
if retry_open is not None and ret == QtWidgets.QMessageBox.Retry:
retry_open()
else:
# Make sure there are no duplicate files (#12)
data_files = sorted(set(data_files))
if separate_analysis:
# open each file in one analysis
usable = [[ss] for ss in data_files]
else:
usable = [data_files]
for flist in usable:
aclass = registry.fd.UiForceDistance
self.add_subwindow(aclass, flist)


def excepthook(etype, value, trace):
"""
Expand Down
67 changes: 60 additions & 7 deletions tests/test_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,49 @@ def test_init_print_version(qtbot):
assert mock_stdout.getvalue().strip() == pyjibe.__version__


def test_rem_subwindow(qtbot):
def mock_get_open_filenames(*args, **kwargs):
tdir = tempfile.mkdtemp(prefix="pyjibe_test_open_bulk_")
t2 = pathlib.Path(tdir) / "spot1.jpk-force"
shutil.copy2(data_path / "spot3-0192.jpk-force", t2)
return [t2], None

mw = pyjibe.head.PyJibe()

with mock.patch("PyQt5.QtWidgets.QFileDialog.getOpenFileNames",
mock_get_open_filenames):
mw.on_open_single()

sub = mw.subwindows[0]
title = sub.windowTitle()
action_texts = [action.text() for action in mw.menuExport.actions()]
assert title in action_texts
sub.close()
action_texts_2 = [action.text() for action in mw.menuExport.actions()]
assert title not in action_texts_2
assert len(mw.subwindows) == 0
mw.close()


@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python>=3.8")
def test_on_about(qtbot):
mock_about = mock.Mock()
with mock.patch("PyQt5.QtWidgets.QMessageBox.about", mock_about):
with mock.patch("PyQt5.QtWidgets.QMessageBox.about") as mock_about:
mw = pyjibe.head.PyJibe()
mw.on_about()
mw.close()

assert mock_about.call_args.args[1] == f"PyJibe {pyjibe.__version__}"
assert "PyJibe" in mock_about.call_args.args[2]
assert mock_about.call_args.args[1] == f"PyJibe {pyjibe.__version__}"
assert "PyJibe" in mock_about.call_args.args[2]


@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python>=3.8")
def test_on_documentation(qtbot):
mock_wbopen = mock.Mock()
with mock.patch("webbrowser.open", mock_wbopen):
with mock.patch("webbrowser.open") as mock_wbopen:
mw = pyjibe.head.PyJibe()
mw.on_documentation()
mw.close()

assert mock_wbopen.call_args.args[0] == "https://pyjibe.readthedocs.io"
assert mock_wbopen.call_args.args[0] == "https://pyjibe.readthedocs.io"


def test_on_open_bulk(qtbot):
Expand Down Expand Up @@ -79,6 +101,24 @@ def mock_selected_files(self):
mw.close()


def test_on_open_bulk_no_data(qtbot):
def mock_selected_files(self):
return [tempfile.mkdtemp(prefix="pyjibe_no_data_")]

mw = pyjibe.head.PyJibe()

with mock.patch("pyjibe.head.custom_widgets."
"DirectoryDialogMultiSelect.selectedFiles",
mock_selected_files):
with mock.patch("pyjibe.head.custom_widgets."
"DirectoryDialogMultiSelect.exec_"):
with mock.patch("PyQt5.QtWidgets.QMessageBox.warning") as warn:
mw.on_open_bulk()
assert warn.call_args.args[1] == "No AFM data found!"

mw.close()


def test_on_open_multiple(qtbot):
def mock_selected_files(self):
tdir = tempfile.mkdtemp(prefix="pyjibe_test_open_bulk_")
Expand Down Expand Up @@ -125,3 +165,16 @@ def mock_get_open_filenames(*args, **kwargs):
war = mw.subwindows[0].widget()
assert war.list_curves.topLevelItemCount() == 1
mw.close()


@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python>=3.8")
def test_on_software(qtbot):
mw = pyjibe.head.PyJibe()

with mock.patch("PyQt5.QtWidgets.QMessageBox.information") as info:
mw.on_software()
assert info.call_args.args[1] == "Software"
assert info.call_args.args[2].count(f"PyJibe {pyjibe.__version__}")
assert info.call_args.args[2].count("lmfit")

mw.close()

0 comments on commit 670fed1

Please sign in to comment.