Skip to content

Commit

Permalink
update widget tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niksirbi committed Jul 30, 2024
1 parent f4fe16d commit ae2b480
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions movement/napari/_loader_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from napari.settings import get_settings
from napari.utils.notifications import show_warning
from napari.viewer import Viewer
from qtpy.QtWidgets import (
QComboBox,
Expand Down Expand Up @@ -60,7 +61,7 @@ def _create_file_path_widget(self):
# File path line edit and browse button
self.file_path_edit = QLineEdit()
self.browse_button = QPushButton("browse")
self.browse_button.clicked.connect(self._on_browse_button_clicked)
self.browse_button.clicked.connect(self._on_browse_clicked)
# Layout for line edit and button
self.file_path_layout = QHBoxLayout()
self.file_path_layout.addWidget(self.file_path_edit)
Expand All @@ -70,10 +71,10 @@ def _create_file_path_widget(self):
def _create_load_button(self):
"""Create a button to load the file and add layers to the viewer."""
self.load_button = QPushButton("Load")
self.load_button.clicked.connect(lambda: self._on_load_button_click())
self.load_button.clicked.connect(lambda: self._on_load_clicked())
self.layout().addRow(self.load_button)

def _on_browse_button_clicked(self):
def _on_browse_clicked(self):
"""Open a file dialog to select a file."""
file_suffix_map = {

Check warning on line 79 in movement/napari/_loader_widget.py

View check run for this annotation

Codecov / codecov/patch

movement/napari/_loader_widget.py#L79

Added line #L79 was not covered by tests
"DeepLabCut": "Files containing predicted poses (*.h5 *.csv)",
Expand All @@ -91,13 +92,13 @@ def _on_browse_button_clicked(self):
# Set the file path in the line edit
self.file_path_edit.setText(file_paths[0])

Check warning on line 93 in movement/napari/_loader_widget.py

View check run for this annotation

Codecov / codecov/patch

movement/napari/_loader_widget.py#L93

Added line #L93 was not covered by tests

def _on_load_button_click(self):
def _on_load_clicked(self):
"""Load the file and add as a Points layer to the viewer."""
fps = self.fps_spinbox.value()
source_software = self.source_software_combo.currentText()
file_path = self.file_path_edit.text()
if file_path == "":
logger.warning("No file path specified.")
show_warning("No file path specified.")
return
ds = load_poses.from_file(file_path, source_software, fps)

Check warning on line 103 in movement/napari/_loader_widget.py

View check run for this annotation

Codecov / codecov/patch

movement/napari/_loader_widget.py#L103

Added line #L103 was not covered by tests

Expand Down
20 changes: 10 additions & 10 deletions tests/test_integration/test_napari_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ def test_meta_widget(meta_widget):
def test_loader_widget(loader_widget):
"""Test that the loader widget is properly instantiated."""
assert loader_widget is not None
assert loader_widget.layout().rowCount() == 1
assert loader_widget.layout().rowCount() == 4


def test_hello_button_calls_on_hello_clicked(make_napari_viewer_proxy, mocker):
"""Test that clicking the hello button calls _on_hello_clicked.
def test_load_button_calls_on_load_clicked(make_napari_viewer_proxy, mocker):
"""Test that clicking the 'Load' call the right function.
Here we have to create a new Loader widget after mocking the method.
We cannot reuse the existing widget fixture because then it would be too
late to mock (the widget has already "decided" which method to call).
"""
mock_method = mocker.patch(
"movement.napari._loader_widget.Loader._on_hello_clicked"
"movement.napari._loader_widget.Loader._on_load_clicked"
)
loader = Loader(make_napari_viewer_proxy)
hello_button = loader.findChildren(QPushButton)[0]
hello_button.click()
load_button = loader.findChildren(QPushButton)[-1]
load_button.click()
mock_method.assert_called_once()


def test_on_hello_clicked_outputs_message(loader_widget, capsys):
"""Test that _on_hello_clicked outputs the expected message."""
loader_widget._on_hello_clicked()
def test_on_load_clicked_without_file_path(loader_widget, capsys):
"""Test that clicking 'Load' without a file path shows a warning."""
loader_widget._on_load_clicked()
captured = capsys.readouterr()
assert "INFO: Hello, world!" in captured.out
assert "No file path specified." in captured.out

0 comments on commit ae2b480

Please sign in to comment.