Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to get full coverage #17

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ markers = [
"datarepo: Tests that require the test-data submodule"
]

[tool.coverage.run]
omit = ["src/garnet/_version.py",
"src/garnet/__init__.py",
"src/garnet/version.py",]

[tool.ruff]
line-length = 119

Expand Down
40 changes: 40 additions & 0 deletions tests/tabs/home/model/test_home_presenter.py
Copy link
Collaborator

@mpatrou mpatrou Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this can be part of the test /presenter folder?

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Unit test for the garnet.tabs.home.model.home_presenter.py HomePresenter."""

import unittest
from typing import Any
from unittest.mock import MagicMock

# Import the HomePresenter class to be tested
from garnet.home.model import HomeModel
from garnet.home.presenter import HomePresenter
from garnet.home.view import HomeView


class TestHomePresenter(unittest.TestCase):
"""Test the HomePresenter class."""

def test_init(self: Any) -> None:
"""Test the HomePresenter constructor."""
mock_view = MagicMock(spec=HomeView)
mock_model = MagicMock(spec=HomeModel)

presenter = HomePresenter(mock_view, mock_model)

assert presenter._view is mock_view
assert presenter._model is mock_model

def test_view_property(self: Any) -> None:
"""Test the view property."""
mock_view = MagicMock(spec=HomeView)
mock_model = MagicMock(spec=HomeModel)

presenter = HomePresenter(mock_view, mock_model)
assert presenter.view is mock_view

def test_model_property(self: Any) -> None:
"""Test the model property."""
mock_view = MagicMock(spec=HomeView)
mock_model = MagicMock(spec=HomeModel)

presenter = HomePresenter(mock_view, mock_model)
assert presenter.model is mock_model
18 changes: 18 additions & 0 deletions tests/tabs/home/views/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def test_plan_name(qtbot: pytest.fixture):
qtbot.keyClicks(window.plan_name, "Plan Name")
assert window.save_button.isEnabled() is True

for _ in range(10):
qtbot.keyClick(window.plan_name, QtCore.Qt.Key_Backspace)
assert window.save_button.isEnabled() is False


def test_wavelength(qtbot: pytest.fixture):
"""Test that the wavelength LineEdit is required and has default value"""
Expand All @@ -33,3 +37,17 @@ def test_wavelength(qtbot: pytest.fixture):
for _ in range(10):
qtbot.keyClick(window.wavelength, QtCore.Qt.Key_Backspace)
assert window.save_button.isEnabled() is False


def test_save_reduction_plan(qtbot: pytest.fixture):
"""Test that the save button is enabled when both fields are filled"""
window = HomeView()
qtbot.addWidget(window)
window.show()

assert window.save_button.isEnabled() is False
qtbot.keyClicks(window.plan_name, "Plan Name")
assert window.save_button.isEnabled() is True

window.save_button.click()
assert window.save_button.isEnabled() is True
33 changes: 33 additions & 0 deletions tests/utility/test_gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test the garnet.garnet.gui function

This is the entry point for the GUI application.
"""

import sys
from unittest import mock
from unittest.mock import patch

import pytest
from garnet.garnet import gui


def test_gui_version():
"""Test the version flag."""
t_argv = sys.argv.copy()
sys.argv.append("--version")
with pytest.raises(SystemExit) as excinfo:
gui()
assert excinfo.value.code is None
sys.argv = t_argv


@patch("garnet.garnet.QApplication")
@patch("garnet.garnet.Garnet")
def test_gui(mock_garnet: mock, mock_qtapp: mock):
"""Test the GUI entry point."""
with pytest.raises(SystemExit) as excinfo:
gui()

assert excinfo.type == SystemExit
assert mock_garnet.called
assert mock_qtapp.called
14 changes: 14 additions & 0 deletions tests/utility/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Tests for the Logger class in garnet/logger.py."""

from garnet.logger import Logger
from mantid.kernel import Logger as mantid_logger # noqa: N813


def test_get_logger():
"""Test getting the logger."""
logger_name = "custom_logger"
logger = Logger(logger_name)
logger.get(logger_name)

# Assert that the returned logger instance is correct
assert isinstance(logger, mantid_logger)
3 changes: 2 additions & 1 deletion tests/utility/ui_elements/test_base_tablewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def test_with_args():
"""Test setting the read only style."""
num_rows = 12
num_columns = 3
base_table_widget = BaseTableWidget(num_rows, num_columns, required=True)
# QTableWidget(12, 3, parent=None)
base_table_widget = BaseTableWidget(num_rows, num_columns)

assert base_table_widget.rowCount() == num_rows
assert base_table_widget.columnCount() == num_columns
Loading