From a17039ba308f97c9c1ac64a706bf288eab40047b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 28 Sep 2020 21:42:45 -0700 Subject: [PATCH] Fix a bunch of tests and warnings Some of the song import tests were not being run, and pytest was warning about them. I refactored them so that pytest would run them again. Also, there were some other warnings in the code about deprecations, so I fixed as many of those as I could too. --- openlp/core/lib/__init__.py | 4 +- openlp/core/widgets/layouts.py | 2 +- .../songs/test_opensongimport.py | 39 ++++------ .../songs/test_propresenterimport.py | 45 ++++++------ .../songs/test_singingthefaithimport.py | 32 ++++----- .../songs/test_songbeamerimport.py | 45 ++++++------ .../openlp_plugins/songs/test_songselect.py | 22 +++--- .../songs/test_wordsofworshipimport.py | 72 ++++++++++--------- tests/helpers/songfileimport.py | 9 +++ 9 files changed, 128 insertions(+), 142 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 737d18556..0b629da49 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -375,10 +375,10 @@ def resize_image(image_path, width, height, background='#000000', ignore_aspect_ size = QtCore.QSize(width, height) elif image_ratio < resize_ratio: # Use the image's height as reference for the new size. - size = QtCore.QSize(image_ratio * height, height) + size = QtCore.QSize(int(image_ratio * height), height) else: # Use the image's width as reference for the new size. - size = QtCore.QSize(width, 1 / (image_ratio / width)) + size = QtCore.QSize(width, int(1 / (image_ratio / width))) reader.setScaledSize(size) preview = reader.read() if image_ratio == resize_ratio: diff --git a/openlp/core/widgets/layouts.py b/openlp/core/widgets/layouts.py index 313c78f7e..7c7f85b12 100644 --- a/openlp/core/widgets/layouts.py +++ b/openlp/core/widgets/layouts.py @@ -166,7 +166,7 @@ class AspectRatioLayout(QtWidgets.QLayout): x = rect.width() - self.margin - width else: x = self.margin + (available_width - width) / 2 - widget.setGeometry(rect.x() + x, rect.y() + self.margin, width, height) + widget.setGeometry(rect.x() + int(x), rect.y() + self.margin, width, height) self.resize.emit(QtCore.QSize(width, height)) def sizeHint(self): diff --git a/tests/functional/openlp_plugins/songs/test_opensongimport.py b/tests/functional/openlp_plugins/songs/test_opensongimport.py index 9ce0ca1d0..fb40752e5 100644 --- a/tests/functional/openlp_plugins/songs/test_opensongimport.py +++ b/tests/functional/openlp_plugins/songs/test_opensongimport.py @@ -33,30 +33,21 @@ from tests.utils.constants import RESOURCE_PATH TEST_PATH = RESOURCE_PATH / 'songs' / 'opensong' -class TestOpenSongFileImport(SongImportTestHelper): - - def __init__(self, *args, **kwargs): - self.importer_class_name = 'OpenSongImport' - self.importer_module_name = 'opensong' - super(TestOpenSongFileImport, self).__init__(*args, **kwargs) - - def test_song_import(self): - """ - Test that loading an OpenSong file works correctly on various files - """ - # Mock out the settings - always return False - self.settings.value.side_effect = lambda value: True if value == 'songs/enable chords' else False - # Do the test import - self.file_import([TEST_PATH / 'Amazing Grace'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - self.file_import([TEST_PATH / 'Beautiful Garden Of Prayer'], - self.load_external_result_data(TEST_PATH / 'Beautiful Garden Of Prayer.json')) - self.file_import([TEST_PATH / 'One, Two, Three, Four, Five'], - self.load_external_result_data(TEST_PATH / 'One, Two, Three, Four, Five.json')) - self.file_import([TEST_PATH / 'Amazing Grace2'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - self.file_import([TEST_PATH / 'Amazing Grace with bad CCLI'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace without CCLI.json')) +def test_opensong_file_import(settings): + """ + Test that loading an OpenSong file works correctly on various files + """ + with SongImportTestHelper('OpenSongImport', 'opensong') as helper: + helper.file_import([TEST_PATH / 'Amazing Grace'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) + helper.file_import([TEST_PATH / 'Beautiful Garden Of Prayer'], + helper.load_external_result_data(TEST_PATH / 'Beautiful Garden Of Prayer.json')) + helper.file_import([TEST_PATH / 'One, Two, Three, Four, Five'], + helper.load_external_result_data(TEST_PATH / 'One, Two, Three, Four, Five.json')) + helper.file_import([TEST_PATH / 'Amazing Grace2'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) + helper.file_import([TEST_PATH / 'Amazing Grace with bad CCLI'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace without CCLI.json')) class TestOpenSongImport(TestCase): diff --git a/tests/functional/openlp_plugins/songs/test_propresenterimport.py b/tests/functional/openlp_plugins/songs/test_propresenterimport.py index dcf897a1f..a40123935 100644 --- a/tests/functional/openlp_plugins/songs/test_propresenterimport.py +++ b/tests/functional/openlp_plugins/songs/test_propresenterimport.py @@ -25,34 +25,31 @@ ProPresenter song files into the current installation database. from tests.helpers.songfileimport import SongImportTestHelper from tests.utils.constants import RESOURCE_PATH - TEST_PATH = RESOURCE_PATH / 'songs' / 'propresenter' -class TestProPresenterFileImport(SongImportTestHelper): +def test_pro4_song_import(settings): + """ + Test that loading a ProPresenter 4 file works correctly + """ + with SongImportTestHelper('ProPresenterImport', 'propresenter') as helper: + helper.file_import([TEST_PATH / 'Amazing Grace.pro4'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - def __init__(self, *args, **kwargs): - self.importer_class_name = 'ProPresenterImport' - self.importer_module_name = 'propresenter' - super(TestProPresenterFileImport, self).__init__(*args, **kwargs) - def test_pro4_song_import(self): - """ - Test that loading a ProPresenter 4 file works correctly - """ - self.file_import([TEST_PATH / 'Amazing Grace.pro4'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) +def test_pro5_song_import(settings): + """ + Test that loading a ProPresenter 5 file works correctly + """ + with SongImportTestHelper('ProPresenterImport', 'propresenter') as helper: + helper.file_import([TEST_PATH / 'Amazing Grace.pro5'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - def test_pro5_song_import(self): - """ - Test that loading a ProPresenter 5 file works correctly - """ - self.file_import([TEST_PATH / 'Amazing Grace.pro5'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - def test_pro6_song_import(self): - """ - Test that loading a ProPresenter 6 file works correctly - """ - self.file_import([TEST_PATH / 'Amazing Grace.pro6'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) +def test_pro6_song_import(settings): + """ + Test that loading a ProPresenter 6 file works correctly + """ + with SongImportTestHelper('ProPresenterImport', 'propresenter') as helper: + helper.file_import([TEST_PATH / 'Amazing Grace.pro6'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) diff --git a/tests/functional/openlp_plugins/songs/test_singingthefaithimport.py b/tests/functional/openlp_plugins/songs/test_singingthefaithimport.py index adcef2ff0..7ef4924a4 100644 --- a/tests/functional/openlp_plugins/songs/test_singingthefaithimport.py +++ b/tests/functional/openlp_plugins/songs/test_singingthefaithimport.py @@ -28,26 +28,20 @@ from tests.utils.constants import RESOURCE_PATH TEST_PATH = RESOURCE_PATH / 'songs' / 'singingthefaith' -class TestSingingTheFaithFileImport(SongImportTestHelper): - - def __init__(self, *args, **kwargs): - self.importer_class_name = 'SingingTheFaithImport' - self.importer_module_name = 'singingthefaith' - super(TestSingingTheFaithFileImport, self).__init__(*args, **kwargs) - - def test_song_import(self): - """ - Test that loading a Singing The Faith file works correctly on various files - """ +def test_singingthefaith_file_import(registry): + """ + Test that loading a Singing The Faith file works correctly on various files + """ + with SongImportTestHelper('SingingTheFaithImport', 'singingthefaith') as helper: # Note that the previous tests without hints no longer apply as there is always a # hints file, which contains the hints for the real Singing The Faith Songs. # Unhinted songs here must be numbered above any real Singing The Faith Song # Single verse - self.file_import([TEST_PATH / 'H901.txt'], - self.load_external_result_data(TEST_PATH / 'STF901.json')) + helper.file_import([TEST_PATH / 'H901.txt'], + helper.load_external_result_data(TEST_PATH / 'STF901.json')) # Whole song - self.file_import([TEST_PATH / 'H902.txt'], - self.load_external_result_data(TEST_PATH / 'STF902.json')) + helper.file_import([TEST_PATH / 'H902.txt'], + helper.load_external_result_data(TEST_PATH / 'STF902.json')) # Tests with hints - note that the hints directory has a hints.tag which specifies # SongbookNumberInTitle: True @@ -55,8 +49,8 @@ class TestSingingTheFaithFileImport(SongImportTestHelper): # song tests will need it # Single verse - self.file_import([TEST_PATH / 'hints' / 'H1.txt'], - self.load_external_result_data(TEST_PATH / 'hints' / 'STF001.json')) + helper.file_import([TEST_PATH / 'hints' / 'H1.txt'], + helper.load_external_result_data(TEST_PATH / 'hints' / 'STF001.json')) # Whole song - self.file_import([TEST_PATH / 'hints' / 'H2.txt'], - self.load_external_result_data(TEST_PATH / 'hints' / 'STF002.json')) + helper.file_import([TEST_PATH / 'hints' / 'H2.txt'], + helper.load_external_result_data(TEST_PATH / 'hints' / 'STF002.json')) diff --git a/tests/functional/openlp_plugins/songs/test_songbeamerimport.py b/tests/functional/openlp_plugins/songs/test_songbeamerimport.py index 1b3e9902a..44cc11d21 100644 --- a/tests/functional/openlp_plugins/songs/test_songbeamerimport.py +++ b/tests/functional/openlp_plugins/songs/test_songbeamerimport.py @@ -25,6 +25,7 @@ from unittest import TestCase from unittest.mock import MagicMock, patch from openlp.core.common.registry import Registry +from openlp.core.common.settings import Settings from openlp.plugins.songs.lib.importers.songbeamer import SongBeamerImport, SongBeamerTypes from tests.helpers.songfileimport import SongImportTestHelper from tests.utils.constants import RESOURCE_PATH @@ -33,32 +34,28 @@ from tests.utils.constants import RESOURCE_PATH TEST_PATH = RESOURCE_PATH / 'songs' / 'songbeamer' -class TestSongBeamerFileImport(SongImportTestHelper): - - def __init__(self, *args, **kwargs): - self.importer_class_name = 'SongBeamerImport' - self.importer_module_name = 'songbeamer' - super(TestSongBeamerFileImport, self).__init__(*args, **kwargs) - - def test_song_import(self): - """ - Test that loading an SongBeamer file works correctly on various files - """ +def test_songbeamer_file_import(settings): + """ + Test that loading an SongBeamer file works correctly on various files + """ + with SongImportTestHelper('SongBeamerImport', 'songbeamer') as helper: # Mock out the settings - always return False - self.settings.value.side_effect = lambda value: True if value == 'songs/enable chords' else False - self.file_import([TEST_PATH / 'Amazing Grace.sng'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) - self.file_import([TEST_PATH / 'Lobsinget dem Herrn.sng'], - self.load_external_result_data(TEST_PATH / 'Lobsinget dem Herrn.json')) - self.file_import([TEST_PATH / 'When I Call On You.sng'], - self.load_external_result_data(TEST_PATH / 'When I Call On You.json')) + Settings().setValue('songs/enable chords', True) + helper.file_import([TEST_PATH / 'Amazing Grace.sng'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace.json')) + helper.file_import([TEST_PATH / 'Lobsinget dem Herrn.sng'], + helper.load_external_result_data(TEST_PATH / 'Lobsinget dem Herrn.json')) + helper.file_import([TEST_PATH / 'When I Call On You.sng'], + helper.load_external_result_data(TEST_PATH / 'When I Call On You.json')) - def test_cp1252_encoded_file(self): - """ - Test that a CP1252 encoded file get's decoded properly. - """ - self.file_import([TEST_PATH / 'cp1252song.sng'], - self.load_external_result_data(TEST_PATH / 'cp1252song.json')) + +def test_songbeamer_cp1252_encoded_file(settings): + """ + Test that a CP1252 encoded file get's decoded properly. + """ + with SongImportTestHelper('SongBeamerImport', 'songbeamer') as helper: + helper.file_import([TEST_PATH / 'cp1252song.sng'], + helper.load_external_result_data(TEST_PATH / 'cp1252song.json')) class TestSongBeamerImport(TestCase): diff --git a/tests/functional/openlp_plugins/songs/test_songselect.py b/tests/functional/openlp_plugins/songs/test_songselect.py index 0d8e26cc6..105a04123 100644 --- a/tests/functional/openlp_plugins/songs/test_songselect.py +++ b/tests/functional/openlp_plugins/songs/test_songselect.py @@ -922,16 +922,12 @@ class TestSongSelectForm(TestCase, TestMixin): mocked_view_song.assert_called_with() -class TestSongSelectFileImport(SongImportTestHelper): - - def __init__(self, *args, **kwargs): - self.importer_class_name = 'CCLIFileImport' - self.importer_module_name = 'cclifile' - super().__init__(*args, **kwargs) - - def test_song_import(self): - """ - Test that loading an OpenSong file works correctly on various files - """ - self.file_import([TEST_PATH / 'TestSong.bin'], self.load_external_result_data(TEST_PATH / 'TestSong-bin.json')) - self.file_import([TEST_PATH / 'TestSong.txt'], self.load_external_result_data(TEST_PATH / 'TestSong-txt.json')) +def test_songselect_file_import(): + """ + Test that loading a SongSelect file works correctly on various files + """ + with SongImportTestHelper('CCLIFileImport', 'cclifile') as helper: + helper.file_import([TEST_PATH / 'TestSong.bin'], + helper.load_external_result_data(TEST_PATH / 'TestSong-bin.json')) + helper.file_import([TEST_PATH / 'TestSong.txt'], + helper.load_external_result_data(TEST_PATH / 'TestSong-txt.json')) diff --git a/tests/functional/openlp_plugins/songs/test_wordsofworshipimport.py b/tests/functional/openlp_plugins/songs/test_wordsofworshipimport.py index cf78d5ade..a0ee0717c 100644 --- a/tests/functional/openlp_plugins/songs/test_wordsofworshipimport.py +++ b/tests/functional/openlp_plugins/songs/test_wordsofworshipimport.py @@ -28,44 +28,46 @@ from tests.utils.constants import RESOURCE_PATH TEST_PATH = RESOURCE_PATH / 'songs' / 'wordsofworship' -class TestWordsOfWorshipFileImport(SongImportTestHelper): +def test_amazing_grace_song_import(registry): + """ + Test that loading a Words of Worship file works correctly + """ + with SongImportTestHelper('WordsOfWorshipImport', 'wordsofworship') as helper: + helper.file_import([TEST_PATH / 'Amazing Grace (6 Verses)_v2_1_2.wow-song'], + helper.load_external_result_data(TEST_PATH / 'Amazing Grace (6 Verses)_v2_1_2.json')) - def __init__(self, *args, **kwargs): - self.importer_class_name = 'WordsOfWorshipImport' - self.importer_module_name = 'wordsofworship' - super().__init__(*args, **kwargs) - def test_amazing_grace_song_import(self): - """ - Test that loading a Words of Worship file works correctly - """ - self.file_import([TEST_PATH / 'Amazing Grace (6 Verses)_v2_1_2.wow-song'], - self.load_external_result_data(TEST_PATH / 'Amazing Grace (6 Verses)_v2_1_2.json')) +def test_when_morning_gilds_song_import(registry): + """ + Test that loading a Words of Worship file v2.0.0 works correctly + """ + with SongImportTestHelper('WordsOfWorshipImport', 'wordsofworship') as helper: + helper.file_import([TEST_PATH / 'When morning gilds the skies_v2_0_0.wsg'], + helper.load_external_result_data(TEST_PATH / 'When morning gilds the skies_v2_0_0.json')) - def test_when_morning_gilds_song_import(self): - """ - Test that loading a Words of Worship file v2.0.0 works correctly - """ - self.file_import([TEST_PATH / 'When morning gilds the skies_v2_0_0.wsg'], - self.load_external_result_data(TEST_PATH / 'When morning gilds the skies_v2_0_0.json')) - def test_holy_holy_holy_song_import(self): - """ - Test that loading a Words of Worship file works correctly - """ - self.file_import([TEST_PATH / 'Holy Holy Holy Lord God Almighty_v2_1_2.wow-song'], - self.load_external_result_data(TEST_PATH / 'Holy Holy Holy Lord God Almighty_v2_1_2.json')) +def test_holy_holy_holy_song_import(registry): + """ + Test that loading a Words of Worship file works correctly + """ + with SongImportTestHelper('WordsOfWorshipImport', 'wordsofworship') as helper: + helper.file_import([TEST_PATH / 'Holy Holy Holy Lord God Almighty_v2_1_2.wow-song'], + helper.load_external_result_data(TEST_PATH / 'Holy Holy Holy Lord God Almighty_v2_1_2.json')) - def test_test_song_v2_0_0_song_import(self): - """ - Test that loading a Words of Worship file v2.0.0 works correctly - """ - self.file_import([TEST_PATH / 'Test_Song_v2_0_0.wsg'], - self.load_external_result_data(TEST_PATH / 'Test_Song_v2_0_0.json')) - def test_test_song_song_import(self): - """ - Test that loading a Words of Worship file v2.1.2 works correctly - """ - self.file_import([TEST_PATH / 'Test_Song_v2_1_2.wow-song'], - self.load_external_result_data(TEST_PATH / 'Test_Song_v2_1_2.json')) +def test_test_song_v2_0_0_song_import(registry): + """ + Test that loading a Words of Worship file v2.0.0 works correctly + """ + with SongImportTestHelper('WordsOfWorshipImport', 'wordsofworship') as helper: + helper.file_import([TEST_PATH / 'Test_Song_v2_0_0.wsg'], + helper.load_external_result_data(TEST_PATH / 'Test_Song_v2_0_0.json')) + + +def test_test_song_song_import(registry): + """ + Test that loading a Words of Worship file v2.1.2 works correctly + """ + with SongImportTestHelper('WordsOfWorshipImport', 'wordsofworship') as helper: + helper.file_import([TEST_PATH / 'Test_Song_v2_1_2.wow-song'], + helper.load_external_result_data(TEST_PATH / 'Test_Song_v2_1_2.json')) diff --git a/tests/helpers/songfileimport.py b/tests/helpers/songfileimport.py index 89189d634..608d6f26a 100644 --- a/tests/helpers/songfileimport.py +++ b/tests/helpers/songfileimport.py @@ -73,6 +73,15 @@ class SongImportTestHelper(object): self.add_author_patcher.stop() self.song_import_patcher.stop() + def __enter__(self): + """Make this class into a context manager""" + self.setUp() + return self + + def __exit__(self, exc_type, exc_value, traceback): + """Make this class into a context manager""" + self.tearDown + def load_external_result_data(self, file_path): """ A method to load and return an object containing the song data from an external file.