openlp/tests/openlp_plugins/songs/test_opsproimport.py

170 lines
6.9 KiB
Python
Raw Normal View History

2016-03-07 22:27:28 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2020-12-30 21:42:49 +00:00
# Copyright (c) 2008-2021 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/>. #
##########################################################################
2016-03-07 22:27:28 +00:00
"""
This module contains tests for the WorshipCenter Pro song importer.
"""
2016-05-31 21:40:13 +00:00
from unittest import TestCase, skipUnless
2018-10-02 04:39:42 +00:00
from unittest.mock import MagicMock, patch
from tests.utils import load_external_result_data
from tests.utils.constants import RESOURCE_PATH
2016-03-07 22:27:28 +00:00
2016-05-31 21:40:13 +00:00
try:
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
2016-05-31 21:40:13 +00:00
from openlp.plugins.songs.lib.importers.opspro import OPSProImport
CAN_RUN_TESTS = True
except ImportError:
CAN_RUN_TESTS = False
2016-03-20 08:28:41 +00:00
2017-12-22 21:20:49 +00:00
TEST_PATH = RESOURCE_PATH / 'songs' / 'opspro'
2016-03-07 22:27:28 +00:00
def _get_item(data, key):
"""
Get an item or return a blank string
"""
if key in data:
return data[key]
return ''
def _build_data(test_file, dual_language):
"""
Build the test data
"""
song = MagicMock()
song.ID = 100
song.SongNumber = 123
song.SongBookName = 'The Song Book'
song.Title = 'Song Title'
song.CopyrightText = 'Music and text by me'
song.Version = '1'
song.Origin = '...'
lyrics = MagicMock()
2017-12-24 07:15:50 +00:00
lyrics.Lyrics = (TEST_PATH / test_file).read_bytes().decode()
lyrics.Type = 1
lyrics.IsDualLanguage = dual_language
return song, lyrics
2016-05-31 21:40:13 +00:00
@skipUnless(CAN_RUN_TESTS, 'Not Windows, skipping test')
2016-03-07 22:27:28 +00:00
class TestOpsProSongImport(TestCase):
"""
Test the functions in the :mod:`opsproimport` module.
"""
def setUp(self):
"""
Create the registry
"""
Registry.create()
@patch('openlp.plugins.songs.lib.importers.opspro.SongImport')
2016-05-31 21:40:13 +00:00
def test_create_importer(self, mocked_songimport):
2016-03-07 22:27:28 +00:00
"""
Test creating an instance of the OPS Pro file importer
"""
# GIVEN: A mocked out SongImport class, and a mocked out "manager"
mocked_manager = MagicMock()
# WHEN: An importer object is created
2017-09-30 20:16:30 +00:00
importer = OPSProImport(mocked_manager, file_paths=[])
2016-03-07 22:27:28 +00:00
# THEN: The importer object should not be None
2017-12-22 21:04:29 +00:00
assert importer is not None, 'Import should not be none'
2016-03-07 22:27:28 +00:00
@patch('openlp.plugins.songs.lib.importers.opspro.SongImport')
2016-05-31 21:40:13 +00:00
def test_detect_chorus(self, mocked_songimport):
2016-03-07 22:27:28 +00:00
"""
Test importing lyrics with a chorus in OPS Pro
"""
2016-03-11 21:56:07 +00:00
# GIVEN: A mocked out SongImport class, a mocked out "manager" and a mocked song and lyrics entry
2016-03-07 22:27:28 +00:00
mocked_manager = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OPSProImport(mocked_manager, file_paths=[])
2016-03-07 22:27:28 +00:00
importer.finish = MagicMock()
song, lyrics = _build_data('you are so faithfull.txt', False)
2016-03-19 06:20:12 +00:00
2016-03-07 22:27:28 +00:00
# WHEN: An importer object is created
importer.process_song(song, lyrics, [])
2016-03-11 21:56:07 +00:00
# THEN: The imported data should look like expected
2017-12-22 22:21:38 +00:00
result_data = load_external_result_data(TEST_PATH / 'You are so faithful.json')
2017-12-22 21:04:29 +00:00
assert importer.verses == _get_item(result_data, 'verses')
assert importer.verse_order_list_generated == _get_item(result_data, 'verse_order_list')
2016-03-11 21:56:07 +00:00
2016-03-16 21:28:29 +00:00
@patch('openlp.plugins.songs.lib.importers.opspro.SongImport')
2016-05-31 21:40:13 +00:00
def test_join_and_split(self, mocked_songimport):
2016-03-16 21:28:29 +00:00
"""
Test importing lyrics with a split and join tags works in OPS Pro
"""
# GIVEN: A mocked out SongImport class, a mocked out "manager" and a mocked song and lyrics entry
mocked_manager = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OPSProImport(mocked_manager, file_paths=[])
2016-03-16 21:28:29 +00:00
importer.finish = MagicMock()
song, lyrics = _build_data('amazing grace.txt', False)
2016-03-19 06:20:12 +00:00
2016-03-18 22:09:49 +00:00
# WHEN: An importer object is created
importer.process_song(song, lyrics, [])
# THEN: The imported data should look like expected
2017-12-22 22:21:38 +00:00
result_data = load_external_result_data(TEST_PATH / 'Amazing Grace.json')
2017-12-22 21:04:29 +00:00
assert importer.verses == _get_item(result_data, 'verses')
assert importer.verse_order_list_generated == _get_item(result_data, 'verse_order_list')
2016-03-18 22:09:49 +00:00
@patch('openlp.plugins.songs.lib.importers.opspro.SongImport')
2016-05-31 21:40:13 +00:00
def test_trans_off_tag(self, mocked_songimport):
2016-03-18 22:09:49 +00:00
"""
Test importing lyrics with a split and join and translations tags works in OPS Pro
"""
# GIVEN: A mocked out SongImport class, a mocked out "manager" and a mocked song and lyrics entry
mocked_manager = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OPSProImport(mocked_manager, file_paths=[])
2016-03-18 22:09:49 +00:00
importer.finish = MagicMock()
song, lyrics = _build_data('amazing grace2.txt', True)
2016-03-19 06:20:12 +00:00
2016-03-16 21:28:29 +00:00
# WHEN: An importer object is created
importer.process_song(song, lyrics, [])
# THEN: The imported data should look like expected
2017-12-22 22:21:38 +00:00
result_data = load_external_result_data(TEST_PATH / 'Amazing Grace.json')
2017-12-22 21:04:29 +00:00
assert importer.verses == _get_item(result_data, 'verses')
assert importer.verse_order_list_generated == _get_item(result_data, 'verse_order_list')
2016-03-16 21:28:29 +00:00
2016-03-19 06:20:12 +00:00
@patch('openlp.plugins.songs.lib.importers.opspro.SongImport')
2016-05-31 21:40:13 +00:00
def test_trans_tag(self, mocked_songimport):
2016-03-19 06:20:12 +00:00
"""
Test importing lyrics with various translations tags works in OPS Pro
"""
# GIVEN: A mocked out SongImport class, a mocked out "manager" and a mocked song and lyrics entry
mocked_manager = MagicMock()
2017-09-30 20:16:30 +00:00
importer = OPSProImport(mocked_manager, file_paths=[])
2016-03-19 06:20:12 +00:00
importer.finish = MagicMock()
song, lyrics = _build_data('amazing grace3.txt', True)
2016-03-19 06:20:12 +00:00
# WHEN: An importer object is created
importer.process_song(song, lyrics, [])
# THEN: The imported data should look like expected
2017-12-22 22:21:38 +00:00
result_data = load_external_result_data(TEST_PATH / 'Amazing Grace3.json')
2017-12-22 21:04:29 +00:00
assert importer.verses == _get_item(result_data, 'verses')
assert importer.verse_order_list_generated == _get_item(result_data, 'verse_order_list')