Migrated songselect and songbeamer tests to the SongImportTestHelper based structure.

This commit is contained in:
Tomas Groth 2015-12-20 17:46:39 +01:00
parent 037d3f4f7d
commit 2a4205c0a1
7 changed files with 96 additions and 125 deletions

View File

@ -250,10 +250,10 @@ class SongFormat(object):
},
Lyrix: {
'class': LyrixImport,
'name': 'Lyrix',
'name': 'LyriX',
'prefix': 'lyrix',
'filter': '%s (*.txt)' % translate('SongsPlugin.ImportWizardForm', 'Lyrix Files'),
'comboBoxText': translate('SongsPlugin.ImportWizardForm', 'Lyrix (exported txt-files)')
'filter': '%s (*.txt)' % translate('SongsPlugin.ImportWizardForm', 'LyriX Files'),
'comboBoxText': translate('SongsPlugin.ImportWizardForm', 'LyriX (Exported TXT-files)')
},
MediaShout: {
'name': 'MediaShout',

View File

@ -48,7 +48,7 @@ class CCLIFileImport(SongImport):
:param manager: The song manager for the running OpenLP installation.
:param kwargs: The files to be imported.
"""
SongImport.__init__(self, manager, **kwargs)
super(CCLIFileImport, self).__init__(manager, **kwargs)
def do_import(self):
"""
@ -161,7 +161,7 @@ class CCLIFileImport(SongImport):
elif line.startswith('Author='):
song_author = line[7:].strip()
elif line.startswith('Copyright='):
self.copyright = line[10:].strip()
self.add_copyright(line[10:].strip())
elif line.startswith('Themes='):
song_topics = line[7:].strip().replace(' | ', '/t')
elif line.startswith('Fields='):
@ -318,14 +318,14 @@ class CCLIFileImport(SongImport):
if line_number == 2:
line_number += 1
if clean_line.startswith('©'):
self.copyright = clean_line
self.add_copyright(clean_line)
else:
song_author = clean_line
# n=3, authors
elif line_number == 3:
line_number += 1
if song_author:
self.copyright = clean_line
self.add_copyright(clean_line)
else:
song_author = clean_line
# line_number=4, comments lines before last line

View File

@ -26,27 +26,28 @@ This module contains tests for the Songbeamer song importer.
import os
from unittest import TestCase
from tests.helpers.songfileimport import SongImportTestHelper
from tests.functional import MagicMock, patch
from openlp.plugins.songs.lib.importers.songbeamer import SongBeamerImport
from openlp.plugins.songs.lib import VerseType
from openlp.core.common import Registry
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', '..', 'resources', 'songbeamersongs'))
SONG_TEST_DATA = {
'Lobsinget dem Herrn.sng': {
'title': 'GL 1 - Lobsinget dem Herrn',
'verses': [
('1. Lobsinget dem Herrn,\no preiset Ihn gern!\nAnbetung und Lob Ihm gebühret.\n', 'v'),
('2. Lobsingt Seiner Lieb´,\ndie einzig ihn trieb,\nzu sterben für unsere Sünden!\n', 'v'),
('3. Lobsingt Seiner Macht!\nSein Werk ist vollbracht:\nEr sitzet zur Rechten des Vaters.\n', 'v'),
('4. Lobsingt seiner Treu´,\ndie immerdar neu,\nbis Er uns zur Herrlichket führet!\n\n', 'v')
],
'song_book_name': 'Glaubenslieder I',
'song_number': "1",
'authors': ['Carl Brockhaus', 'Johann Jakob Vetter']
}
}
class TestSongBeamerFileImport(SongImportTestHelper):
def __init__(self, *args, **kwargs):
self.importer_class_name = 'SongBeamerImport'
self.importer_module_name = 'songbeamer'
super(TestSongBeamerFileImport, self).__init__(*args, **kwargs)
def test_song_import(self):
"""
Test that loading an OpenSong file works correctly on various files
"""
self.file_import([os.path.join(TEST_PATH, 'Lobsinget dem Herrn.sng')],
self.load_external_result_data(os.path.join(TEST_PATH, 'Lobsinget dem Herrn.json')))
class TestSongBeamerImport(TestCase):
@ -115,51 +116,6 @@ class TestSongBeamerImport(TestCase):
'do_import should return None when import_source is a list and stop_import_flag is True')
mocked_import_wizard.progress_bar.setMaximum.assert_called_with(len(importer.import_source))
def file_import_test(self):
"""
Test the actual import of real song files and check that the imported data is correct.
"""
# GIVEN: Test files with a mocked out SongImport class, a mocked out "manager", a mocked out "import_wizard",
# and mocked out "author", "add_copyright", "add_verse", "finish" methods.
with patch('openlp.plugins.songs.lib.importers.songbeamer.SongImport'):
for song_file in SONG_TEST_DATA:
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
mocked_add_verse = MagicMock()
mocked_finish = MagicMock()
mocked_finish.return_value = True
importer = SongBeamerImport(mocked_manager, filenames=[])
importer.import_wizard = mocked_import_wizard
importer.stop_import_flag = False
importer.add_verse = mocked_add_verse
importer.finish = mocked_finish
# WHEN: Importing each file
importer.import_source = [os.path.join(TEST_PATH, song_file)]
title = SONG_TEST_DATA[song_file]['title']
add_verse_calls = SONG_TEST_DATA[song_file]['verses']
song_book_name = SONG_TEST_DATA[song_file]['song_book_name']
song_number = SONG_TEST_DATA[song_file]['song_number']
song_authors = SONG_TEST_DATA[song_file]['authors']
# THEN: do_import should return none, the song data should be as expected, and finish should have been
# called.
self.assertIsNone(importer.do_import(), 'do_import should return None when it has completed')
self.assertEqual(importer.title, title, 'title for %s should be "%s"' % (song_file, title))
for verse_text, verse_tag in add_verse_calls:
mocked_add_verse.assert_any_call(verse_text, verse_tag)
if song_book_name:
self.assertEqual(importer.song_book_name, song_book_name,
'song_book_name for %s should be "%s"' % (song_file, song_book_name))
if song_number:
self.assertEqual(importer.song_number, song_number,
'song_number for %s should be %s' % (song_file, song_number))
if song_authors:
for author in importer.authors:
self.assertIn(author, song_authors)
mocked_finish.assert_called_with()
def check_verse_marks_test(self):
"""
Tests different lines to see if a verse mark is detected or not

View File

@ -28,6 +28,7 @@ from urllib.error import URLError
from PyQt4 import QtGui
from tests.helpers.songfileimport import SongImportTestHelper
from openlp.core import Registry
from openlp.plugins.songs.forms.songselectform import SongSelectForm, SearchWorker
from openlp.plugins.songs.lib import Song
@ -37,6 +38,9 @@ from openlp.plugins.songs.lib.importers.cclifile import CCLIFileImport
from tests.functional import MagicMock, patch, call
from tests.helpers.testmixin import TestMixin
TEST_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources', 'songselect'))
class TestSongSelectImport(TestCase, TestMixin):
"""
@ -645,68 +649,21 @@ class TestSongSelectForm(TestCase, TestMixin):
mocked_view_button.setEnabled.assert_called_with(True)
class TestSongSelectFileImport(TestCase, TestMixin):
"""
Test SongSelect file import
"""
def setUp(self):
"""
Initial setups
"""
Registry.create()
test_song_name = 'TestSong'
self.file_name = os.path.join('tests', 'resources', 'songselect', test_song_name)
self.title = 'Test Song'
self.ccli_number = '0000000'
self.authors = ['Author One', 'Author Two']
self.topics = ['Adoration', 'Praise']
class TestSongSelectFileImport(SongImportTestHelper):
def songselect_import_bin_file_test(self):
"""
Verify import SongSelect BIN file parses file properly
"""
# GIVEN: Text file to import and mocks
copyright_bin = '2011 OpenLP Programmer One (Admin. by OpenLP One) | ' \
'Openlp Programmer Two (Admin. by OpenLP Two)'
verses_bin = [
['v1', 'Line One Verse One\nLine Two Verse One\nLine Three Verse One\nLine Four Verse One', None],
['v2', 'Line One Verse Two\nLine Two Verse Two\nLine Three Verse Two\nLine Four Verse Two', None]
]
song_import = CCLIFileImport(manager=None, filename=['{}.bin'.format(self.file_name)])
def __init__(self, *args, **kwargs):
self.importer_class_name = 'CCLIFileImport'
self.importer_module_name = 'cclifile'
super(TestSongSelectFileImport, self).__init__(*args, **kwargs)
with patch.object(song_import, 'import_wizard'), patch.object(song_import, 'finish'):
# WHEN: We call the song importer
song_import.do_import()
# THEN: Song values should be equal to test values in setUp
self.assertEquals(song_import.title, self.title, 'Song title should match')
self.assertEquals(song_import.ccli_number, self.ccli_number, 'CCLI Song Number should match')
self.assertEquals(song_import.authors, self.authors, 'Author(s) should match')
self.assertEquals(song_import.copyright, copyright_bin, 'Copyright should match')
self.assertEquals(song_import.topics, self.topics, 'Theme(s) should match')
self.assertEquals(song_import.verses, verses_bin, 'Verses should match with test verses')
def songselect_import_text_file_test(self):
def test_song_import(self):
"""
Verify import SongSelect TEXT file parses file properly
Test that loading an OpenSong file works correctly on various files
"""
# GIVEN: Text file to import and mocks
copyright_txt = '© 2011 OpenLP Programmer One (Admin. by OpenLP One)'
verses_txt = [
['v1', 'Line One Verse One\r\nLine Two Verse One\r\nLine Three Verse One\r\nLine Four Verse One', None],
['v2', 'Line One Verse Two\r\nLine Two Verse Two\r\nLine Three Verse Two\r\nLine Four Verse Two', None]
]
song_import = CCLIFileImport(manager=None, filename=['{}.txt'.format(self.file_name)])
with patch.object(song_import, 'import_wizard'), patch.object(song_import, 'finish'):
# WHEN: We call the song importer
song_import.do_import()
# THEN: Song values should be equal to test values in setUp
self.assertEquals(song_import.title, self.title, 'Song title should match')
self.assertEquals(song_import.ccli_number, self.ccli_number, 'CCLI Song Number should match')
self.assertEquals(song_import.authors, self.authors, 'Author(s) should match')
self.assertEquals(song_import.copyright, copyright_txt, 'Copyright should match')
self.assertEquals(song_import.verses, verses_txt, 'Verses should match with test verses')
self.file_import([os.path.join(TEST_PATH, 'TestSong.bin')],
self.load_external_result_data(os.path.join(TEST_PATH, 'TestSong-bin.json')))
self.file_import([os.path.join(TEST_PATH, 'TestSong.txt')],
self.load_external_result_data(os.path.join(TEST_PATH, 'TestSong-txt.json')))
class TestSearchWorker(TestCase, TestMixin):

View File

@ -0,0 +1,12 @@
{
"title": "GL 1 - Lobsinget dem Herrn",
"verses": [
["1. Lobsinget dem Herrn,\no preiset Ihn gern!\nAnbetung und Lob Ihm gebühret.\n", "v"],
["2. Lobsingt Seiner Lieb´,\ndie einzig ihn trieb,\nzu sterben für unsere Sünden!\n", "v"],
["3. Lobsingt Seiner Macht!\nSein Werk ist vollbracht:\nEr sitzet zur Rechten des Vaters.\n", "v"],
["4. Lobsingt seiner Treu´,\ndie immerdar neu,\nbis Er uns zur Herrlichket führet!\n\n", "v"]
],
"song_book_name": "Glaubenslieder I",
"song_number": "1",
"authors": ["Carl Brockhaus", "Johann Jakob Vetter"]
}

View File

@ -0,0 +1,25 @@
{
"authors": [
"Author One",
"Author Two"
],
"ccli_number": "0000000",
"song_number": 0,
"title": "Test Song",
"topics": [
"Adoration",
"Praise"
],
"copyright": "2011 OpenLP Programmer One (Admin. by OpenLP One) | Openlp Programmer Two (Admin. by OpenLP Two)",
"verse_order_list": [],
"verses": [
[
"Line One Verse One\nLine Two Verse One\nLine Three Verse One\nLine Four Verse One\n",
"v"
],
[
"Line One Verse Two\nLine Two Verse Two\nLine Three Verse Two\nLine Four Verse Two\n",
"v"
]
]
}

View File

@ -0,0 +1,21 @@
{
"authors": [
"Author One",
"Author Two"
],
"ccli_number": "0000000",
"song_number": 0,
"title": "Test Song",
"copyright": "© 2011 OpenLP Programmer One (Admin. by OpenLP One)",
"verse_order_list": [],
"verses": [
[
"Line One Verse One\nLine Two Verse One\nLine Three Verse One\nLine Four Verse One\n",
"v"
],
[
"Line One Verse Two\nLine Two Verse Two\nLine Three Verse Two\nLine Four Verse Two\n",
"v"
]
]
}