Add tests

This commit is contained in:
Tim Bentley 2013-04-23 20:08:07 +01:00
parent 581c83e70d
commit fba86c5552
5 changed files with 134 additions and 6 deletions

View File

@ -52,3 +52,4 @@ This allows OpenLP to use ``self.object`` for all the GUI elements while keeping
them separate from the functionality, so that it is easier to recreate the GUI
from the .ui files later if necessary.
"""
from editsongform import EditSongForm

View File

@ -72,10 +72,7 @@ class SongMediaItem(MediaManagerItem):
def __init__(self, parent, plugin):
self.icon_path = u'songs/song'
MediaManagerItem.__init__(self, parent, plugin)
self.editSongForm = EditSongForm(self, self.main_window, self.plugin.manager)
self.openLyrics = OpenLyrics(self.plugin.manager)
self.single_service_item = False
self.songMaintenanceForm = SongMaintenanceForm(self.plugin.manager, self)
# Holds information about whether the edit is remotely triggered and
# which Song is required.
self.remoteSong = -1
@ -135,6 +132,12 @@ class SongMediaItem(MediaManagerItem):
'Maintain the lists of authors, topics and books.'))
def initialise(self):
"""
Initialise variables when they cannot be initialised in the constructor.
"""
self.songMaintenanceForm = SongMaintenanceForm(self.plugin.manager, self)
self.editSongForm = EditSongForm(self, self.main_window, self.plugin.manager)
self.openLyrics = OpenLyrics(self.plugin.manager)
self.search_text_edit.set_search_types([
(SongSearch.Entire, u':/songs/song_search_all.png',
translate('SongsPlugin.MediaItem', 'Entire Song'),

View File

@ -235,8 +235,7 @@ class SongsPlugin(Plugin):
u'delete': translate('SongsPlugin', 'Delete the selected song.'),
u'preview': translate('SongsPlugin', 'Preview the selected song.'),
u'live': translate('SongsPlugin', 'Send the selected song live.'),
u'service': translate('SongsPlugin',
'Add the selected song to the service.')
u'service': translate('SongsPlugin', 'Add the selected song to the service.')
}
self.set_plugin_ui_text_strings(tooltips)

View File

@ -1,7 +1,7 @@
"""
Package to test the openlp.core.lib.screenlist package.
"""
import copy
from unittest import TestCase
from mock import MagicMock

View File

@ -0,0 +1,125 @@
"""
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
from PyQt4 import QtGui
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()
Registry().register(u'service_list', MagicMock())
with patch('openlp.core.lib.mediamanageritem.MediaManagerItem.__init__'), \
patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__') :
self.media_item = SongMediaItem(MagicMock(), MagicMock())
fd, self.ini_file = mkstemp(u'.ini')
Settings().set_filename(self.ini_file)
self.application = QtGui.QApplication.instance()
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())
except:
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()
mock_song.title = u'My Song'
mock_author = MagicMock()
mock_author.display_name = u'my author'
mock_song.authors = []
mock_song.authors.append(mock_author)
mock_song.copyright = u'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
self.assertEqual(service_item.raw_footer, [u'My Song', u'my author', u'My copyright'],
u'The array should be returned correctly with a song, one author and copyright')
self.assertEqual(author_list, [u'my author'],
u'The author list should be returned correctly with one author')
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()
mock_song.title = u'My Song'
mock_author = MagicMock()
mock_author.display_name = u'my author'
mock_song.authors = []
mock_song.authors.append(mock_author)
mock_author = MagicMock()
mock_author.display_name = u'another author'
mock_song.authors.append(mock_author)
mock_song.copyright = u'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
self.assertEqual(service_item.raw_footer, [u'My Song', u'my author and another author', u'My copyright'],
u'The array should be returned correctly with a song, two authors and copyright')
self.assertEqual(author_list, [u'my author', u'another author'],
u'The author list should be returned correctly with two authors')
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()
mock_song.title = u'My Song'
mock_author = MagicMock()
mock_author.display_name = u'my author'
mock_song.authors = []
mock_song.authors.append(mock_author)
mock_song.copyright = u'My copyright'
service_item = ServiceItem(None)
Settings().setValue(u'core/ccli number', u'1234')
# WHEN: I generate the Footer with default settings
self.media_item.generate_footer(service_item, mock_song)
# THEN: I get the following Array returned
self.assertEqual(service_item.raw_footer, [u'My Song', u'my author', u'My copyright', u'CCLI License: 1234'],
u'The array should be returned correctly with a song, an author, copyright and ccli')
# WHEN: I amend the CCLI value
Settings().setValue(u'core/ccli number', u'4321')
self.media_item.generate_footer(service_item, mock_song)
# THEN: I would get an amended footer string
self.assertEqual(service_item.raw_footer, [u'My Song', u'my author', u'My copyright', u'CCLI License: 4321'],
u'The array should be returned correctly with a song, an author, copyright and amended ccli')