Skip to content

Commit

Permalink
matplotlib version issue with pyside6 resolved, configuration tests a…
Browse files Browse the repository at this point in the history
…dded, tests updated
  • Loading branch information
mpatrou committed Nov 1, 2024
1 parent 80e1cc9 commit b64b79c
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 19 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- qtpy
- numpy
- scipy
- matplotlib
- matplotlib <3.9 #resolves pyside 6 error
- pre-commit
# package building:
- versioningit
Expand Down
7 changes: 7 additions & 0 deletions src/hyspecplanningtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
from ._version import __version__ # noqa: F401
except ImportError:
__version__ = "unknown"

# def HyspecPlanningTool(): # pylint: disable=invalid-name
# """This is needed for backward compatibility because mantid workbench does "from shiver import Shiver" """
# from .hyspecplanningtools import HyspecPlanningTool as hyspecplanningtools
# # pylint: disable=import-outside-toplevel

# return hyspecplanningtools()
1 change: 1 addition & 0 deletions src/hyspecplanningtools/configuration_template.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[global.other]
#url to documentation
help_url = https://github.com/neutrons/HyspecPlanningTools/blob/next/README.md
3 changes: 3 additions & 0 deletions src/hyspecplanningtools/hyspecplanningtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class HyspecPlanningTool(QMainWindow):

__instance = None

def get_instance():
return HyspecPlanningTool.__instance

def __new__(cls):
if HyspecPlanningTool.__instance is None:
HyspecPlanningTool.__instance = QMainWindow.__new__(cls) # pylint: disable=no-value-for-parameter
Expand Down
17 changes: 11 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

import pytest

from hyspecplanningtools.hyspecplanningtools import HyspecPlanningTool
# from hyspecplanningtools.hyspecplanningtools import HyspecPlanningTool


@pytest.fixture(scope="session")
def hyspec_app():
app = HyspecPlanningTool()
yield app
app.destroy()
# @pytest.fixture(scope="session")
# def hyspec_app():
# if HyspecPlanningTool.get_instance() is None:
# app = HyspecPlanningTool()
# print("fsfsdf")
# else:
# app = HyspecPlanningTool.get_instance()
# print("fsfsdf")

# return app


@pytest.fixture(scope="session")
Expand Down
174 changes: 174 additions & 0 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
"""Tests for Configuration mechanism"""

import os
from configparser import ConfigParser
from pathlib import Path

import pytest

from hyspecplanningtools.configuration import Configuration, get_data


def test_config_path_default():
"""Test configuration default file path"""
config = Configuration()
assert config.config_file_path.endswith(".hyspecplanningtools/configuration.ini") is True
# check the valid state
assert config.is_valid()
assert config.valid == config.is_valid()


def test_config_path_in_folder(monkeypatch, tmp_path):
"""Test configuration configuration user-defined file path that does not exist in a new directory"""

user_path = os.path.join(tmp_path, "temp2", "test_config.ini")
assert not os.path.exists(user_path)

monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_path)

config = Configuration()
# check if the file exists now
assert os.path.exists(user_path)
assert config.is_valid()


def test_config_path_does_not_exist(monkeypatch, tmp_path):
"""Test configuration user-defined file path that does not exist"""
user_path = os.path.join(tmp_path, "test_config.ini")
assert not os.path.exists(user_path)

monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_path)

config = Configuration()
# check if the file is exists now
assert os.path.exists(user_path)
assert config.is_valid()


@pytest.mark.parametrize(
"user_conf_file",
[
"""
[global.other]
#url to documentation
help_url = https://github.com/neutrons/HyspecPlanningTools/blob/next/README.md
"""
],
indirect=True,
)
def test_field_validate_fields_exist(monkeypatch, user_conf_file):
"""Test configuration validate all fields exist with the same values as templates
Note: update the parameters if the fields increase"""
# read the custom configuration file
monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
user_config = Configuration()

assert user_config.config_file_path.endswith(user_conf_file) is True
# check if the file exists
assert os.path.exists(user_conf_file)

# check all fields are the same as the configuration template file
project_directory = Path(__file__).resolve().parent.parent
template_file_path = os.path.join(project_directory, "src", "hyspecplanningtools", "configuration_template.ini")
template_config = ConfigParser(allow_no_value=True, comment_prefixes="/")
template_config.read(template_file_path)
# comments should be copied too
for section in user_config.config.sections():
for field in user_config.config[section]:
assert user_config.config[section][field] == template_config[section][field]


@pytest.mark.parametrize(
"user_conf_file",
[
"""
[generate_tab.oncat]
oncat_url = test_url
client_id = 0000-0000
use_notes = True
"""
],
indirect=True,
)
def test_field_validate_fields_same(monkeypatch, user_conf_file):
"""Test configuration validate all fields exist with their values; different from the template"""

# read the custom configuration file
monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
user_config = Configuration()

# check if the file exists
assert os.path.exists(user_conf_file)
assert user_config.config_file_path == user_conf_file

# check all field values have the same values as the user configuration file
assert get_data("generate_tab.oncat", "oncat_url") == "test_url"
assert get_data("generate_tab.oncat", "client_id") == "0000-0000"
# cast to bool
assert get_data("generate_tab.oncat", "use_notes") is True


@pytest.mark.parametrize(
"user_conf_file",
[
"""
"""
],
indirect=True,
)
def test_field_validate_fields_missing(monkeypatch, user_conf_file):
"""Test configuration validate missing fields added from the template"""

# read the custom configuration file
monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
user_config = Configuration()

# check if the file exists
assert os.path.exists(user_conf_file)
assert user_config.config_file_path == user_conf_file

# check all field values have the same values as the user configuration file
assert get_data("global.other", "help_url") == "https://github.com/neutrons/HyspecPlanningTools/blob/next/README.md"


@pytest.mark.parametrize("user_conf_file", ["""[global.other]"""], indirect=True)
def test_get_data_valid(monkeypatch, user_conf_file):
"""Test configuration get_data - valid"""

monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
config = Configuration()
assert config.config_file_path.endswith(user_conf_file) is True
# get the data
# section
assert len(get_data("global.other", "")) == 1
# fields
assert get_data("global.other", "help_url") == "https://github.com/neutrons/HyspecPlanningTools/blob/next/README.md"

assert config.is_valid()


@pytest.mark.parametrize(
"user_conf_file",
[
"""
[generate_tab.oncat]
oncat_url = test_url
client_id = 0000-0000
use_notes = 1
"""
],
indirect=True,
)
def test_get_data_invalid(monkeypatch, user_conf_file):
"""Test configuration get_data - invalid"""
# read the custom configuration file
monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
config = Configuration()
assert config.config_file_path.endswith(user_conf_file) is True

# section
assert get_data("section_not_here", "") is None

assert len(get_data("generate_tab.oncat", "")) == 3
# field
assert get_data("generate_tab.oncat", "field_not_here") is None
28 changes: 16 additions & 12 deletions tests/test_mainwindow.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
"""UI tests for the application"""

import subprocess
from unittest.mock import Mock

import pytest

from hyspecplanningtools.hyspecplanningtools import HyspecPlanningTool, __version__
from hyspecplanningtools.mainwindow import MainWindow


def test_appwindow(qtbot):
"""Test that the application starts successfully"""
hyspecplanningtools = HyspecPlanningTool()
hyspecplanningtools.show()
qtbot.waitUntil(hyspecplanningtools.show, timeout=5000)
assert hyspecplanningtools.isVisible()
assert hyspecplanningtools.windowTitle() == f"HyspecPlanning Tools - {__version__}"
hyspecplanningtools.destroy()
hyspec_app = HyspecPlanningTool()
hyspec = hyspec_app
hyspec.show()
qtbot.waitUntil(hyspec.show, timeout=5000)
assert hyspec.isVisible()
assert hyspec.windowTitle() == f"HyspecPlanning Tools - {__version__}"
hyspec_app = hyspec = None


def test_gui_version():
Expand Down Expand Up @@ -48,11 +47,16 @@ def test_gui_v():
)
def test_mainwindow_help(monkeypatch, user_conf_file):
"""Test the help function in the main window"""
hyspec_app = HyspecPlanningTool()

help_url = ""

def fake_webbrowser(url):
nonlocal help_url
help_url = url

fake_webbrowser = Mock()
monkeypatch.setattr("hyspecplanningtools.configuration.CONFIG_PATH_FILE", user_conf_file)
monkeypatch.setattr("webbrowser.open", fake_webbrowser)

main_window = MainWindow()
main_window.handle_help()
fake_webbrowser.assert_called_once()
hyspec_app.main_window.handle_help()
assert help_url == "https://test.url.com"

0 comments on commit b64b79c

Please sign in to comment.