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

171 lines
8.6 KiB
Python
Raw Normal View History

2013-07-23 18:23:45 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# 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/>. #
##########################################################################
2013-07-23 18:23:45 +00:00
"""
This module contains tests for the SongShow Plus song importer.
"""
from unittest import TestCase
2018-10-02 04:39:42 +00:00
from unittest.mock import MagicMock, patch
2013-07-23 18:23:45 +00:00
2014-07-04 09:31:06 +00:00
from openlp.plugins.songs.lib.importers.foilpresenter import FoilPresenter
2013-07-23 18:23:45 +00:00
class TestFoilPresenter(TestCase):
"""
Test the functions in the :mod:`foilpresenterimport` module.
"""
def setUp(self):
self.to_str_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.to_str')
2014-07-04 09:31:06 +00:00
self.clean_song_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.clean_song')
self.objectify_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.objectify')
2013-07-23 18:23:45 +00:00
self.process_authors_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_authors')
2013-07-23 18:23:45 +00:00
self.process_cclinumber_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_cclinumber')
2013-07-23 18:23:45 +00:00
self.process_comments_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_comments')
2013-07-23 18:23:45 +00:00
self.process_lyrics_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_lyrics')
2013-07-23 18:23:45 +00:00
self.process_songbooks_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_songbooks')
2013-07-23 18:23:45 +00:00
self.process_titles_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_titles')
2013-07-23 18:23:45 +00:00
self.process_topics_patcher = \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.foilpresenter.FoilPresenter._process_topics')
self.re_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.re')
self.song_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.Song')
self.song_xml_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.SongXML')
self.translate_patcher = patch('openlp.plugins.songs.lib.importers.foilpresenter.translate')
2013-07-23 18:23:45 +00:00
self.mocked_child = self.to_str_patcher.start()
2013-07-23 18:23:45 +00:00
self.mocked_clean_song = self.clean_song_patcher.start()
self.mocked_objectify = self.objectify_patcher.start()
self.mocked_process_authors = self.process_authors_patcher.start()
self.mocked_process_cclinumber = self.process_cclinumber_patcher.start()
self.mocked_process_comments = self.process_comments_patcher.start()
self.mocked_process_lyrics = self.process_lyrics_patcher.start()
self.mocked_process_songbooks = self.process_songbooks_patcher.start()
self.mocked_process_titles = self.process_titles_patcher.start()
self.mocked_process_topics = self.process_topics_patcher.start()
self.mocked_re = self.re_patcher.start()
self.mocked_song = self.song_patcher.start()
self.mocked_song_xml = self.song_xml_patcher.start()
self.mocked_translate = self.translate_patcher.start()
2013-08-31 18:17:38 +00:00
self.mocked_child.return_value = 'Element Text'
self.mocked_translate.return_value = 'Translated String'
2013-07-23 18:23:45 +00:00
self.mocked_manager = MagicMock()
self.mocked_song_import = MagicMock()
def tearDown(self):
self.to_str_patcher.stop()
2013-07-23 18:23:45 +00:00
self.clean_song_patcher.stop()
self.objectify_patcher.stop()
self.process_authors_patcher.stop()
self.process_cclinumber_patcher.stop()
self.process_comments_patcher.stop()
self.process_lyrics_patcher.stop()
self.process_songbooks_patcher.stop()
self.process_titles_patcher.stop()
self.process_topics_patcher.stop()
self.re_patcher.stop()
self.song_patcher.stop()
self.song_xml_patcher.stop()
self.translate_patcher.stop()
2016-05-31 21:40:13 +00:00
def test_create_foil_presenter(self):
2013-07-23 18:23:45 +00:00
"""
2014-03-06 20:56:31 +00:00
Test creating an instance of the foil_presenter class
2013-07-23 18:23:45 +00:00
"""
# GIVEN: A mocked out "manager" and "SongImport" instance
mocked_manager = MagicMock()
mocked_song_import = MagicMock()
2014-03-06 20:56:31 +00:00
# WHEN: An foil_presenter instance is created
2013-07-23 18:23:45 +00:00
foil_presenter_instance = FoilPresenter(mocked_manager, mocked_song_import)
# THEN: The instance should not be None
2017-12-22 22:20:04 +00:00
assert foil_presenter_instance is not None, 'foil_presenter instance should not be none'
2013-07-23 18:23:45 +00:00
2016-05-31 21:40:13 +00:00
def test_no_xml(self):
2013-07-23 18:23:45 +00:00
"""
Test calling xml_to_song with out the xml argument
"""
# GIVEN: A mocked out "manager" and "SongImport" as well as an foil_presenter instance
mocked_manager = MagicMock()
mocked_song_import = MagicMock()
foil_presenter_instance = FoilPresenter(mocked_manager, mocked_song_import)
# WHEN: xml_to_song is called without valid an argument
2013-08-31 18:17:38 +00:00
for arg in [None, False, 0, '']:
2013-07-23 18:23:45 +00:00
result = foil_presenter_instance.xml_to_song(arg)
# Then: xml_to_song should return False
2017-12-22 22:20:04 +00:00
assert result is None, 'xml_to_song should return None when called with %s' % arg
2013-07-23 18:23:45 +00:00
2016-05-31 21:40:13 +00:00
def test_encoding_declaration_removal(self):
2013-07-23 18:23:45 +00:00
"""
Test that the encoding declaration is removed
"""
# GIVEN: A reset mocked out re and an instance of foil_presenter
self.mocked_re.reset()
foil_presenter_instance = FoilPresenter(self.mocked_manager, self.mocked_song_import)
# WHEN: xml_to_song is called with a string with an xml encoding declaration
2013-08-31 18:17:38 +00:00
foil_presenter_instance.xml_to_song('<?xml version="1.0" encoding="UTF-8"?>\n<foilpresenterfolie>')
2013-07-23 18:23:45 +00:00
# THEN: the xml encoding declaration should have been stripped
2013-08-31 18:17:38 +00:00
self.mocked_re.compile.sub.called_with('\n<foilpresenterfolie>')
2013-07-23 18:23:45 +00:00
2016-05-31 21:40:13 +00:00
def test_no_encoding_declaration(self):
2013-07-23 18:23:45 +00:00
"""
Check that the xml sting is left intact when no encoding declaration is made
"""
# GIVEN: A reset mocked out re and an instance of foil_presenter
self.mocked_re.reset()
foil_presenter_instance = FoilPresenter(self.mocked_manager, self.mocked_song_import)
# WHEN: xml_to_song is called with a string without an xml encoding declaration
2013-08-31 18:17:38 +00:00
foil_presenter_instance.xml_to_song('<foilpresenterfolie>')
2013-07-23 18:23:45 +00:00
2014-03-05 18:58:22 +00:00
# THEN: the string should have been left intact
2013-08-31 18:17:38 +00:00
self.mocked_re.compile.sub.called_with('<foilpresenterfolie>')
2013-07-23 18:23:45 +00:00
2016-05-31 21:40:13 +00:00
def test_process_lyrics_no_verses(self):
2013-07-23 18:23:45 +00:00
"""
Test that _process_lyrics handles song files that have no verses.
"""
# GIVEN: A mocked foilpresenterfolie with no attribute strophe, a mocked song and a
# foil presenter instance
self.process_lyrics_patcher.stop()
self.mocked_song_xml.reset()
mock_foilpresenterfolie = MagicMock()
del mock_foilpresenterfolie.strophen.strophe
mocked_song = MagicMock()
foil_presenter_instance = FoilPresenter(self.mocked_manager, self.mocked_song_import)
# WHEN: _process_lyrics is called
result = foil_presenter_instance._process_lyrics(mock_foilpresenterfolie, mocked_song)
2014-03-05 18:58:22 +00:00
# THEN: _process_lyrics should return None and the song_import log_error method should have been called once
2017-12-22 22:20:04 +00:00
assert result is None
2014-03-05 18:58:22 +00:00
self.mocked_song_import.log_error.assert_called_once_with('Element Text', 'Translated String')
self.process_lyrics_patcher.start()