Add test coverage for __main__.py and remove some unused files

This commit is contained in:
Raoul Snyman 2023-08-12 22:53:30 -07:00
parent eaa97f433d
commit 586b54c7b8
4 changed files with 90 additions and 129 deletions

View File

@ -76,4 +76,4 @@ def start():
if __name__ == '__main__':
start()
start() # pragma: nocover

View File

@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2023 OpenLP Developers #
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
from PyQt5 import QtWidgets
from openlp.core.common.i18n import translate
from openlp.core.lib.ui import create_button_box
class Ui_AddGroupDialog(object):
def setup_ui(self, add_group_dialog):
add_group_dialog.setObjectName('add_group_dialog')
add_group_dialog.resize(300, 10)
self.dialog_layout = QtWidgets.QVBoxLayout(add_group_dialog)
self.dialog_layout.setObjectName('dialog_layout')
self.name_layout = QtWidgets.QFormLayout()
self.name_layout.setObjectName('name_layout')
self.parent_group_label = QtWidgets.QLabel(add_group_dialog)
self.parent_group_label.setObjectName('parent_group_label')
self.parent_group_combobox = QtWidgets.QComboBox(add_group_dialog)
self.parent_group_combobox.setObjectName('parent_group_combobox')
self.name_layout.addRow(self.parent_group_label, self.parent_group_combobox)
self.name_label = QtWidgets.QLabel(add_group_dialog)
self.name_label.setObjectName('name_label')
self.name_edit = QtWidgets.QLineEdit(add_group_dialog)
self.name_edit.setObjectName('name_edit')
self.name_label.setBuddy(self.name_edit)
self.name_layout.addRow(self.name_label, self.name_edit)
self.dialog_layout.addLayout(self.name_layout)
self.button_box = create_button_box(add_group_dialog, 'button_box', ['cancel', 'save'])
self.dialog_layout.addWidget(self.button_box)
self.retranslate_ui(add_group_dialog)
add_group_dialog.setMaximumHeight(add_group_dialog.sizeHint().height())
def retranslate_ui(self, add_group_dialog):
add_group_dialog.setWindowTitle(translate('ImagePlugin.AddGroupForm', 'Add group'))
self.parent_group_label.setText(translate('ImagePlugin.AddGroupForm', 'Parent group:'))
self.name_label.setText(translate('ImagePlugin.AddGroupForm', 'Group name:'))

View File

@ -1,72 +0,0 @@
# -*- coding: utf-8 -*-
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2023 OpenLP Developers #
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
from PyQt5 import QtCore, QtWidgets
from openlp.core.common.i18n import translate
from openlp.core.lib.ui import critical_error_message_box
from openlp.plugins.images.forms.addgroupdialog import Ui_AddGroupDialog
class AddGroupForm(QtWidgets.QDialog, Ui_AddGroupDialog):
"""
This class implements the 'Add group' form for the Images plugin.
"""
def __init__(self, parent=None):
"""
Constructor
"""
super(AddGroupForm, self).__init__(parent, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowCloseButtonHint)
self.setup_ui(self)
def exec(self, clear=True, show_top_level_group=False, selected_group=None):
"""
Show the form.
:param clear: Set to False if the text input box should not be cleared when showing the dialog (default: True).
:param show_top_level_group: Set to True when "-- Top level group --" should be showed as first item
(default: False).
:param selected_group: The ID of the group that should be selected by default when showing the dialog.
"""
if clear:
self.name_edit.clear()
self.name_edit.setFocus()
if show_top_level_group and not self.parent_group_combobox.top_level_group_added:
self.parent_group_combobox.insertItem(0, translate('ImagePlugin.MediaItem', '-- Top-level group --'), 0)
self.parent_group_combobox.top_level_group_added = True
if selected_group is not None:
for i in range(self.parent_group_combobox.count()):
if self.parent_group_combobox.itemData(i) == selected_group:
self.parent_group_combobox.setCurrentIndex(i)
return QtWidgets.QDialog.exec(self)
def accept(self):
"""
Override the accept() method from QDialog to make sure something is entered in the text input box.
"""
if not self.name_edit.text():
critical_error_message_box(message=translate('ImagePlugin.AddGroupForm',
'You need to type in a group name.'))
self.name_edit.setFocus()
return False
else:
return QtWidgets.QDialog.accept(self)

View File

@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2023 OpenLP Developers #
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
"""
Test OpenLP's ``__main__`` module
"""
from unittest.mock import MagicMock, patch
from openlp.__main__ import start, set_up_fault_handling, tear_down_fault_handling
@patch('openlp.__main__.error_log_file')
def test_tear_down_fault_handling(mocked_error_log_file: MagicMock):
"""Test that the teardown function closes the file"""
# GIVEN: A mocked error log file
# WHEN: tear_down_fault_handling() is called
tear_down_fault_handling()
# THEN: The log file should have been closed
mocked_error_log_file.close.assert_called_once()
@patch('openlp.__main__.AppLocation.get_directory')
@patch('openlp.__main__.create_paths')
@patch('openlp.__main__.atexit.register')
@patch('openlp.__main__.faulthandler.enable')
def test_set_up_fault_handling(mocked_enable: MagicMock, mocked_register: MagicMock, mocked_create_paths: MagicMock,
mocked_get_directory: MagicMock):
"""Test that the set_up_fault_handling() function does the correct things"""
# GIVEN: A whole bunch o' mocks
# WHEN: set_up_fault_handling() is called
set_up_fault_handling()
# THEN: The correct calls should have been made
mocked_create_paths.assert_called_once()
mocked_register.assert_called_once_with(tear_down_fault_handling)
mocked_enable.assert_called_once()
@patch('openlp.__main__.create_paths')
@patch('openlp.__main__.atexit.register')
@patch('openlp.__main__.atexit.unregister')
def test_set_up_fault_handling_exception(mocked_unregister: MagicMock, mocked_register: MagicMock,
mocked_create_paths: MagicMock):
"""Test that the set_up_fault_handling() function tears itself down correctly if there is an issue"""
# GIVEN: A whole bunch o' mocks
mocked_register.side_effect = OSError('Test')
# WHEN: set_up_fault_handling() is called
set_up_fault_handling()
# THEN: The correct calls should have been made
mocked_create_paths.assert_called_once()
mocked_unregister.assert_called_once_with(tear_down_fault_handling)
@patch('openlp.__main__.multiprocessing.freeze_support')
@patch('openlp.__main__.set_up_fault_handling')
@patch('openlp.__main__.is_win')
@patch('openlp.__main__.main')
def test_start(mocked_main: MagicMock, mocked_is_win: MagicMock, mocked_fault_handling: MagicMock,
mocked_freeze_support: MagicMock):
"""Test that the start method calls the correct functions"""
# GIVEN: A bunch of mocks
mocked_is_win.return_value = True
# WHEN: start() is called
start()
# THEN: The right methods are called
mocked_fault_handling.assert_called_once()
mocked_freeze_support.assert_called_once()
mocked_main.assert_called_once()