2014-07-03 17:57:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-04-21 17:23:00 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:01:36 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2014-04-21 17:23:00 +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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
This module contains tests for the OpenSong song importer.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
from unittest import TestCase
|
2017-04-24 05:17:55 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
2014-04-21 17:23:00 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
2017-09-30 20:16:30 +00:00
|
|
|
from openlp.core.common.path import Path
|
2017-04-24 05:17:55 +00:00
|
|
|
from openlp.plugins.songs.lib.importers.opensong import OpenSongImport
|
2016-06-07 07:35:06 +00:00
|
|
|
|
|
|
|
from tests.helpers.songfileimport import SongImportTestHelper
|
2014-04-21 17:23:00 +00:00
|
|
|
|
|
|
|
TEST_PATH = os.path.abspath(
|
|
|
|
os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources', 'opensongsongs'))
|
|
|
|
|
|
|
|
|
|
|
|
class TestOpenSongFileImport(SongImportTestHelper):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.importer_class_name = 'OpenSongImport'
|
2014-07-04 09:31:06 +00:00
|
|
|
self.importer_module_name = 'opensong'
|
2014-04-21 17:23:00 +00:00
|
|
|
super(TestOpenSongFileImport, self).__init__(*args, **kwargs)
|
|
|
|
|
2016-07-25 20:07:07 +00:00
|
|
|
@patch('openlp.plugins.songs.lib.importers.opensong.Settings')
|
|
|
|
def test_song_import(self, mocked_settings):
|
2014-04-21 17:23:00 +00:00
|
|
|
"""
|
|
|
|
Test that loading an OpenSong file works correctly on various files
|
|
|
|
"""
|
2016-07-25 20:07:07 +00:00
|
|
|
# Mock out the settings - always return False
|
|
|
|
mocked_returned_settings = MagicMock()
|
2017-02-26 21:14:49 +00:00
|
|
|
mocked_returned_settings.value.side_effect = lambda value: True if value == 'songs/enable chords' else False
|
2016-07-25 20:07:07 +00:00
|
|
|
mocked_settings.return_value = mocked_returned_settings
|
|
|
|
# Do the test import
|
2017-09-30 20:16:30 +00:00
|
|
|
self.file_import([Path(TEST_PATH, 'Amazing Grace')],
|
2014-04-21 17:23:00 +00:00
|
|
|
self.load_external_result_data(os.path.join(TEST_PATH, 'Amazing Grace.json')))
|
2017-09-30 20:16:30 +00:00
|
|
|
self.file_import([Path(TEST_PATH, 'Beautiful Garden Of Prayer')],
|
2014-04-21 17:23:00 +00:00
|
|
|
self.load_external_result_data(os.path.join(TEST_PATH, 'Beautiful Garden Of Prayer.json')))
|
2017-09-30 20:16:30 +00:00
|
|
|
self.file_import([Path(TEST_PATH, 'One, Two, Three, Four, Five')],
|
2014-04-30 20:39:40 +00:00
|
|
|
self.load_external_result_data(os.path.join(TEST_PATH, 'One, Two, Three, Four, Five.json')))
|
2017-09-30 20:16:30 +00:00
|
|
|
self.file_import([Path(TEST_PATH, 'Amazing Grace2')],
|
2016-01-23 08:19:12 +00:00
|
|
|
self.load_external_result_data(os.path.join(TEST_PATH, 'Amazing Grace.json')))
|
2017-09-30 20:16:30 +00:00
|
|
|
self.file_import([Path(TEST_PATH, 'Amazing Grace with bad CCLI')],
|
2016-06-07 07:35:06 +00:00
|
|
|
self.load_external_result_data(os.path.join(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.
|
|
|
|
"""
|
2014-09-25 14:59:56 +00:00
|
|
|
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
|
|
|
|
self.assertIsNotNone(importer, 'Import should not be none')
|
|
|
|
|
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.
|
|
|
|
self.assertIsNone(importer.do_import(), 'do_import should return None when import_source is not a list')
|
|
|
|
self.assertEqual(mocked_import_wizard.progress_bar.setMaximum.called, False,
|
2014-04-22 21:48:48 +00:00
|
|
|
'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.
|
|
|
|
self.assertIsNone(importer.do_import(), 'do_import should return None when import_source is a list '
|
|
|
|
'and stop_import_flag is True')
|
|
|
|
mocked_import_wizard.progress_bar.setMaximum.assert_called_with(len(importer.import_source))
|