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

127 lines
5.0 KiB
Python
Raw Normal View History

2013-04-23 19:08:07 +00:00
"""
This module contains tests for the lib submodule of the Songs plugin.
"""
import os
from tempfile import mkstemp
from unittest import TestCase
from mock import patch, MagicMock
2013-07-06 20:17:24 +00:00
from PyQt4 import QtCore, QtGui
2013-04-23 19:08:07 +00:00
from openlp.core.lib import Registry, ServiceItem, Settings
from openlp.plugins.songs.lib.mediaitem import SongMediaItem
class TestMediaItem(TestCase):
"""
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())
2013-04-23 19:08:07 +00:00
with patch('openlp.core.lib.mediamanageritem.MediaManagerItem.__init__'), \
2013-07-17 13:59:35 +00:00
patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__'):
2013-04-23 19:08:07 +00:00
self.media_item = SongMediaItem(MagicMock(), MagicMock())
2013-08-31 18:17:38 +00:00
fd, self.ini_file = mkstemp('.ini')
2013-04-23 19:08:07 +00:00
Settings().set_filename(self.ini_file)
self.application = QtGui.QApplication.instance()
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
"""
del self.application
# Not all tests use settings!
try:
os.unlink(self.ini_file)
os.unlink(Settings().fileName())
2013-04-23 19:17:25 +00:00
except Exception:
2013-04-23 19:08:07 +00:00
pass
def build_song_footer_one_author_test(self):
"""
Test build songs footer with basic song and one author
"""
# GIVEN: A Song and a Service Item
mock_song = MagicMock()
2013-08-31 18:17:38 +00:00
mock_song.title = 'My Song'
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'
2013-04-23 19:08:07 +00:00
mock_song.authors = []
mock_song.authors.append(mock_author)
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
2013-08-31 18:17:38 +00:00
self.assertEqual(service_item.raw_footer, ['My Song', 'my author', 'My copyright'],
'The array should be returned correctly with a song, one author and copyright')
self.assertEqual(author_list, ['my author'],
'The author list should be returned correctly with one author')
2013-04-23 19:08:07 +00:00
def build_song_footer_two_authors_test(self):
"""
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'
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'
2013-04-23 19:08:07 +00:00
mock_song.authors = []
mock_song.authors.append(mock_author)
mock_author = MagicMock()
2013-08-31 18:17:38 +00:00
mock_author.display_name = 'another author'
2013-04-23 19:08:07 +00:00
mock_song.authors.append(mock_author)
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
2013-08-31 18:17:38 +00:00
self.assertEqual(service_item.raw_footer, ['My Song', 'my author and another author', 'My copyright'],
'The array should be returned correctly with a song, two authors and copyright')
self.assertEqual(author_list, ['my author', 'another author'],
'The author list should be returned correctly with two authors')
2013-04-23 19:08:07 +00:00
def build_song_footer_base_ccli_test(self):
"""
Test build songs footer with basic song and two authors
"""
# 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'
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'
2013-04-23 19:08:07 +00:00
mock_song.authors = []
mock_song.authors.append(mock_author)
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)
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
2013-08-31 18:17:38 +00:00
self.assertEqual(service_item.raw_footer, ['My Song', 'my author', '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
2013-08-31 18:17:38 +00:00
self.assertEqual(service_item.raw_footer, ['My Song', 'my author', 'My copyright', 'CCLI License: 4321'],
'The array should be returned correctly with a song, an author, copyright and amended ccli')