From 86fb3437599062d4972d6a0c7815ebdee1b7ca2f Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Sun, 24 Mar 2013 11:32:03 +0000 Subject: [PATCH] Added test for SongShowPlus --- .../plugins/songs/lib/songshowplusimport.py | 17 +- .../songs/test_songshowplusimport.py | 169 ++++++++++++++++++ .../openlp_plugins_songs_lib/__init__.py | 0 .../test_songshowplusimport.py | 31 ---- tests/resources/Amazing Grace.sbsong | Bin 0 -> 1018 bytes 5 files changed, 178 insertions(+), 39 deletions(-) create mode 100644 tests/functional/openlp_plugins/songs/test_songshowplusimport.py delete mode 100644 tests/functional/openlp_plugins_songs_lib/__init__.py delete mode 100644 tests/functional/openlp_plugins_songs_lib/test_songshowplusimport.py create mode 100644 tests/resources/Amazing Grace.sbsong diff --git a/openlp/plugins/songs/lib/songshowplusimport.py b/openlp/plugins/songs/lib/songshowplusimport.py index 84e888856..57a0f3236 100644 --- a/openlp/plugins/songs/lib/songshowplusimport.py +++ b/openlp/plugins/songs/lib/songshowplusimport.py @@ -89,8 +89,9 @@ class SongShowPlusImport(SongImport): * .sbsong """ - otherList = {} - otherCount = 0 + + other_count = 0 + other_list = {} def __init__(self, manager, **kwargs): """ @@ -109,8 +110,8 @@ class SongShowPlusImport(SongImport): if self.stop_import_flag: return self.sspVerseOrderList = [] - other_count = 0 - other_list = {} + self.other_count = 0 + self.other_list = {} file_name = os.path.split(file)[1] self.import_wizard.increment_progress_bar(WizardStrings.ImportingType % file_name, 0) song_data = open(file, 'rb') @@ -204,11 +205,11 @@ class SongShowPlusImport(SongImport): elif verse_type == "pre-chorus": verse_tag = VerseType.tags[VerseType.PreChorus] else: - if verse_name not in self.otherList: + if verse_name not in self.other_list: if ignore_unique: return None - self.otherCount += 1 - self.otherList[verse_name] = str(self.otherCount) + self.other_count += 1 + self.other_list[verse_name] = str(self.other_count) verse_tag = VerseType.tags[VerseType.Other] - verse_number = self.otherList[verse_name] + verse_number = self.other_list[verse_name] return verse_tag + verse_number diff --git a/tests/functional/openlp_plugins/songs/test_songshowplusimport.py b/tests/functional/openlp_plugins/songs/test_songshowplusimport.py new file mode 100644 index 000000000..38962b79d --- /dev/null +++ b/tests/functional/openlp_plugins/songs/test_songshowplusimport.py @@ -0,0 +1,169 @@ +""" +This module contains tests for the OpenLP song importer. +""" + +import os +from unittest import TestCase +from mock import call, patch, MagicMock + +from openlp.plugins.songs.lib import VerseType +from openlp.plugins.songs.lib.songshowplusimport import SongShowPlusImport + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources')) + +class TestSongShowPlusFileImport(TestCase): + """ + Test the functions in the :mod:`lib` module. + """ + def create_importer_test(self): + """ + Test creating an instance of the SongShow Plus file importer + """ + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.songshowplusimport.SongImport'): + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = SongShowPlusImport(mocked_manager) + + # THEN: The importer object should not be None + self.assertIsNotNone(importer, u'Import should not be none') + + def toOpenLPVerseTag_test(self): + """ + Test toOpenLPVerseTag method + """ + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.songshowplusimport.SongImport'): + mocked_manager = MagicMock() + importer = SongShowPlusImport(mocked_manager) + + # WHEN: Supplied with the following arguments replicating verses being added + test_values = [(u'Verse 1', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse 2', VerseType.tags[VerseType.Verse] + u'2'), + (u'verse1', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse1', VerseType.tags[VerseType.Verse] + u'1'), + (u'chorus 1', VerseType.tags[VerseType.Chorus] + u'1'), + (u'bridge 1', VerseType.tags[VerseType.Bridge] + u'1'), + (u'pre-chorus 1', VerseType.tags[VerseType.PreChorus] + u'1'), + (u'different 1', VerseType.tags[VerseType.Other] + u'1'), + (u'random 1', VerseType.tags[VerseType.Other] + u'2')] + + # THEN: The returned value should should correlate with the input arguments + for original_tag, openlp_tag in test_values: + self.assertEquals(importer.toOpenLPVerseTag(original_tag), openlp_tag, + u'SongShowPlusImport.toOpenLPVerseTag should return "%s" when called with "%s"' + % (openlp_tag, original_tag)) + + # WHEN: Supplied with the following arguments replicating a verse order being added + test_values = [(u'Verse 1', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse 2', VerseType.tags[VerseType.Verse] + u'2'), + (u'verse1', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse', VerseType.tags[VerseType.Verse] + u'1'), + (u'Verse1', VerseType.tags[VerseType.Verse] + u'1'), + (u'chorus 1', VerseType.tags[VerseType.Chorus] + u'1'), + (u'bridge 1', VerseType.tags[VerseType.Bridge] + u'1'), + (u'pre-chorus 1', VerseType.tags[VerseType.PreChorus] + u'1'), + (u'different 1', VerseType.tags[VerseType.Other] + u'1'), + (u'random 1', VerseType.tags[VerseType.Other] + u'2'), + (u'unused 2', None)] + + # THEN: The returned value should should correlate with the input arguments + for original_tag, openlp_tag in test_values: + self.assertEquals(importer.toOpenLPVerseTag(original_tag, ignore_unique=True), openlp_tag, + u'SongShowPlusImport.toOpenLPVerseTag should return "%s" when called with "%s"' + % (openlp_tag, original_tag)) + + + + + def import_source_test(self): + """ + Test creating an instance of the SongShow Plus file importer + """ + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.songshowplusimport.SongImport'): + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = SongShowPlusImport(mocked_manager) + importer.import_wizard = mocked_import_wizard + importer.stop_import_flag = True + + # WHEN: Import source is a string + importer.import_source = u'not a list' + + # THEN: doImport should return none and the progress bar maximum should not be set. + self.assertIsNone(importer.doImport(), u'doImport should return None when import_source is not a list') + self.assertEquals(mocked_import_wizard.progress_bar.setMaximum.called, False, + u'setMaxium on import_wizard.progress_bar should not have been called') + + # WHEN: Import source is an int + importer.import_source = 0 + + # THEN: doImport should return none and the progress bar maximum should not be set. + self.assertIsNone(importer.doImport(), u'doImport should return None when import_source is not a list') + self.assertEquals(mocked_import_wizard.progress_bar.setMaximum.called, False, + u'setMaxium on import_wizard.progress_bar should not have been called') + + # WHEN: Import source is a list + importer.import_source = [u'List', u'of', u'files'] + + # THEN: doImport should return none and the progress bar maximum should be set. + self.assertIsNone(importer.doImport(), + u'doImport 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 creating an instance of the SongShow Plus file importer + """ + + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.songshowplusimport.SongImport'): + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + mocked_parse_author = MagicMock() + mocked_add_copyright = MagicMock() + mocked_add_verse = MagicMock() + mocked_finish = MagicMock() + mocked_finish.return_value = True + importer = SongShowPlusImport(mocked_manager) + importer.import_wizard = mocked_import_wizard + importer.stop_import_flag = False + importer.parse_author = mocked_parse_author + importer.addCopyright = mocked_add_copyright + importer.addVerse = mocked_add_verse + importer.finish = mocked_finish + importer.topics = [] + + # WHEN: Import source is a string + importer.import_source = [os.path.join(TEST_PATH, u'Amazing Grace.sbsong')] + + # THEN: doImport should return none and the progress bar maximum should not be set. + self.assertIsNone(importer.doImport(), u'doImport should return None when import_source is not a list') + self.assertEquals(importer.title, u'Amazing Grace (Demonstration)', + u'Title for Amazing Grace.sbsong should be "Amazing Grace (Demonstration)"') + calls = [call(u'John Newton'), call(u'Edwin Excell'), call(u'John P. Rees')] + mocked_parse_author.assert_has_calls(calls) + mocked_add_copyright.assert_called_with(u'Public Domain ') + self.assertEquals(importer.ccliNumber, 22025, u'ccliNumber should be set as 22025 for Amazing Grace.sbsong') + calls = [call(u'Amazing grace! How sweet the sound!\r\nThat saved a wretch like me!\r\n' + u'I once was lost, but now am found;\r\nWas blind, but now I see.', u'v1'), + call(u"'Twas grace that taught my heart to fear,\r\nAnd grace my fears relieved.\r\n" + u"How precious did that grace appear,\r\nThe hour I first believed.", u'v2'), + call(u'The Lord has promised good to me,\r\nHis Word my hope secures.\r\n' + u'He will my shield and portion be\r\nAs long as life endures.', u'v3'), + call(u"Thro' many dangers, toils and snares\r\nI have already come.\r\n" + u"'Tis grace that brought me safe thus far,\r\nAnd grace will lead me home.", u'v4'), + call(u"When we've been there ten thousand years,\r\nBright shining as the sun,\r\n" + u"We've no less days to sing God's praise,\r\nThan when we first begun.", u'v5')] + mocked_add_verse.assert_has_calls(calls) + self.assertEquals(importer.topics, [u'Assurance', u'Grace', u'Praise', u'Salvation']) + self.assertEquals(importer.comments, u'\n\n\n', u'comments should be "\\n\\n\\n" Amazing Grace.sbsong') + self.assertEquals(importer.songBookName, u'Demonstration Songs', u'songBookName should be ' + u'"Demonstration Songs"') + self.assertEquals(importer.songNumber, 0, u'songNumber should be 0') + self.assertEquals(importer.verseOrderList, [], u'verseOrderList should be empty') + mocked_finish.assert_called_with() \ No newline at end of file diff --git a/tests/functional/openlp_plugins_songs_lib/__init__.py b/tests/functional/openlp_plugins_songs_lib/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/openlp_plugins_songs_lib/test_songshowplusimport.py b/tests/functional/openlp_plugins_songs_lib/test_songshowplusimport.py deleted file mode 100644 index 77733a1ba..000000000 --- a/tests/functional/openlp_plugins_songs_lib/test_songshowplusimport.py +++ /dev/null @@ -1,31 +0,0 @@ -""" - Package to test the openlp.plugins.songs.lib package. -""" -import os - -from unittest import TestCase -from mock import MagicMock, patch -from openlp.plugins.songs.lib import songshowplusimport - -TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources')) - - -class TestSongShowPlusImport(TestCase): - -#test do import - # set self.import source to non list type. Do import should return None or False? - # set self.import source to a list of files - # importWizard.progressBar should be set to the number of files in the list - # set self.stop_import_flag to true. Do import should return None or False? - - - - def do_import_test(self): - mocked_manager = MagicMock() - - with patch(u'openlp.plugins.songs.lib.songshowplusimport.SongImport') as mocked_song_import: - ssp_import_class = songshowplusimport.SongShowPlusImport(mocked_manager) - - songshowplusimport.SongShowPlusImport.importSource = '' - - self.assertEquals(ssp_import_class.SongShowPlusImport().doImport(), False) diff --git a/tests/resources/Amazing Grace.sbsong b/tests/resources/Amazing Grace.sbsong new file mode 100644 index 0000000000000000000000000000000000000000..14b7c35971ad673c98ca9ad8e128e0ad3922444c GIT binary patch literal 1018 zcmZ8g-)j>=5KdZq+S7<2K31nFL?J{8_~cWhwT*}r8_4tKZgRJ{w;Oi%dX4xXC@3QS z&3<#&AL_#;vpcijeDlprzt`(M!k6q#EA>g+f{wh(n4TVR#*FANZ?v30Foz50$#@$t?#ZjWC-u_kj1F9- z5cULjg1FV&!S79p*qKaTOkO51`}lU{u8X*JW!;7)U$Xm#=!j^#q&ql%lYoBm<+8X! zirc4S*HCDfBgK*_xZ39XgLGc1NI{)(PKp}OF)PXFk4zQAJ0oWyOrruB7vhMPbtDTQ zRnbZiUJcR(oT$a-*WMWg=CN@3C0w?WAH%s|v`mm5DWj^3GE%jnl9k8V(F(?BkWOuW z5eTQ;1@de(gW`CQN)>C*nRa!cT<0BH2dviX4q}c1OILfE(MtOeX?Y1CoIVSu?c`jd z-Z`IB32JNaDjlFg;T%96>Iau&9cUpT!qcrG8)voWAVeUGHby+5)NG(1h_9WO*+D`S zBBEiqfNu1PiEZA#6%OBp!;R$Yy!38Jm9iVkl`Ys~R-)4;v}nO9B$GCj=nyI6S>+qb zT*Y88oP*t8k}kdLGzCqCe6fT?tN%1@IUB&BK$HX^q4Qhl>?A)IC0lBEh-6EKiAnJQ zYApyZ6>g*>kmj}5(m>R1WrI*;J65%YZ_y%HM}`Bsq&9Fm3hk!3d?;!wh>b{$9};$1 zuX5fJlwC-YlNRmz#i=r9?Fv7HTNWzWPSX_sy+1Rg B8W{ip literal 0 HcmV?d00001