2014-12-19 22:01:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2017-12-29 09:15:48 +00:00
|
|
|
# Copyright (c) 2008-2018 OpenLP Developers #
|
2014-12-19 22:01:15 +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 #
|
|
|
|
###############################################################################
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
|
|
|
This module contains tests for the lib submodule of the Songs plugin.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2018-10-27 11:05:41 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2013-04-23 19:08:07 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore
|
2013-04-23 19:08:07 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
|
|
|
from openlp.core.common.settings import Settings
|
2018-08-25 14:08:19 +00:00
|
|
|
from openlp.core.lib.serviceitem import ServiceItem
|
2016-01-04 19:20:21 +00:00
|
|
|
from openlp.plugins.songs.lib.db import AuthorType, Song
|
2017-04-24 05:17:55 +00:00
|
|
|
from openlp.plugins.songs.lib.mediaitem import SongMediaItem
|
2014-03-14 22:08:44 +00:00
|
|
|
from tests.helpers.testmixin import TestMixin
|
2013-04-23 19:08:07 +00:00
|
|
|
|
|
|
|
|
2014-03-14 22:08:44 +00:00
|
|
|
class TestMediaItem(TestCase, TestMixin):
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
|
|
|
Test the functions in the :mod:`lib` module.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Set up the components need for all tests.
|
|
|
|
"""
|
|
|
|
Registry.create()
|
2013-08-31 18:17:38 +00:00
|
|
|
Registry().register('service_list', MagicMock())
|
|
|
|
Registry().register('main_window', MagicMock())
|
2017-06-05 21:41:29 +00:00
|
|
|
self.mocked_plugin = MagicMock()
|
2013-10-02 21:07:20 +00:00
|
|
|
with patch('openlp.core.lib.mediamanageritem.MediaManagerItem._setup'), \
|
2013-07-17 13:59:35 +00:00
|
|
|
patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__'):
|
2017-06-05 21:41:29 +00:00
|
|
|
self.media_item = SongMediaItem(None, self.mocked_plugin)
|
2015-06-20 22:35:22 +00:00
|
|
|
self.media_item.save_auto_select_id = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
self.media_item.list_view = MagicMock()
|
|
|
|
self.media_item.list_view.save_auto_select_id = MagicMock()
|
|
|
|
self.media_item.list_view.clear = MagicMock()
|
|
|
|
self.media_item.list_view.addItem = MagicMock()
|
2016-04-17 21:22:30 +00:00
|
|
|
self.media_item.list_view.setCurrentItem = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
self.media_item.auto_select_id = -1
|
2014-05-03 10:42:18 +00:00
|
|
|
self.media_item.display_songbook = False
|
2014-07-09 12:41:55 +00:00
|
|
|
self.media_item.display_copyright_symbol = False
|
2014-10-22 20:43:05 +00:00
|
|
|
self.setup_application()
|
2014-03-14 22:08:44 +00:00
|
|
|
self.build_settings()
|
2013-07-06 20:17:24 +00:00
|
|
|
QtCore.QLocale.setDefault(QtCore.QLocale('en_GB'))
|
2013-04-23 19:08:07 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Delete all the C++ objects at the end so that we don't have a segfault
|
|
|
|
"""
|
2014-03-14 22:08:44 +00:00
|
|
|
self.destroy_settings()
|
2013-04-23 19:08:07 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_song(self):
|
2015-06-14 20:58:56 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results, plus a mocked QtListWidgetItem
|
2016-02-06 17:50:58 +00:00
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
2015-06-14 20:58:56 +00:00
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
|
|
|
mock_search_results = []
|
|
|
|
mock_song = MagicMock()
|
|
|
|
mock_song.id = 1
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.sort_key = 'My Song'
|
|
|
|
mock_song.authors = []
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp = MagicMock()
|
|
|
|
mock_song_temp.id = 2
|
|
|
|
mock_song_temp.title = 'My Temporary'
|
|
|
|
mock_song_temp.sort_key = 'My Temporary'
|
|
|
|
mock_song_temp.authors = []
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_author = MagicMock()
|
|
|
|
mock_author.display_name = 'My Author'
|
|
|
|
mock_song.authors.append(mock_author)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.authors.append(mock_author)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_song.temporary = False
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.temporary = True
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_search_results.append(mock_song)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_search_results.append(mock_song_temp)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
2016-04-17 21:22:30 +00:00
|
|
|
self.media_item.auto_select_id = 1
|
2015-06-14 20:58:56 +00:00
|
|
|
|
|
|
|
# WHEN: I display song search results
|
|
|
|
self.media_item.display_results_song(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2015-06-20 22:35:22 +00:00
|
|
|
self.media_item.save_auto_select_id.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('My Song (My Author)')
|
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id)
|
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
|
|
|
self.media_item.list_view.setCurrentItem.assert_called_with(mock_qlist_widget)
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_author(self):
|
2015-06-14 20:58:56 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results grouped by author with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results grouped by author, plus a mocked QtListWidgetItem
|
2016-02-06 17:50:58 +00:00
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
2015-06-14 20:58:56 +00:00
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
|
|
|
mock_search_results = []
|
|
|
|
mock_author = MagicMock()
|
|
|
|
mock_song = MagicMock()
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_author.display_name = 'My Author'
|
|
|
|
mock_author.songs = []
|
|
|
|
mock_song.id = 1
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.sort_key = 'My Song'
|
|
|
|
mock_song.temporary = False
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.id = 2
|
|
|
|
mock_song_temp.title = 'My Temporary'
|
|
|
|
mock_song_temp.sort_key = 'My Temporary'
|
|
|
|
mock_song_temp.temporary = True
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_author.songs.append(mock_song)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_author.songs.append(mock_song_temp)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_search_results.append(mock_author)
|
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
|
|
|
|
|
|
|
# WHEN: I display song search results grouped by author
|
|
|
|
self.media_item.display_results_author(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('My Author (My Song)')
|
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id)
|
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_book(self):
|
2016-03-19 15:01:10 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results grouped by book and entry with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results grouped by book and entry, plus a mocked QtListWidgetItem
|
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
2016-04-29 17:10:50 +00:00
|
|
|
mock_search_results = [('1', 'My Book', 'My Song', 1)]
|
2016-03-19 15:01:10 +00:00
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
|
|
|
|
|
|
|
# WHEN: I display song search results grouped by book
|
|
|
|
self.media_item.display_results_book(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('My Book #1: My Song')
|
2016-04-29 17:10:50 +00:00
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, 1)
|
2016-04-17 21:22:30 +00:00
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
2016-03-19 15:01:10 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_songbook_natural_sorting(self):
|
2016-04-29 17:10:50 +00:00
|
|
|
"""
|
|
|
|
Test that songbooks are sorted naturally
|
|
|
|
"""
|
2018-10-27 10:50:15 +00:00
|
|
|
# GIVEN: Search results grouped by book and entry
|
|
|
|
search_results = [('2', 'Thy Book', 'Thy Song', 50),
|
|
|
|
('2', 'My Book', 'Your Song', 7),
|
|
|
|
('10', 'My Book', 'Our Song', 12),
|
|
|
|
('1', 'My Book', 'My Song', 1),
|
|
|
|
('2', 'Thy Book', 'A Song', 8)]
|
|
|
|
|
|
|
|
# WHEN: I display song search results grouped by book
|
|
|
|
self.media_item.display_results_book(search_results)
|
|
|
|
|
|
|
|
# THEN: The songbooks are sorted inplace in the right (natural) order,
|
|
|
|
# grouped first by book, then by number, then by song title
|
|
|
|
assert search_results == [('1', 'My Book', 'My Song', 1),
|
|
|
|
('2', 'My Book', 'Your Song', 7),
|
|
|
|
('10', 'My Book', 'Our Song', 12),
|
|
|
|
('2', 'Thy Book', 'A Song', 8),
|
|
|
|
('2', 'Thy Book', 'Thy Song', 50)]
|
2016-04-29 17:10:50 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_topic(self):
|
2015-06-14 20:58:56 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results grouped by topic with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results grouped by topic, plus a mocked QtListWidgetItem
|
2016-02-06 17:50:58 +00:00
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
2015-06-14 20:58:56 +00:00
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
|
|
|
mock_search_results = []
|
|
|
|
mock_topic = MagicMock()
|
|
|
|
mock_song = MagicMock()
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_topic.name = 'My Topic'
|
|
|
|
mock_topic.songs = []
|
|
|
|
mock_song.id = 1
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.sort_key = 'My Song'
|
|
|
|
mock_song.temporary = False
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.id = 2
|
|
|
|
mock_song_temp.title = 'My Temporary'
|
|
|
|
mock_song_temp.sort_key = 'My Temporary'
|
|
|
|
mock_song_temp.temporary = True
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_topic.songs.append(mock_song)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_topic.songs.append(mock_song_temp)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_search_results.append(mock_topic)
|
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
|
|
|
|
|
|
|
# WHEN: I display song search results grouped by topic
|
|
|
|
self.media_item.display_results_topic(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('My Topic (My Song)')
|
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id)
|
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_themes(self):
|
2015-06-14 20:58:56 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results sorted by theme with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results sorted by theme, plus a mocked QtListWidgetItem
|
2016-02-06 17:50:58 +00:00
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
2015-06-14 20:58:56 +00:00
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
|
|
|
mock_search_results = []
|
|
|
|
mock_song = MagicMock()
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_song.id = 1
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.sort_key = 'My Song'
|
|
|
|
mock_song.theme_name = 'My Theme'
|
|
|
|
mock_song.temporary = False
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.id = 2
|
|
|
|
mock_song_temp.title = 'My Temporary'
|
|
|
|
mock_song_temp.sort_key = 'My Temporary'
|
|
|
|
mock_song_temp.theme_name = 'My Theme'
|
|
|
|
mock_song_temp.temporary = True
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_search_results.append(mock_song)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_search_results.append(mock_song_temp)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
|
|
|
|
|
|
|
# WHEN: I display song search results sorted by theme
|
|
|
|
self.media_item.display_results_themes(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('My Theme (My Song)')
|
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id)
|
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_display_results_cclinumber(self):
|
2015-06-14 20:58:56 +00:00
|
|
|
"""
|
|
|
|
Test displaying song search results sorted by CCLI number with basic song
|
|
|
|
"""
|
|
|
|
# GIVEN: Search results sorted by CCLI number, plus a mocked QtListWidgetItem
|
2016-02-06 17:50:58 +00:00
|
|
|
with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \
|
2015-06-14 20:58:56 +00:00
|
|
|
patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole:
|
|
|
|
mock_search_results = []
|
|
|
|
mock_song = MagicMock()
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp = MagicMock()
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_song.id = 1
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.sort_key = 'My Song'
|
|
|
|
mock_song.ccli_number = '12345'
|
|
|
|
mock_song.temporary = False
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_song_temp.id = 2
|
|
|
|
mock_song_temp.title = 'My Temporary'
|
|
|
|
mock_song_temp.sort_key = 'My Temporary'
|
|
|
|
mock_song_temp.ccli_number = '12346'
|
|
|
|
mock_song_temp.temporary = True
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_search_results.append(mock_song)
|
2016-04-17 21:22:30 +00:00
|
|
|
mock_search_results.append(mock_song_temp)
|
2015-06-14 20:58:56 +00:00
|
|
|
mock_qlist_widget = MagicMock()
|
|
|
|
MockedQListWidgetItem.return_value = mock_qlist_widget
|
|
|
|
|
|
|
|
# WHEN: I display song search results sorted by CCLI number
|
|
|
|
self.media_item.display_results_cclinumber(mock_search_results)
|
|
|
|
|
|
|
|
# THEN: The current list view is cleared, the widget is created, and the relevant attributes set
|
|
|
|
self.media_item.list_view.clear.assert_called_with()
|
2016-04-17 21:22:30 +00:00
|
|
|
MockedQListWidgetItem.assert_called_once_with('12345 (My Song)')
|
|
|
|
mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id)
|
|
|
|
self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-09-18 20:15:33 +00:00
|
|
|
@patch(u'openlp.plugins.songs.lib.mediaitem.Settings')
|
|
|
|
def test_build_song_footer_one_author_show_written_by(self, MockedSettings):
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
|
|
|
Test build songs footer with basic song and one author
|
|
|
|
"""
|
2016-09-18 20:15:33 +00:00
|
|
|
# GIVEN: A Song and a Service Item, mocked settings: True for 'songs/display written by'
|
|
|
|
# and False for 'core/ccli number' (ccli will cause traceback if true)
|
|
|
|
|
|
|
|
mocked_settings = MagicMock()
|
|
|
|
mocked_settings.value.side_effect = [True, False]
|
|
|
|
MockedSettings.return_value = mocked_settings
|
|
|
|
|
2013-04-23 19:08:07 +00:00
|
|
|
mock_song = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_song.title = 'My Song'
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_song.authors_songs = []
|
2013-04-23 19:08:07 +00:00
|
|
|
mock_author = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_author.display_name = 'my author'
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_author_song = MagicMock()
|
|
|
|
mock_author_song.author = mock_author
|
|
|
|
mock_song.authors_songs.append(mock_author_song)
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_song.copyright = 'My copyright'
|
2013-04-23 19:08:07 +00:00
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
author_list = self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: I get the following Array returned
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'Written by: my author', 'My copyright'], \
|
|
|
|
'The array should be returned correctly with a song, one author and copyright'
|
|
|
|
assert author_list == ['my author'], 'The author list should be returned correctly with one author'
|
2013-04-23 19:08:07 +00:00
|
|
|
|
2016-12-04 03:08:56 +00:00
|
|
|
@patch(u'openlp.plugins.songs.lib.mediaitem.Settings')
|
|
|
|
def test_build_song_footer_one_author_hide_written_by(self, MockedSettings):
|
|
|
|
"""
|
|
|
|
Test build songs footer with basic song and one author
|
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a Service Item, mocked settings: False for 'songs/display written by'
|
|
|
|
# and False for 'core/ccli number' (ccli will cause traceback if true)
|
|
|
|
|
|
|
|
mocked_settings = MagicMock()
|
|
|
|
mocked_settings.value.side_effect = [False, False]
|
|
|
|
MockedSettings.return_value = mocked_settings
|
|
|
|
|
|
|
|
mock_song = MagicMock()
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.authors_songs = []
|
|
|
|
mock_author = MagicMock()
|
|
|
|
mock_author.display_name = 'my author'
|
|
|
|
mock_author_song = MagicMock()
|
|
|
|
mock_author_song.author = mock_author
|
|
|
|
mock_song.authors_songs.append(mock_author_song)
|
|
|
|
mock_song.copyright = 'My copyright'
|
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
author_list = self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: I get the following Array returned
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'my author', 'My copyright'], \
|
|
|
|
'The array should be returned correctly with a song, one author and copyright, ' \
|
|
|
|
'text Written by should not be part of the text.'
|
|
|
|
assert author_list == ['my author'], 'The author list should be returned correctly with one author'
|
2016-12-04 03:08:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_song_footer_two_authors(self):
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
|
|
|
Test build songs footer with basic song and two authors
|
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a Service Item
|
|
|
|
mock_song = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_song.title = 'My Song'
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_song.authors_songs = []
|
2013-04-23 19:08:07 +00:00
|
|
|
mock_author = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_author.display_name = 'my author'
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_author_song = MagicMock()
|
|
|
|
mock_author_song.author = mock_author
|
2014-04-21 10:06:17 +00:00
|
|
|
mock_author_song.author_type = AuthorType.Music
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_song.authors_songs.append(mock_author_song)
|
2013-04-23 19:08:07 +00:00
|
|
|
mock_author = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_author.display_name = 'another author'
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_author_song = MagicMock()
|
|
|
|
mock_author_song.author = mock_author
|
2014-04-21 10:06:17 +00:00
|
|
|
mock_author_song.author_type = AuthorType.Words
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_song.authors_songs.append(mock_author_song)
|
|
|
|
mock_author = MagicMock()
|
|
|
|
mock_author.display_name = 'translator'
|
|
|
|
mock_author_song = MagicMock()
|
|
|
|
mock_author_song.author = mock_author
|
2014-04-21 10:06:17 +00:00
|
|
|
mock_author_song.author_type = AuthorType.Translation
|
2014-03-30 18:01:03 +00:00
|
|
|
mock_song.authors_songs.append(mock_author_song)
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_song.copyright = 'My copyright'
|
2013-04-23 19:08:07 +00:00
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
author_list = self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: I get the following Array returned
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'Words: another author', 'Music: my author',
|
|
|
|
'Translation: translator', 'My copyright'], \
|
|
|
|
'The array should be returned correctly with a song, two authors and copyright'
|
|
|
|
assert author_list == ['another author', 'my author', 'translator'], \
|
|
|
|
'The author list should be returned correctly with two authors'
|
2013-04-23 19:08:07 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_song_footer_base_ccli(self):
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
2014-03-30 18:01:03 +00:00
|
|
|
Test build songs footer with basic song and a CCLI number
|
2013-04-23 19:08:07 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a Service Item and a configured CCLI license
|
|
|
|
mock_song = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.copyright = 'My copyright'
|
2013-04-23 19:08:07 +00:00
|
|
|
service_item = ServiceItem(None)
|
2013-08-31 18:17:38 +00:00
|
|
|
Settings().setValue('core/ccli number', '1234')
|
2013-04-23 19:08:07 +00:00
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: I get the following Array returned
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'My copyright', 'CCLI License: 1234'], \
|
|
|
|
'The array should be returned correctly with a song, an author, copyright and ccli'
|
2013-04-23 19:08:07 +00:00
|
|
|
|
|
|
|
# WHEN: I amend the CCLI value
|
2013-08-31 18:17:38 +00:00
|
|
|
Settings().setValue('core/ccli number', '4321')
|
2013-04-23 19:08:07 +00:00
|
|
|
self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: I would get an amended footer string
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'My copyright', 'CCLI License: 4321'], \
|
|
|
|
'The array should be returned correctly with a song, an author, copyright and amended ccli'
|
2014-05-13 09:44:19 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_song_footer_base_songbook(self):
|
2014-05-03 10:42:18 +00:00
|
|
|
"""
|
2016-01-04 19:23:42 +00:00
|
|
|
Test build songs footer with basic song and multiple songbooks
|
2014-05-03 10:42:18 +00:00
|
|
|
"""
|
2014-05-03 10:53:51 +00:00
|
|
|
# GIVEN: A Song and a Service Item
|
2016-01-09 15:23:11 +00:00
|
|
|
song = Song()
|
|
|
|
song.title = 'My Song'
|
2018-03-09 21:58:45 +00:00
|
|
|
song.alternate_title = ''
|
2016-01-09 15:23:11 +00:00
|
|
|
song.copyright = 'My copyright'
|
|
|
|
song.authors_songs = []
|
2016-01-09 15:53:49 +00:00
|
|
|
song.songbook_entries = []
|
2016-01-09 15:23:11 +00:00
|
|
|
song.ccli_number = ''
|
2019-01-04 19:48:42 +00:00
|
|
|
# song.topics = None
|
2016-01-04 19:23:42 +00:00
|
|
|
book1 = MagicMock()
|
2018-03-09 21:58:45 +00:00
|
|
|
book1.name = 'My songbook'
|
2016-01-04 19:23:42 +00:00
|
|
|
book2 = MagicMock()
|
2018-03-09 21:58:45 +00:00
|
|
|
book2.name = 'Thy songbook'
|
2016-01-09 15:23:11 +00:00
|
|
|
song.songbookentries = []
|
2016-01-09 15:53:49 +00:00
|
|
|
song.add_songbook_entry(book1, '12')
|
|
|
|
song.add_songbook_entry(book2, '502A')
|
2014-05-03 10:42:18 +00:00
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
2016-01-09 15:23:11 +00:00
|
|
|
self.media_item.generate_footer(service_item, song)
|
2014-05-03 10:42:18 +00:00
|
|
|
|
|
|
|
# THEN: The songbook should not be in the footer
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'My copyright']
|
2014-05-03 10:42:18 +00:00
|
|
|
|
|
|
|
# WHEN: I activate the "display songbook" option
|
|
|
|
self.media_item.display_songbook = True
|
2016-01-09 15:23:11 +00:00
|
|
|
self.media_item.generate_footer(service_item, song)
|
2014-05-03 10:42:18 +00:00
|
|
|
|
|
|
|
# THEN: The songbook should be in the footer
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'My copyright', 'My songbook #12, Thy songbook #502A']
|
2014-05-13 09:53:02 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_song_footer_copyright_enabled(self):
|
2014-07-09 12:41:55 +00:00
|
|
|
"""
|
|
|
|
Test building song footer with displaying the copyright symbol
|
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a Service Item; displaying the copyright symbol is enabled
|
|
|
|
self.media_item.display_copyright_symbol = True
|
|
|
|
mock_song = MagicMock()
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.copyright = 'My copyright'
|
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: The copyright symbol should be in the footer
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', '© My copyright']
|
2014-07-09 12:41:55 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_song_footer_copyright_disabled(self):
|
2014-07-12 20:00:58 +00:00
|
|
|
"""
|
|
|
|
Test building song footer without displaying the copyright symbol
|
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a Service Item; displaying the copyright symbol should be disabled by default
|
|
|
|
mock_song = MagicMock()
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.copyright = 'My copyright'
|
|
|
|
service_item = ServiceItem(None)
|
|
|
|
|
|
|
|
# WHEN: I generate the Footer with default settings
|
|
|
|
self.media_item.generate_footer(service_item, mock_song)
|
|
|
|
|
|
|
|
# THEN: The copyright symbol should not be in the footer
|
2017-12-22 17:51:59 +00:00
|
|
|
assert service_item.raw_footer == ['My Song', 'My copyright']
|
2014-07-12 20:00:58 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_authors_match(self):
|
2014-05-13 09:44:19 +00:00
|
|
|
"""
|
|
|
|
Test the author matching when importing a song from a service
|
|
|
|
"""
|
|
|
|
# GIVEN: A song and a string with authors
|
|
|
|
song = MagicMock()
|
|
|
|
song.authors = []
|
|
|
|
author = MagicMock()
|
|
|
|
author.display_name = "Hans Wurst"
|
|
|
|
song.authors.append(author)
|
|
|
|
author2 = MagicMock()
|
|
|
|
author2.display_name = "Max Mustermann"
|
|
|
|
song.authors.append(author2)
|
|
|
|
# There are occasions where an author appears twice in a song (with different types).
|
|
|
|
# We need to make sure that this case works (lp#1313538)
|
|
|
|
author3 = MagicMock()
|
|
|
|
author3.display_name = "Max Mustermann"
|
|
|
|
song.authors.append(author3)
|
|
|
|
authors_str = "Hans Wurst, Max Mustermann, Max Mustermann"
|
|
|
|
|
|
|
|
# WHEN: Checking for matching
|
2014-05-13 17:52:08 +00:00
|
|
|
result = self.media_item._authors_match(song, authors_str)
|
2014-05-13 09:44:19 +00:00
|
|
|
|
|
|
|
# THEN: They should match
|
2017-12-22 17:51:59 +00:00
|
|
|
assert result is True, "Authors should match"
|
2014-05-13 17:52:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_authors_dont_match(self):
|
2014-05-21 19:55:16 +00:00
|
|
|
# GIVEN: A song and a string with authors
|
|
|
|
song = MagicMock()
|
|
|
|
song.authors = []
|
|
|
|
author = MagicMock()
|
|
|
|
author.display_name = "Hans Wurst"
|
|
|
|
song.authors.append(author)
|
|
|
|
author2 = MagicMock()
|
|
|
|
author2.display_name = "Max Mustermann"
|
|
|
|
song.authors.append(author2)
|
|
|
|
# There are occasions where an author appears twice in a song (with different types).
|
|
|
|
# We need to make sure that this case works (lp#1313538)
|
|
|
|
author3 = MagicMock()
|
|
|
|
author3.display_name = "Max Mustermann"
|
|
|
|
song.authors.append(author3)
|
|
|
|
|
2014-05-13 17:52:08 +00:00
|
|
|
# WHEN: An author is missing in the string
|
|
|
|
authors_str = "Hans Wurst, Max Mustermann"
|
|
|
|
result = self.media_item._authors_match(song, authors_str)
|
|
|
|
|
|
|
|
# THEN: They should not match
|
2017-12-22 17:51:59 +00:00
|
|
|
assert result is False, "Authors should not match"
|
2015-06-14 20:58:56 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_build_remote_search(self):
|
2016-01-05 19:32:12 +00:00
|
|
|
"""
|
|
|
|
Test results for the remote search api
|
|
|
|
"""
|
|
|
|
# GIVEN: A Song and a search a JSON array should be returned.
|
|
|
|
mock_song = MagicMock()
|
|
|
|
mock_song.id = 123
|
|
|
|
mock_song.title = 'My Song'
|
|
|
|
mock_song.search_title = 'My Song'
|
|
|
|
mock_song.alternate_title = 'My alternative'
|
|
|
|
self.media_item.search_entire = MagicMock()
|
|
|
|
self.media_item.search_entire.return_value = [mock_song]
|
|
|
|
|
|
|
|
# WHEN: I process a search
|
|
|
|
search_results = self.media_item.search('My Song', False)
|
|
|
|
|
|
|
|
# THEN: The correct formatted results are returned
|
2017-12-22 17:51:59 +00:00
|
|
|
assert search_results == [[123, 'My Song', 'My alternative']]
|
2017-06-05 21:41:29 +00:00
|
|
|
|
|
|
|
@patch('openlp.plugins.songs.lib.mediaitem.Book')
|
|
|
|
@patch('openlp.plugins.songs.lib.mediaitem.SongBookEntry')
|
|
|
|
@patch('openlp.plugins.songs.lib.mediaitem.Song')
|
|
|
|
@patch('openlp.plugins.songs.lib.mediaitem.or_')
|
|
|
|
def test_entire_song_search(self, mocked_or, MockedSong, MockedSongBookEntry, MockedBook):
|
|
|
|
"""
|
|
|
|
Test that searching the entire song does the right queries
|
|
|
|
"""
|
|
|
|
# GIVEN: A song media item, a keyword and some mocks
|
|
|
|
keyword = 'Jesus'
|
|
|
|
mocked_or.side_effect = lambda a, b, c, d, e: ' '.join([a, b, c, d, e])
|
|
|
|
MockedSong.search_title.like.side_effect = lambda a: a
|
|
|
|
MockedSong.search_lyrics.like.side_effect = lambda a: a
|
|
|
|
MockedSong.comments.like.side_effect = lambda a: a
|
|
|
|
MockedSongBookEntry.entry.like.side_effect = lambda a: a
|
|
|
|
MockedBook.name.like.side_effect = lambda a: a
|
|
|
|
|
|
|
|
# WHEN: search_entire_song() is called with the keyword
|
|
|
|
self.media_item.search_entire(keyword)
|
|
|
|
|
|
|
|
# THEN: The correct calls were made
|
|
|
|
MockedSong.search_title.like.assert_called_once_with('%jesus%')
|
|
|
|
MockedSong.search_lyrics.like.assert_called_once_with('%jesus%')
|
|
|
|
MockedSong.comments.like.assert_called_once_with('%jesus%')
|
|
|
|
MockedSongBookEntry.entry.like.assert_called_once_with('%jesus%')
|
|
|
|
MockedBook.name.like.assert_called_once_with('%jesus%')
|
|
|
|
mocked_or.assert_called_once_with('%jesus%', '%jesus%', '%jesus%', '%jesus%', '%jesus%')
|
|
|
|
self.mocked_plugin.manager.session.query.assert_called_once_with(MockedSong)
|
|
|
|
|
2017-12-22 17:51:59 +00:00
|
|
|
assert self.mocked_plugin.manager.session.query.mock_calls[4][0] == '().join().join().filter().all'
|