openlp/tests/functional/openlp_plugins/songs/test_opensongimport.py

118 lines
5.7 KiB
Python
Raw Normal View History

2014-07-03 17:57:13 +00:00
# -*- coding: utf-8 -*-
2014-04-21 17:23:00 +00:00
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2020 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# 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/>. #
##########################################################################
2014-04-21 17:23:00 +00:00
"""
This module contains tests for the OpenSong song importer.
"""
from unittest import TestCase
2018-10-02 04:39:42 +00:00
from unittest.mock import MagicMock, patch
2014-04-21 17:23:00 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
from openlp.plugins.songs.lib.importers.opensong import OpenSongImport
from tests.helpers.songfileimport import SongImportTestHelper
2017-12-22 21:20:49 +00:00
from tests.utils.constants import RESOURCE_PATH
2014-04-21 17:23:00 +00:00
2018-10-02 04:39:42 +00:00
TEST_PATH = RESOURCE_PATH / 'songs' / 'opensong'
2014-04-21 17:23:00 +00:00
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'))
2014-04-21 17:23:00 +00:00
class TestOpenSongImport(TestCase):
"""
Test the functions in the :mod:`opensongimport` module.
"""
def setUp(self):
"""
Create the registry
"""
Registry.create()
2016-05-31 21:40:13 +00:00
def test_create_importer(self):
2014-04-21 17:23:00 +00:00
"""
Test creating an instance of the OpenSong file importer
"""
# GIVEN: A mocked out SongImport class, and a mocked out "manager"
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.opensong.SongImport'):
2014-04-21 17:23:00 +00:00
mocked_manager = MagicMock()
# WHEN: An importer object is created
2017-09-30 20:16:30 +00:00
importer = OpenSongImport(mocked_manager, file_paths=[])
2014-04-21 17:23:00 +00:00
# THEN: The importer object should not be None
2017-12-22 22:20:04 +00:00
assert importer is not None, 'Import should not be none'
2014-04-21 17:23:00 +00:00
2016-05-31 21:40:13 +00:00
def test_invalid_import_source(self):
2014-04-21 17:23:00 +00:00
"""
Test OpenSongImport.do_import handles different invalid import_source values
"""
# GIVEN: A mocked out SongImport class, and a mocked out "manager"
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.opensong.SongImport'):
2014-04-21 17:23:00 +00:00
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OpenSongImport(mocked_manager, file_paths=[])
2014-04-21 17:23:00 +00:00
importer.import_wizard = mocked_import_wizard
importer.stop_import_flag = True
# WHEN: Import source is not a list
for source in ['not a list', 0]:
importer.import_source = source
# THEN: do_import should return none and the progress bar maximum should not be set.
2017-12-22 21:04:29 +00:00
assert importer.do_import() is None, 'do_import should return None when import_source is not a list'
assert mocked_import_wizard.progress_bar.setMaximum.called is False, \
'setMaximum on import_wizard.progress_bar should not have been called'
2014-04-21 17:23:00 +00:00
2016-05-31 21:40:13 +00:00
def test_valid_import_source(self):
2014-04-21 17:23:00 +00:00
"""
Test OpenSongImport.do_import handles different invalid import_source values
"""
# GIVEN: A mocked out SongImport class, and a mocked out "manager"
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.opensong.SongImport'):
2014-04-21 17:23:00 +00:00
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OpenSongImport(mocked_manager, file_paths=[])
2014-04-21 17:23:00 +00:00
importer.import_wizard = mocked_import_wizard
importer.stop_import_flag = True
# WHEN: Import source is a list
importer.import_source = ['List', 'of', 'files']
# THEN: do_import should return none and the progress bar setMaximum should be called with the length of
# import_source.
2017-12-22 21:04:29 +00:00
assert importer.do_import() is None, \
'do_import should return None when import_source is a list and stop_import_flag is True'
2014-04-21 17:23:00 +00:00
mocked_import_wizard.progress_bar.setMaximum.assert_called_with(len(importer.import_source))