Merge branch 'non-linux-test-fixes' into 'master'

Fix tests for non-linux

See merge request openlp/openlp!717
This commit is contained in:
Tim Bentley 2024-02-15 21:48:23 +00:00
commit f2410aede7
6 changed files with 46 additions and 14 deletions

View File

@ -51,6 +51,9 @@ test_script:
cd $env:APPVEYOR_BUILD_FOLDER
# Run the tests
python -m pytest tests
if ($? -eq $False) {
throw "The tests failed!"
}
# Go back to the user root folder
cd ..
}

View File

@ -32,6 +32,7 @@ from unittest.mock import MagicMock, PropertyMock, call, patch
from openlp.core.common import Singleton, add_actions, clean_filename, clean_button_text, de_hump, delete_file, \
extension_loader, get_file_encoding, get_filesystem_encoding, get_uno_command, get_uno_instance, md5_hash, \
normalize_str, path_to_module, qmd5_hash, sha256_file_hash, trace_error_handler, verify_ip_address
from openlp.core.common.platform import is_win
from tests.resources.projector.data import TEST_HASH, TEST_PIN, TEST_SALT
from tests.utils.constants import TEST_RESOURCES_PATH
@ -90,18 +91,24 @@ def test_extension_loader_files_found():
assert "/app/dir/community" not in sys.path, "Community path has been added to the application sys.path"
if is_win():
p_prefix = 'C:\\'
else:
p_prefix = '/'
def test_extension_loader_files_found_community():
"""
Test the `extension_loader` function when it successfully finds and loads some files
"""
# GIVEN: A mocked `Path.glob` method which returns a list of files
with patch('openlp.core.common.applocation.AppLocation.get_directory',
return_value=Path('/', 'app', 'dir')), \
return_value=Path(p_prefix, 'app', 'dir')), \
patch.object(Path, 'glob', return_value=[
Path('/', 'app', 'dir', 'contrib', 'import_dir', 'file1.py'),
Path('/', 'app', 'dir', 'contrib', 'import_dir', 'file2.py'),
Path('/', 'app', 'dir', 'contrib', 'import_dir', 'file3.py'),
Path('/', 'app', 'dir', 'contrib', 'import_dir', 'file4.py')]), \
Path(p_prefix, 'app', 'dir', 'contrib', 'import_dir', 'file1.py'),
Path(p_prefix, 'app', 'dir', 'contrib', 'import_dir', 'file2.py'),
Path(p_prefix, 'app', 'dir', 'contrib', 'import_dir', 'file3.py'),
Path(p_prefix, 'app', 'dir', 'contrib', 'import_dir', 'file4.py')]), \
patch('openlp.core.common.import_openlp_module') as mocked_import_module:
# WHEN: Calling `extension_loader` with a list of files to exclude
@ -111,7 +118,8 @@ def test_extension_loader_files_found_community():
# files listed in the `excluded_files` argument
mocked_import_module.assert_has_calls([call('contrib.import_dir.file1'),
call('contrib.import_dir.file4')])
assert "/app/dir" in sys.path, "app/dir path has not been added to the application sys.path"
expected_path = p_prefix + 'app' + os.path.sep + 'dir'
assert expected_path in sys.path, expected_path + ' path has not been added to the application sys.path'
def test_extension_loader_import_error():
@ -120,7 +128,7 @@ def test_extension_loader_import_error():
"""
# GIVEN: A mocked `import_module` which raises an `ImportError`
with patch('openlp.core.common.applocation.AppLocation.get_directory',
return_value=Path('/', 'app', 'dir', 'openlp')), \
return_value=Path(p_prefix, 'app', 'dir', 'openlp')), \
patch.object(Path, 'glob', return_value=[
Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file1.py')]), \
patch('openlp.core.common.import_openlp_module', side_effect=ImportError()), \

View File

@ -58,11 +58,14 @@ def mock_geometry():
def display_window_env():
box_layout_patcher = patch('openlp.core.display.window.QtWidgets.QVBoxLayout')
web_view_patcher = patch('openlp.core.display.webengine.WebEngineView')
web_view_schemes_patcher = patch('openlp.core.display.webengine.WebViewSchemes')
box_layout_patcher.start()
web_view_patcher.start()
web_view_schemes_patcher.start()
yield
box_layout_patcher.stop()
web_view_patcher.stop()
web_view_schemes_patcher.stop()
def test_x11_override_on(display_window_env, mock_settings):

View File

@ -44,7 +44,7 @@ def app_main_env():
patch('openlp.core.app.LanguageManager'), \
patch('openlp.core.app.qInitResources'), \
patch('openlp.core.app.parse_options'), \
patch('openlp.core.app.QtWidgets.QApplication'), \
patch('openlp.core.app.QtWidgets.QApplication') as mock_qapp, \
patch('openlp.core.app.QtWidgets.QMessageBox.warning') as mock_warn, \
patch('openlp.core.app.QtWidgets.QMessageBox.information'), \
patch('openlp.core.app.OpenLP') as mock_openlp, \
@ -58,6 +58,7 @@ def app_main_env():
openlp_server.is_another_instance_running.return_value = False
mock_apploc.get_data_path.return_value = Path()
mock_apploc.get_directory.return_value = Path()
mock_qapp.devicePixelRatio.return_value = 1.0
mock_warn.return_value = True
openlp_instance = MagicMock()
mock_openlp.return_value = openlp_instance
@ -364,10 +365,16 @@ def test_main_future_settings(mock_move: MagicMock, mock_get_path: MagicMock, mo
mock_warn.assert_called_once()
if is_win():
p_prefix = 'C:'
else:
p_prefix = ''
@pytest.mark.parametrize('portable_path, settings_path',
[('settings', str(Path('/openlp/settings/Data/OpenLP.ini'))),
(None, str(Path('/Data/OpenLP.ini'))),
('/openlp/settings/', str(Path('/openlp/settings/Data/OpenLP.ini')))])
[('settings', str(Path(p_prefix + '/openlp/settings/Data/OpenLP.ini'))),
(None, str(Path(p_prefix + '/Data/OpenLP.ini'))),
(p_prefix + '/openlp/settings/', str(Path(p_prefix + '/openlp/settings/Data/OpenLP.ini')))])
@patch('openlp.core.app.Settings')
@patch('openlp.core.app.AppLocation')
def test_setup_portable_settings(MockAppLocation: MagicMock, MockSettings: MagicMock, portable_path: str,

View File

@ -29,6 +29,7 @@ from typing import Any
from PyQt5 import QtCore, QtGui, QtTest, QtWidgets
from openlp.core.common.platform import is_win
from openlp.core.common.registry import Registry
from openlp.core.widgets.dialogs import FileDialog
from openlp.core.widgets.edits import PathEdit, HistoryComboBox, SearchEdit
@ -87,9 +88,15 @@ def test_path_getter(path_edit: PathEdit):
assert path_edit.path == Path('getter', 'test', 'pat.h')
if is_win():
p_prefix = 'C:/'
else:
p_prefix = ''
@pytest.mark.parametrize('prop, expected', [
(Path('setter', 'test', 'pat.h'), ('setter', 'test', 'pat.h')),
('setter/str/test/pat.h', ('setter', 'str', 'test', 'pat.h')),
(Path(p_prefix, 'setter', 'test', 'pat.h'), (p_prefix, 'setter', 'test', 'pat.h')),
(p_prefix + 'setter/str/test/pat.h', (p_prefix, 'setter', 'str', 'test', 'pat.h')),
(None, None)
])
def test_path_setter(prop: Any, expected: Any, path_edit: PathEdit):

View File

@ -59,7 +59,11 @@ def db_url():
dst_path = tmp_path / f'openlp-{secrets.token_urlsafe()}.sqlite'
shutil.copyfile(src_path, dst_path)
yield 'sqlite:///' + str(dst_path)
dst_path.unlink()
try:
dst_path.unlink()
except PermissionError:
# on windows sometimes we try to delete this while still in use...?
pass
def test_upgrade_2_basic(mock_message_box, db_url, mock_settings):