From 7c635ce79b72c455e643a4ea036f68577faeb62d Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Tue, 19 Feb 2013 19:03:50 +0000 Subject: [PATCH 01/16] Fixes #1114457 by counting the records --- openlp/plugins/songs/lib/ewimport.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 8a344c189..06f062e90 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -107,10 +107,6 @@ class EasyWorshipSongImport(SongImport): self.encoding = retrieve_windows_encoding(self.encoding) if not self.encoding: return - # There does not appear to be a _reliable_ way of getting the number - # of songs/records, so let's use file blocks for measuring progress. - total_blocks = (db_size - header_size) / (block_size * 1024) - self.importWizard.progressBar.setMaximum(total_blocks) # Read the field description information db_file.seek(120) field_info = db_file.read(num_fields * 2) @@ -134,12 +130,22 @@ class EasyWorshipSongImport(SongImport): except IndexError: # This is the wrong table success = False - # Loop through each block of the file + # There does not appear to be a _reliable_ way of getting the number of songs/records, so loop through the file + # blocks and total the number of records. Store the information in a list so we dont have to do all this again. cur_block = first_block + total_count = 0 + block_list = [] while cur_block != 0 and success: - db_file.seek(header_size + ((cur_block - 1) * 1024 * block_size)) + cur_block_pos = header_size + ((cur_block - 1) * 1024 * block_size) + db_file.seek(cur_block_pos) cur_block, rec_count = struct.unpack(' Date: Tue, 19 Feb 2013 21:56:23 +0000 Subject: [PATCH 02/16] Unindented set progress max --- openlp/plugins/songs/lib/ewimport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 06f062e90..6947da17b 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -142,7 +142,7 @@ class EasyWorshipSongImport(SongImport): rec_count = (rec_count + record_size) / record_size block_list.append((cur_block_pos, rec_count)) total_count += rec_count - self.importWizard.progressBar.setMaximum(total_count) + self.importWizard.progressBar.setMaximum(total_count) for block in block_list: cur_block_pos, rec_count = block db_file.seek(cur_block_pos + 6) From b61cfce03f57c81303213359c0f19761e5c168fb Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Thu, 28 Feb 2013 20:45:57 +0000 Subject: [PATCH 03/16] Started on test --- openlp/plugins/songs/forms/songimportform.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/openlp/plugins/songs/forms/songimportform.py b/openlp/plugins/songs/forms/songimportform.py index b635b309b..8e812f00e 100644 --- a/openlp/plugins/songs/forms/songimportform.py +++ b/openlp/plugins/songs/forms/songimportform.py @@ -35,7 +35,7 @@ import os from PyQt4 import QtCore, QtGui -from openlp.core.lib import Settings, UiStrings, translate +from openlp.core.lib import Registry, Settings, UiStrings, translate from openlp.core.lib.ui import critical_error_message_box from openlp.core.ui.wizard import OpenLPWizard, WizardStrings from openlp.plugins.songs.lib.importer import SongFormat, SongFormatSelect @@ -489,6 +489,16 @@ class SongImportForm(OpenLPWizard): self.formatWidgets[this_format][u'importWidget'] = importWidget return importWidget + def _get_main_window(self): + """ + Adds the main window to the class dynamically + """ + if not hasattr(self, u'_main_window'): + self._main_window = Registry().get(u'main_window') + return self._main_window + + main_window = property(_get_main_window) + class SongImportSourcePage(QtGui.QWizardPage): """ From 03db7ff2a3bd39364450c3df3b219d18609a9c08 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Fri, 29 Mar 2013 21:13:55 +0000 Subject: [PATCH 04/16] Started EasyWorship import test --- .../openlp_plugins_songs_lib/test_ewimport.py | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 tests/functional/openlp_plugins_songs_lib/test_ewimport.py diff --git a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py new file mode 100644 index 000000000..afe4dd1c9 --- /dev/null +++ b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +""" +This module contains tests for the EasyWorship 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.ewimport import EasyWorshipSongImport, FieldDescEntry + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources')) + +class TestFieldDesc: + def __init__(self, name, type, size): + self.name = name + self.type = type + self.size = size + +TEST_DATA_ENCODING = u'cp1252' +TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), + TestFieldDesc(u'Text Percentage Bottom', 3, 2), + TestFieldDesc(u'RecID', 4, 4), + TestFieldDesc(u'Default Background', 9, 1), + TestFieldDesc(u'Words', 12, 250), + TestFieldDesc(u'BK Bitmap', 13, 10), + TestFieldDesc(u'Last Modified', 21, 10)] +TEST_FIELDS = ['A Heart Like Thine\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 32868, 2147483750, + 129, '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' + '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;' + '\\red255\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g��\7\0f\r\0\0\1\0', '\0\0\0\0\0\0\0\0\0\0', 0] +#22 + +class TestEasyWorshipSongImport(TestCase): + """ + Test the functions in the :mod:`ewimport` module. + """ + def create_field_desc_entry_test(self): + """ + Test creating an instance of the :class`FieldDescEntry` class. + """ + # GIVEN: Set arguments + name = u'Title' + type = 1 + size = 50 + + # WHEN: A FieldDescEntry object is created. + field_desc_entry = FieldDescEntry(name, type, size) + + # THEN: + self.assertIsNotNone(field_desc_entry, u'Import should not be none') + self.assertEquals(field_desc_entry.name, name, u'FieldDescEntry.name should be the same as the name argument') + self.assertEquals(field_desc_entry.type, type, u'FieldDescEntry.type should be the same as the type argument') + self.assertEquals(field_desc_entry.size, size, u'FieldDescEntry.size should be the same as the size argument') + + def create_importer_test(self): + """ + Test creating an instance of the EasyWorship file importer + """ + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = EasyWorshipSongImport(mocked_manager) + + # THEN: The importer object should not be None + self.assertIsNotNone(importer, u'Import should not be none') + + def find_field_test(self): + """ + Test finding a field in a given list using the :mod:`findField` + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.fieldDescs = TEST_FIELD_DESCS + + # WHEN: Given a field name that exists + existing_fields = [u'Title', u'Text Percentage Bottom', u'RecID', u'Default Background', u'Words', + u'BK Bitmap', u'Last Modified'] + + # THEN: The item corresponding the index returned should have the same name attribute + for field_name in existing_fields: + self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) + + # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.fieldDescs = TEST_FIELD_DESCS + + # WHEN: Given a field name that does not exist + non_existing_fields = [u'BK Gradient Shading', u'BK Gradient Variant', u'Favorite', u'Copyright'] + + # THEN: The importer object should not be None + for field_name in non_existing_fields: + self.assertRaises(IndexError, importer.findField, field_name) + + def set_record_struct_test(self): + """ + Test the :mod:`setRecordStruct` module + """ + # GIVEN: A mocked out SongImport class, a mocked out struct class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + + # WHEN: Called with a list of field descriptions + + # THEN: setRecordStruct should return None and structStruct should be called with a value representing + # the list of field descriptions + self.assertIsNone(importer.setRecordStruct(TEST_FIELD_DESCS), u'setRecordStruct should return None') + mocked_struct.Struct.assert_called_with('>50sHIB250s10sQ') + + def get_field_test(self): + + + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.encoding = TEST_DATA_ENCODING + + # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 + importer.fields = TEST_FIELDS + importer.fieldDescs = TEST_FIELD_DESCS + field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (5, None), (6, None)] + + # THEN: getField should return None + for field_index, result in field_results: + self.assertEquals(importer.getField(field_index), result, + u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) + + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + mocked + importer = EasyWorshipSongImport(mocked_manager) + importer.encoding = TEST_DATA_ENCODING + + # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 + importer.fields = TEST_FIELDS + importer.fieldDescs = TEST_FIELD_DESCS + field_results = [(4, u'I dunno')] + + # THEN: getField should return None + for field_index, result in field_results: + self.assertEquals(importer.getField(field_index), result) + + # u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) + + +# TODO: Write doImport Tests + +# TODO: Write getField Tests \ No newline at end of file From dcf2cae3a1516f454fa39fb17e01bf7ea58608f0 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Fri, 29 Mar 2013 21:27:43 +0000 Subject: [PATCH 05/16] minor changes --- openlp/plugins/songs/lib/ewimport.py | 4 ++-- tests/functional/openlp_plugins_songs_lib/test_ewimport.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 6947da17b..3c7234203 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -48,9 +48,9 @@ NOTE_REGEX = re.compile(r'\(.*?\)') class FieldDescEntry: - def __init__(self, name, type, size): + def __init__(self, name, field_type, size): self.name = name - self.type = type + self.type = field_type self.size = size diff --git a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py index afe4dd1c9..8fed5d855 100644 --- a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py +++ b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py @@ -15,9 +15,9 @@ from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEn TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources')) class TestFieldDesc: - def __init__(self, name, type, size): + def __init__(self, name, field_type, size): self.name = name - self.type = type + self.type = field_type self.size = size TEST_DATA_ENCODING = u'cp1252' @@ -70,7 +70,7 @@ class TestEasyWorshipSongImport(TestCase): # THEN: The importer object should not be None self.assertIsNotNone(importer, u'Import should not be none') - def find_field_test(self): + def find_field_exists_test(self): """ Test finding a field in a given list using the :mod:`findField` """ From 047f567ed8e730c3c173b699535be1ee94d4a161 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Sat, 6 Apr 2013 11:54:36 +0100 Subject: [PATCH 06/16] added some tests --- openlp/plugins/songs/lib/ewimport.py | 2 +- .../openlp_plugins_songs_lib/test_ewimport.py | 77 +++++++++++++------ 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 7747a7fdf..949955350 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -142,7 +142,7 @@ class EasyWorshipSongImport(SongImport): rec_count = (rec_count + record_size) / record_size block_list.append((cur_block_pos, rec_count)) total_count += rec_count - self.importWizard.progressBar.setMaximum(total_count) + self.import_wizard.progress_bar.setMaximum(total_count) for block in block_list: cur_block_pos, rec_count = block db_file.seek(cur_block_pos + 6) diff --git a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py index 8fed5d855..8b60df2a7 100644 --- a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py +++ b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py @@ -26,13 +26,29 @@ TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), TestFieldDesc(u'RecID', 4, 4), TestFieldDesc(u'Default Background', 9, 1), TestFieldDesc(u'Words', 12, 250), + TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'BK Bitmap', 13, 10), TestFieldDesc(u'Last Modified', 21, 10)] TEST_FIELDS = ['A Heart Like Thine\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 32868, 2147483750, 129, '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;' - '\\red255\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g��\7\0f\r\0\0\1\0', '\0\0\0\0\0\0\0\0\0\0', 0] -#22 + '\\red255\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g��\7\0f\r\0\0\1\0', + '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' + '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;\\red255' + '\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g>�\6\0�\6\0\0\1\0', '\0\0\0\0\0\0\0\0\0\0', 0] +GET_MEMO_FIELD_TEST_RESULTS = [ + (4, u'\2', {u'return': u'\2', + u'read': (1, 3430), + u'seek': (507136, (8, os.SEEK_CUR))}), + (4, u'\3', {u'return': u'', + u'read': (1, ), + u'seek': (507136, )}), + (5, u'\3', {u'return': u'\3', + u'read': (1, 1725), + u'seek': (3220111360L, (41L, os.SEEK_CUR), 3220111408L)}), + (5, u'\4', {u'return': u'', + u'read': (), + u'seek': ()})] class TestEasyWorshipSongImport(TestCase): """ @@ -72,7 +88,7 @@ class TestEasyWorshipSongImport(TestCase): def find_field_exists_test(self): """ - Test finding a field in a given list using the :mod:`findField` + Test finding an existing field in a given list using the :mod:`findField` """ # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): @@ -88,6 +104,10 @@ class TestEasyWorshipSongImport(TestCase): for field_name in existing_fields: self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) + def find_field_non_exists_test(self): + """ + Test finding an non-existing field in a given list using the :mod:`findField` + """ # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): mocked_manager = MagicMock() @@ -116,7 +136,7 @@ class TestEasyWorshipSongImport(TestCase): # THEN: setRecordStruct should return None and structStruct should be called with a value representing # the list of field descriptions self.assertIsNone(importer.setRecordStruct(TEST_FIELD_DESCS), u'setRecordStruct should return None') - mocked_struct.Struct.assert_called_with('>50sHIB250s10sQ') + mocked_struct.Struct.assert_called_with('>50sHIB250s250s10sQ') def get_field_test(self): @@ -130,33 +150,40 @@ class TestEasyWorshipSongImport(TestCase): # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 importer.fields = TEST_FIELDS importer.fieldDescs = TEST_FIELD_DESCS - field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (5, None), (6, None)] + field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (6, None), (7, None)] # THEN: getField should return None for field_index, result in field_results: self.assertEquals(importer.getField(field_index), result, u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) - # GIVEN: A mocked out SongImport class, a mocked out "manager" - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ - patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - mocked - importer = EasyWorshipSongImport(mocked_manager) - importer.encoding = TEST_DATA_ENCODING + def get_memo_field_test(self): + for test_results in GET_MEMO_FIELD_TEST_RESULTS: + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + mocked_memo_file = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.encoding = TEST_DATA_ENCODING + importer.memoFile = mocked_memo_file - # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 - importer.fields = TEST_FIELDS - importer.fieldDescs = TEST_FIELD_DESCS - field_results = [(4, u'I dunno')] + # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 + importer.fields = TEST_FIELDS + importer.fieldDescs = TEST_FIELD_DESCS + field_index = test_results[0] + mocked_memo_file.read.return_value = test_results[1] + get_field_result = test_results[2][u'return'] + get_field_read_calls = test_results[2][u'read'] + get_field_seek_calls = test_results[2][u'seek'] - # THEN: getField should return None - for field_index, result in field_results: - self.assertEquals(importer.getField(field_index), result) + # THEN: getField should return None + self.assertEquals(importer.getField(field_index), get_field_result) + for call in get_field_read_calls: + mocked_memo_file.read.assert_any_call(call) + for call in get_field_seek_calls: + if isinstance(call, (int, long)): + mocked_memo_file.seek.assert_any_call(call) + else: + mocked_memo_file.seek.assert_any_call(call[0], call[1]) - # u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) - - -# TODO: Write doImport Tests - -# TODO: Write getField Tests \ No newline at end of file +# TODO: Write doImport Tests \ No newline at end of file From a307824d8569677d79eb34b227abb8aa6dedf54f Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Wed, 10 Apr 2013 17:24:53 +0100 Subject: [PATCH 07/16] Moved some directories about. Added test files --- openlp/plugins/songs/lib/ewimport.py | 4 +- .../openlp_plugins/songs/test_ewimport.py | 333 ++++++++++++++++++ .../openlp_plugins_songs_lib/test_ewimport.py | 189 ---------- tests/resources/easyworshipsongs/Songs.DB | Bin 0 -> 6144 bytes tests/resources/easyworshipsongs/Songs.MB | Bin 0 -> 12288 bytes 5 files changed, 334 insertions(+), 192 deletions(-) create mode 100644 tests/functional/openlp_plugins/songs/test_ewimport.py delete mode 100644 tests/functional/openlp_plugins_songs_lib/test_ewimport.py create mode 100644 tests/resources/easyworshipsongs/Songs.DB create mode 100644 tests/resources/easyworshipsongs/Songs.MB diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 949955350..4f8d7d970 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -65,9 +65,7 @@ class EasyWorshipSongImport(SongImport): def doImport(self): # Open the DB and MB files if they exist import_source_mb = self.import_source.replace('.DB', '.MB') - if not os.path.isfile(self.import_source): - return - if not os.path.isfile(import_source_mb): + if not (os.path.isfile(self.import_source) or os.path.isfile(import_source_mb)): return db_size = os.path.getsize(self.import_source) if db_size < 0x800: diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py new file mode 100644 index 000000000..644a898db --- /dev/null +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -0,0 +1,333 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +""" +This module contains tests for the EasyWorship 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.ewimport import EasyWorshipSongImport, FieldDescEntry + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) +SONG_TEST_DATA = [{u'title': u'Amazing Grace (Demonstration)', + u'authors': [u'John Newton', u'Edwin Excell', u'John P. Rees'], + u'copyright': u'Public Domain ', + u'ccli_number': 22025, + u'verses': + [(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'), + (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'), + (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'), + (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'), + (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')], + u'topics': [u'Assurance', u'Grace', u'Praise', u'Salvation'], + u'comments': u'\n\n\n', + u'song_book_name': u'Demonstration Songs', + u'song_number': 0, + u'verse_order_list': []}, + {u'title': u'Beautiful Garden Of Prayer (Demonstration)', + u'authors': [u'Eleanor Allen Schroll', u'James H. Fillmore'], + u'copyright': u'Public Domain ', + u'ccli_number': 60252, + u'verses': + [(u'There\'s a garden where Jesus is waiting,\r\nThere\'s a place that is wondrously fair.\r\n' + u'For it glows with the light of His presence,\r\n\'Tis the beautiful garden of prayer.', u'v1'), + (u'There\'s a garden where Jesus is waiting,\r\nAnd I go with my burden and care.\r\n' + u'Just to learn from His lips, words of comfort,\r\nIn the beautiful garden of prayer.', u'v2'), + (u'There\'s a garden where Jesus is waiting,\r\nAnd He bids you to come meet Him there,\r\n' + u'Just to bow and receive a new blessing,\r\nIn the beautiful garden of prayer.', u'v3'), + (u'O the beautiful garden, the garden of prayer,\r\nO the beautiful garden of prayer.\r\n' + u'There my Savior awaits, and He opens the gates\r\nTo the beautiful garden of prayer.', u'c1')], + u'topics': [u'Devotion', u'Prayer'], + u'comments': u'', + u'song_book_name': u'', + u'song_number': 0, + u'verse_order_list': []}] + + +class TestFieldDesc: + def __init__(self, name, field_type, size): + self.name = name + self.type = field_type + self.size = size + +TEST_DATA_ENCODING = u'cp1252' +TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), TestFieldDesc(u'Text Percentage Bottom', 3, 2), + TestFieldDesc(u'RecID', 4, 4), TestFieldDesc(u'Default Background', 9, 1), TestFieldDesc(u'Words', 12, 250), + TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'BK Bitmap', 13, 10), TestFieldDesc(u'Last Modified', 21, 10)] +TEST_FIELDS = ['A Heart Like Thine\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 32868, 2147483750, + 129, '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' + '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;' + '\\red255\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g��\7\0f\r\0\0\1\0', + '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' + '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;\\red255' + '\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g>�\6\0�\6\0\0\1\0', '\0\0\0\0\0\0\0\0\0\0', 0] +GET_MEMO_FIELD_TEST_RESULTS = [ + (4, u'\2', {u'return': u'\2',u'read': (1, 3430), u'seek': (507136, (8, os.SEEK_CUR))}), + (4, u'\3', {u'return': u'', u'read': (1, ), u'seek': (507136, )}), + (5, u'\3', {u'return': u'\3', u'read': (1, 1725), u'seek': (3220111360L, (41L, os.SEEK_CUR), 3220111408L)}), + (5, u'\4', {u'return': u'', u'read': (), u'seek': ()})] + +class TestEasyWorshipSongImport(TestCase): + """ + Test the functions in the :mod:`ewimport` module. + """ + def create_field_desc_entry_test(self): + """ + Test creating an instance of the :class`FieldDescEntry` class. + """ + # GIVEN: Set arguments + name = u'Title' + field_type = 1 + size = 50 + + # WHEN: A FieldDescEntry object is created. + field_desc_entry = FieldDescEntry(name, field_type, size) + + # THEN: + self.assertIsNotNone(field_desc_entry, u'Import should not be none') + self.assertEquals(field_desc_entry.name, name, u'FieldDescEntry.name should be the same as the name argument') + self.assertEquals(field_desc_entry.type, field_type, + u'FieldDescEntry.type should be the same as the typeargument') + self.assertEquals(field_desc_entry.size, size, u'FieldDescEntry.size should be the same as the size argument') + + def create_importer_test(self): + """ + Test creating an instance of the EasyWorship file importer + """ + # GIVEN: A mocked out SongImport class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = EasyWorshipSongImport(mocked_manager) + + # THEN: The importer object should not be None + self.assertIsNotNone(importer, u'Import should not be none') + + def find_field_exists_test(self): + """ + Test finding an existing field in a given list using the :mod:`findField` + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.fieldDescs = TEST_FIELD_DESCS + + # WHEN: Given a field name that exists + existing_fields = [u'Title', u'Text Percentage Bottom', u'RecID', u'Default Background', u'Words', + u'BK Bitmap', u'Last Modified'] + + # THEN: The item corresponding the index returned should have the same name attribute + for field_name in existing_fields: + self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) + + def find_field_non_exists_test(self): + """ + Test finding an non-existing field in a given list using the :mod:`findField` + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.fieldDescs = TEST_FIELD_DESCS + + # WHEN: Given a field name that does not exist + non_existing_fields = [u'BK Gradient Shading', u'BK Gradient Variant', u'Favorite', u'Copyright'] + + # THEN: The importer object should not be None + for field_name in non_existing_fields: + self.assertRaises(IndexError, importer.findField, field_name) + + def set_record_struct_test(self): + """ + Test the :mod:`setRecordStruct` module + """ + # GIVEN: A mocked out SongImport class, a mocked out struct class, and a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + + # WHEN: Called with a list of field descriptions + + # THEN: setRecordStruct should return None and structStruct should be called with a value representing + # the list of field descriptions + self.assertIsNone(importer.setRecordStruct(TEST_FIELD_DESCS), u'setRecordStruct should return None') + mocked_struct.Struct.assert_called_with('>50sHIB250s250s10sQ') + + def get_field_test(self): + """ + Test the :mod:`getField` module + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" and an encoding + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.encoding = TEST_DATA_ENCODING + + # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 + importer.fields = TEST_FIELDS + importer.fieldDescs = TEST_FIELD_DESCS + field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (6, None), (7, None)] + + # THEN: getField should return None + for field_index, result in field_results: + self.assertEquals(importer.getField(field_index), result, + u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) + + def get_memo_field_test(self): + """ + Test the :mod:`getField` module + """ + for test_results in GET_MEMO_FIELD_TEST_RESULTS: + # GIVEN: A mocked out SongImport class, a mocked out "manager", a mocked out memo_file and an encoding + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): + mocked_manager = MagicMock() + mocked_memo_file = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + importer.memoFile = mocked_memo_file + importer.encoding = TEST_DATA_ENCODING + + # WHEN: Supplied with test fields and test field descriptions + importer.fields = TEST_FIELDS + importer.fieldDescs = TEST_FIELD_DESCS + field_index = test_results[0] + mocked_memo_file.read.return_value = test_results[1] + get_field_result = test_results[2][u'return'] + get_field_read_calls = test_results[2][u'read'] + get_field_seek_calls = test_results[2][u'seek'] + + # THEN: getField should return the appropriate value with the appropriate mocked objects being called + self.assertEquals(importer.getField(field_index), get_field_result) + for call in get_field_read_calls: + mocked_memo_file.read.assert_any_call(call) + for call in get_field_seek_calls: + if isinstance(call, (int, long)): + mocked_memo_file.seek.assert_any_call(call) + else: + mocked_memo_file.seek.assert_any_call(call[0], call[1]) + + def do_import_source_test(self): + """ + Test the :mod:`doImport` module + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + mocked_os_path.isfile.return_value = False + + # WHEN: Supplied with an import source + importer.import_source = u'Songs.DB' + + # THEN: doImport should return None having called os.path.isfile + self.assertIsNone(importer.doImport(), u'doImport should return None') + mocked_os_path.isfile.assert_any_call(u'Songs.DB') + mocked_os_path.isfile.assert_any_call(u'Songs.MB') + + def do_import_source_validity_test(self): + """ + Test the :mod:`doImport` module + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path, \ + patch(u'__builtin__.open') as mocked_open: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + mocked_os_path.isfile.return_value = True + importer.import_source = u'Songs.DB' + + # WHEN: DB file size is less than 0x800 + mocked_os_path.getsize.return_value = 0x7FF + + # THEN: doImport should return None having called os.path.isfile + self.assertIsNone(importer.doImport(), u'doImport should return None when db_size is less than 0x800') + mocked_os_path.getsize.assert_any_call(u'Songs.DB') + + + 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(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 = EasyWorshipSongImport(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: Importing each file + importer.import_source = [os.path.join(TEST_PATH, u'Songs.DB')] + title = SONG_TEST_DATA[song_file][u'title'] + author_calls = SONG_TEST_DATA[song_file][u'authors'] + song_copyright = SONG_TEST_DATA[song_file][u'copyright'] + ccli_number = SONG_TEST_DATA[song_file][u'ccli_number'] + add_verse_calls = SONG_TEST_DATA[song_file][u'verses'] + topics = SONG_TEST_DATA[song_file][u'topics'] + comments = SONG_TEST_DATA[song_file][u'comments'] + song_book_name = SONG_TEST_DATA[song_file][u'song_book_name'] + song_number = SONG_TEST_DATA[song_file][u'song_number'] + verse_order_list = SONG_TEST_DATA[song_file][u'verse_order_list'] + + # THEN: doImport should return none, the song data should be as expected, and finish should have been + # called. + self.assertIsNone(importer.doImport(), u'doImport should return None when it has completed') + self.assertEquals(importer.title, title, u'title for %s should be "%s"' % (song_file, title)) + for author in author_calls: + mocked_parse_author.assert_any_call(author) + if song_copyright: + mocked_add_copyright.assert_called_with(song_copyright) + if ccli_number: + self.assertEquals(importer.ccliNumber, ccli_number, u'ccliNumber for %s should be %s' + % (song_file, ccli_number)) + for verse_text, verse_tag in add_verse_calls: + mocked_add_verse.assert_any_call(verse_text, verse_tag) + if topics: + self.assertEquals(importer.topics, topics, u'topics for %s should be %s' % (song_file, topics)) + if comments: + self.assertEquals(importer.comments, comments, u'comments for %s should be "%s"' + % (song_file, comments)) + if song_book_name: + self.assertEquals(importer.songBookName, song_book_name, u'songBookName for %s should be "%s"' + % (song_file, song_book_name)) + if song_number: + self.assertEquals(importer.songNumber, song_number, u'songNumber for %s should be %s' + % (song_file, song_number)) + if verse_order_list: + self.assertEquals(importer.verseOrderList, [], u'verseOrderList for %s should be %s' + % (song_file, verse_order_list)) + mocked_finish.assert_called_with() + + # Open the DB and MB files if they exist + # import_source_mb = self.import_source.replace('.DB', '.MB') + # if not (os.path.isfile(self.import_source) or os.path.isfile(import_source_mb)): + # return + # db_size = os.path.getsize(self.import_source) + # if db_size < 0x800: + # return + # TODO: Write doImport Tests \ No newline at end of file diff --git a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py b/tests/functional/openlp_plugins_songs_lib/test_ewimport.py deleted file mode 100644 index 8b60df2a7..000000000 --- a/tests/functional/openlp_plugins_songs_lib/test_ewimport.py +++ /dev/null @@ -1,189 +0,0 @@ -# -*- coding: utf-8 -*- -# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 - -""" -This module contains tests for the EasyWorship 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.ewimport import EasyWorshipSongImport, FieldDescEntry - -TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources')) - -class TestFieldDesc: - def __init__(self, name, field_type, size): - self.name = name - self.type = field_type - self.size = size - -TEST_DATA_ENCODING = u'cp1252' -TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), - TestFieldDesc(u'Text Percentage Bottom', 3, 2), - TestFieldDesc(u'RecID', 4, 4), - TestFieldDesc(u'Default Background', 9, 1), - TestFieldDesc(u'Words', 12, 250), - TestFieldDesc(u'Words', 12, 250), - TestFieldDesc(u'BK Bitmap', 13, 10), - TestFieldDesc(u'Last Modified', 21, 10)] -TEST_FIELDS = ['A Heart Like Thine\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 32868, 2147483750, - 129, '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' - '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;' - '\\red255\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g��\7\0f\r\0\0\1\0', - '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}' - '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;\\red255' - '\\green255\\blue0;\\red255\\green0\\blue255;\\red128\\g>�\6\0�\6\0\0\1\0', '\0\0\0\0\0\0\0\0\0\0', 0] -GET_MEMO_FIELD_TEST_RESULTS = [ - (4, u'\2', {u'return': u'\2', - u'read': (1, 3430), - u'seek': (507136, (8, os.SEEK_CUR))}), - (4, u'\3', {u'return': u'', - u'read': (1, ), - u'seek': (507136, )}), - (5, u'\3', {u'return': u'\3', - u'read': (1, 1725), - u'seek': (3220111360L, (41L, os.SEEK_CUR), 3220111408L)}), - (5, u'\4', {u'return': u'', - u'read': (), - u'seek': ()})] - -class TestEasyWorshipSongImport(TestCase): - """ - Test the functions in the :mod:`ewimport` module. - """ - def create_field_desc_entry_test(self): - """ - Test creating an instance of the :class`FieldDescEntry` class. - """ - # GIVEN: Set arguments - name = u'Title' - type = 1 - size = 50 - - # WHEN: A FieldDescEntry object is created. - field_desc_entry = FieldDescEntry(name, type, size) - - # THEN: - self.assertIsNotNone(field_desc_entry, u'Import should not be none') - self.assertEquals(field_desc_entry.name, name, u'FieldDescEntry.name should be the same as the name argument') - self.assertEquals(field_desc_entry.type, type, u'FieldDescEntry.type should be the same as the type argument') - self.assertEquals(field_desc_entry.size, size, u'FieldDescEntry.size should be the same as the size argument') - - def create_importer_test(self): - """ - Test creating an instance of the EasyWorship file importer - """ - # GIVEN: A mocked out SongImport class, and a mocked out "manager" - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - - # WHEN: An importer object is created - importer = EasyWorshipSongImport(mocked_manager) - - # THEN: The importer object should not be None - self.assertIsNotNone(importer, u'Import should not be none') - - def find_field_exists_test(self): - """ - Test finding an existing field in a given list using the :mod:`findField` - """ - # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - importer = EasyWorshipSongImport(mocked_manager) - importer.fieldDescs = TEST_FIELD_DESCS - - # WHEN: Given a field name that exists - existing_fields = [u'Title', u'Text Percentage Bottom', u'RecID', u'Default Background', u'Words', - u'BK Bitmap', u'Last Modified'] - - # THEN: The item corresponding the index returned should have the same name attribute - for field_name in existing_fields: - self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) - - def find_field_non_exists_test(self): - """ - Test finding an non-existing field in a given list using the :mod:`findField` - """ - # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - importer = EasyWorshipSongImport(mocked_manager) - importer.fieldDescs = TEST_FIELD_DESCS - - # WHEN: Given a field name that does not exist - non_existing_fields = [u'BK Gradient Shading', u'BK Gradient Variant', u'Favorite', u'Copyright'] - - # THEN: The importer object should not be None - for field_name in non_existing_fields: - self.assertRaises(IndexError, importer.findField, field_name) - - def set_record_struct_test(self): - """ - Test the :mod:`setRecordStruct` module - """ - # GIVEN: A mocked out SongImport class, a mocked out struct class, and a mocked out "manager" - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ - patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct: - mocked_manager = MagicMock() - importer = EasyWorshipSongImport(mocked_manager) - - # WHEN: Called with a list of field descriptions - - # THEN: setRecordStruct should return None and structStruct should be called with a value representing - # the list of field descriptions - self.assertIsNone(importer.setRecordStruct(TEST_FIELD_DESCS), u'setRecordStruct should return None') - mocked_struct.Struct.assert_called_with('>50sHIB250s250s10sQ') - - def get_field_test(self): - - - # GIVEN: A mocked out SongImport class, a mocked out "manager" - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - importer = EasyWorshipSongImport(mocked_manager) - importer.encoding = TEST_DATA_ENCODING - - # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 - importer.fields = TEST_FIELDS - importer.fieldDescs = TEST_FIELD_DESCS - field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (6, None), (7, None)] - - # THEN: getField should return None - for field_index, result in field_results: - self.assertEquals(importer.getField(field_index), result, - u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) - - def get_memo_field_test(self): - for test_results in GET_MEMO_FIELD_TEST_RESULTS: - # GIVEN: A mocked out SongImport class, a mocked out "manager" - with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): - mocked_manager = MagicMock() - mocked_memo_file = MagicMock() - importer = EasyWorshipSongImport(mocked_manager) - importer.encoding = TEST_DATA_ENCODING - importer.memoFile = mocked_memo_file - - # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 - importer.fields = TEST_FIELDS - importer.fieldDescs = TEST_FIELD_DESCS - field_index = test_results[0] - mocked_memo_file.read.return_value = test_results[1] - get_field_result = test_results[2][u'return'] - get_field_read_calls = test_results[2][u'read'] - get_field_seek_calls = test_results[2][u'seek'] - - # THEN: getField should return None - self.assertEquals(importer.getField(field_index), get_field_result) - for call in get_field_read_calls: - mocked_memo_file.read.assert_any_call(call) - for call in get_field_seek_calls: - if isinstance(call, (int, long)): - mocked_memo_file.seek.assert_any_call(call) - else: - mocked_memo_file.seek.assert_any_call(call[0], call[1]) - -# TODO: Write doImport Tests \ No newline at end of file diff --git a/tests/resources/easyworshipsongs/Songs.DB b/tests/resources/easyworshipsongs/Songs.DB new file mode 100644 index 0000000000000000000000000000000000000000..8c9679b86a8bacecdd559011139959ccbd0c155a GIT binary patch literal 6144 zcmeH~O>7%Q6vyAj8>sU^%SWJmX39q?6e7nE2`Qn)Zkm1&l8}UOv5>~Qv-ZflJJx#K zkd_Ju39gk87bFlj=%FVN;);X>Ck~w90QiK&3Bdt`Wd5^uVml!y5|>KZ8U1$Oy!U2y z_U-fAwPVvEvImIB>huxFGr#^^nYq@iKzy2DY@wr_x=ugLG%Ifb8nK`lKWx-}?ae_9 z8O*T*{Wj&kJVD>sM4wxhb!^aF$uui#WoXm9}hqi?(n3XzZrj_IuAaEAO9bRz3nBgB##0@GbZr`~-dne}lo(&B{@5 z5}XDr;586{b?^?j20jFzfX~2};5+aO_!A7j(5#Gs6W|$e9=rl7U<14j-UT0k>);FU zHTV(y0n)iQuCB`}qGjP+n59+mx@JovazvrJ4OC9hkTi`*@(D>wPRpIDmuBD|71GR1MRnlTz`!W7+I ztC)4S{o_Gi`EDFQx}do)owgibKj+I*7~m<Pkd)vJhlQZ+e)4LLB zRaJNCmg9_C6QQdjhUBwQ{ZeGpAl*W@qEe=5h=yr|M(H*>Oh@Q;x`XbdyJ(E=rhDip z-Anh;{qz7mNDt9C%HT12m>!`==`lJ^kJAZyf}W(4^c1QpuQ^g4#GiO_&o^5H96J~A z%P4H79y(%Q+q zxWe-?=BRlwJN4|9f`{6wi#~)*!3(8d@SHM_YC>mN4z=H(z6#UiP7`XC%R@eW6;{W* zZ?Hrm5^knYii8L=g`!^1LfhSKab93zk|(h&~-Q z-+Y^REJ@ot0*!4`SK3w~sW%($X3zK_ikppAs}<)_d}s`Gda98^bmERo%L`uD(yROI z9FJTPvPF+AMSKIFHhZ+QzThEV!#UrF`-)SJl<%{P_;{(Y^Al`N`hKABxz%kAqBoKf zbCZ-9MsytiC6f}<&Ch?AkhPuGm9{iECnbjPmfBB==?PjQCJ{&?kVGJfKoWr@0{;<# Fe*n9TVl@B& literal 0 HcmV?d00001 diff --git a/tests/resources/easyworshipsongs/Songs.MB b/tests/resources/easyworshipsongs/Songs.MB new file mode 100644 index 0000000000000000000000000000000000000000..b46323005db269bea5b7531b572cd2244009c19f GIT binary patch literal 12288 zcmeHM!EWO=6jX95wq2l~@J>y)2m;$px>@AXv_Kp0rH2O1MHfnvf1WO=DkU>m+U2f+x_H4l6>jEzfY3Sk_}&P_BTw% zsmMTNATkgchzvvqA_I|u$UtNuG7uSv3`7P#1qSx_lDD5H@BnU79|Q`*Dx+pQy{bJ>W_N__YUFo1O=@Sne+F z+T;>4ENDbhb=Dg_QBybNA8!%L=?4Jd6S z6RJOs?8qNiPO%AV9pN~#oUnJ9dH!q{fb$9aOQ?z}!*Wiw?#OnWJ90HAt6NnZ?v8y~ zGe?$P^MV+4hP%8bDSqcP;b*&}j!7$o+A&K~TX#qqcu)wOO{nw{?+$$h%uFKvZ$Zao zIX{8`TM6EJ!~aT%V`Nu=%LC8FTtMe3cdV^yN0TYl948w&O(g~aN}j7?u>e3lmL;cLYG1VZ9(>8?Uv)m%}>4?uQ?y3n7e28{G0YH}69L|ddJn*vmm_4q&Q;=m$ZnXr8Kpz$XGwA^yD%+0D* zM<}djpF(>PV~h+$20mg2(Eoq+MeP5*2NC=K*#Dz7-EM5$@7iPkziI((?AmYLqz~2q z|5$5-B0InU_@yr!GY&UFl&z!+P6SuzInY4O$D~VN_f2_72f2BfTj-;Zn$nlnXiR@) z*Fqa&9TN^+ldrKaIO&F~d2L)V#{&xPOTCe*m1lT-n;<~wOW%Q@gl9`xQHF!J*W6+Q zp^x*dq0kTL9&T5FT?LD#Z|6O-Rs~u^E8!h5VFIN73Gxb91X6c^6|U|Tk$oeCU3!7V zU?^A)gDBuR`nJ~Z|MjeRQmI26p+}e68$x5M^jrkwUe@|!qtCe<9z}r0uUoh^=sMBs z8%3qjlU`RT8sv1RO<}zZa4t&x^Cdv#ao?1GSdD|6qQ_2WuQATgOn~vJZoMYnFM|I9 zfA~U7eD0Z$JX Date: Mon, 15 Apr 2013 21:30:32 +0100 Subject: [PATCH 08/16] some more tests --- .../openlp_plugins/songs/test_ewimport.py | 193 +++++++++--------- 1 file changed, 102 insertions(+), 91 deletions(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index 644a898db..2155d63e9 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -7,49 +7,55 @@ This module contains tests for the EasyWorship song importer. import os from unittest import TestCase -from mock import call, patch, MagicMock +from mock import patch, MagicMock -from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry +class EasyWorshipSongImportLogger(EasyWorshipSongImport): + """ + This class logs changes in the title instance variable + """ + _title_assignment_list = [] + + def __init__(self, manager): + EasyWorshipSongImport.__init__(self, manager) + + @property + def title(self): + return self._title_assignment_list[-1] + + @title.setter + def title(self, title): + self._title_assignment_list.append(title) + TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) -SONG_TEST_DATA = [{u'title': u'Amazing Grace (Demonstration)', - u'authors': [u'John Newton', u'Edwin Excell', u'John P. Rees'], - u'copyright': u'Public Domain ', - u'ccli_number': 22025, +SONG_TEST_DATA = [{u'title': u'Amazing Grace', + u'authors': [u'John Newton'], + u'copyright': u'Public Domain', + u'ccli_number': 0, u'verses': - [(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'), - (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'), - (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'), - (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'), - (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')], - u'topics': [u'Assurance', u'Grace', u'Praise', u'Salvation'], - u'comments': u'\n\n\n', - u'song_book_name': u'Demonstration Songs', - u'song_number': 0, + [(u'Amazing grace how sweet the sound,\nThat saved a wretch like me;\n' + u'I once was lost, but now am found\nWas blind, but now I see.', u'v1'), + (u'T\'was grace that taught my heart to fear,\nAnd grace my fears relieved;\n' + u'How precious did that grace appear\nThe hour I first believed.', u'v2'), + (u'Through many dangers, toil and snares,\nI have already come;\n' + u'\'Tis grace has brought me safe thus far,\nAnd grace will lead me home.', u'v3'), + (u'When we\'ve been there ten thousand years\nBright shining as the sun,\n' + u'We\'ve no less days to sing God\'s praise\nThan when we\'ve first begun.', u'v4')], u'verse_order_list': []}, - {u'title': u'Beautiful Garden Of Prayer (Demonstration)', - u'authors': [u'Eleanor Allen Schroll', u'James H. Fillmore'], - u'copyright': u'Public Domain ', - u'ccli_number': 60252, + {u'title': u'Beautiful Garden Of Prayer', + u'authors': [u'Eleanor Allen Schroll James H. Fillmore'], + u'copyright': u'Public Domain', + u'ccli_number': 0, u'verses': - [(u'There\'s a garden where Jesus is waiting,\r\nThere\'s a place that is wondrously fair.\r\n' - u'For it glows with the light of His presence,\r\n\'Tis the beautiful garden of prayer.', u'v1'), - (u'There\'s a garden where Jesus is waiting,\r\nAnd I go with my burden and care.\r\n' - u'Just to learn from His lips, words of comfort,\r\nIn the beautiful garden of prayer.', u'v2'), - (u'There\'s a garden where Jesus is waiting,\r\nAnd He bids you to come meet Him there,\r\n' - u'Just to bow and receive a new blessing,\r\nIn the beautiful garden of prayer.', u'v3'), - (u'O the beautiful garden, the garden of prayer,\r\nO the beautiful garden of prayer.\r\n' - u'There my Savior awaits, and He opens the gates\r\nTo the beautiful garden of prayer.', u'c1')], - u'topics': [u'Devotion', u'Prayer'], - u'comments': u'', - u'song_book_name': u'', - u'song_number': 0, + [(u'O the beautiful garden, the garden of prayer,\nO the beautiful garden of prayer.\n' + u'There my Savior awaits, and He opens the gates\nTo the beautiful garden of prayer.', u'c1'), + (u'There\'s a garden where Jesus is waiting,\nThere\'s a place that is wondrously fair.\n' + u'For it glows with the light of His presence,\n\'Tis the beautiful garden of prayer.', u'v1'), + (u'There\'s a garden where Jesus is waiting,\nAnd I go with my burden and care.\n' + u'Just to learn from His lips, words of comfort,\nIn the beautiful garden of prayer.', u'v2'), + (u'There\'s a garden where Jesus is waiting,\nAnd He bids you to come meet Him there,\n' + u'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', u'v3')], u'verse_order_list': []}] @@ -236,14 +242,13 @@ class TestEasyWorshipSongImport(TestCase): mocked_os_path.isfile.assert_any_call(u'Songs.DB') mocked_os_path.isfile.assert_any_call(u'Songs.MB') - def do_import_source_validity_test(self): + def do_import_database_validity_test(self): """ - Test the :mod:`doImport` module + Test the :mod:`doImport` module handles invalid database files correctly """ # GIVEN: A mocked out SongImport class, a mocked out "manager" with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ - patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path, \ - patch(u'__builtin__.open') as mocked_open: + patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path: mocked_manager = MagicMock() importer = EasyWorshipSongImport(mocked_manager) mocked_os_path.isfile.return_value = True @@ -256,6 +261,32 @@ class TestEasyWorshipSongImport(TestCase): self.assertIsNone(importer.doImport(), u'doImport should return None when db_size is less than 0x800') mocked_os_path.getsize.assert_any_call(u'Songs.DB') + def do_import_memo_validty_test(self): + """ + Test the :mod:`doImport` module handles invalid memo files correctly + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path, \ + patch(u'__builtin__.open') as mocked_open, \ + patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + mocked_os_path.isfile.return_value = True + mocked_os_path.getsize.return_value = 0x800 + importer.import_source = u'Songs.DB' + + # WHEN: Unpacking first 35 bytes of Memo file + struct_unpack_return_values = [(0, 0x700, 2, 0, 0), (0, 0x800, 0, 0, 0), (0, 0x800, 5, 0, 0)] + mocked_struct.unpack.side_effect = struct_unpack_return_values + + # THEN: doImport should return None having called closed the open files db and memo files. + for effect in struct_unpack_return_values: + self.assertIsNone(importer.doImport(), u'doImport should return None when db_size is less than 0x800') + self.assertEqual(mocked_open().close.call_count, 2, + u'The open db and memo files should have been closed') + mocked_open().close.reset_mock() + self.assertIs(mocked_open().seek.called, False, u'db_file.seek should not have been called.') def file_import_test(self): """ @@ -264,70 +295,50 @@ class TestEasyWorshipSongImport(TestCase): # 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(u'openlp.plugins.songs.lib.songshowplusimport.SongImport'): + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.retrieve_windows_encoding') as mocked_retrieve_windows_encoding: + mocked_retrieve_windows_encoding.return_value = u'cp1252' mocked_manager = MagicMock() mocked_import_wizard = MagicMock() - mocked_parse_author = MagicMock() - mocked_add_copyright = MagicMock() + mocked_add_author = MagicMock() mocked_add_verse = MagicMock() mocked_finish = MagicMock() + mocked_title = MagicMock() mocked_finish.return_value = True - importer = EasyWorshipSongImport(mocked_manager) + importer = EasyWorshipSongImportLogger(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.addAuthor = mocked_add_author importer.addVerse = mocked_add_verse + importer.title = mocked_title importer.finish = mocked_finish importer.topics = [] # WHEN: Importing each file - importer.import_source = [os.path.join(TEST_PATH, u'Songs.DB')] - title = SONG_TEST_DATA[song_file][u'title'] - author_calls = SONG_TEST_DATA[song_file][u'authors'] - song_copyright = SONG_TEST_DATA[song_file][u'copyright'] - ccli_number = SONG_TEST_DATA[song_file][u'ccli_number'] - add_verse_calls = SONG_TEST_DATA[song_file][u'verses'] - topics = SONG_TEST_DATA[song_file][u'topics'] - comments = SONG_TEST_DATA[song_file][u'comments'] - song_book_name = SONG_TEST_DATA[song_file][u'song_book_name'] - song_number = SONG_TEST_DATA[song_file][u'song_number'] - verse_order_list = SONG_TEST_DATA[song_file][u'verse_order_list'] + importer.import_source = os.path.join(TEST_PATH, u'Songs.DB') # THEN: doImport should return none, the song data should be as expected, and finish should have been # called. self.assertIsNone(importer.doImport(), u'doImport should return None when it has completed') - self.assertEquals(importer.title, title, u'title for %s should be "%s"' % (song_file, title)) - for author in author_calls: - mocked_parse_author.assert_any_call(author) - if song_copyright: - mocked_add_copyright.assert_called_with(song_copyright) - if ccli_number: - self.assertEquals(importer.ccliNumber, ccli_number, u'ccliNumber for %s should be %s' - % (song_file, ccli_number)) - for verse_text, verse_tag in add_verse_calls: - mocked_add_verse.assert_any_call(verse_text, verse_tag) - if topics: - self.assertEquals(importer.topics, topics, u'topics for %s should be %s' % (song_file, topics)) - if comments: - self.assertEquals(importer.comments, comments, u'comments for %s should be "%s"' - % (song_file, comments)) - if song_book_name: - self.assertEquals(importer.songBookName, song_book_name, u'songBookName for %s should be "%s"' - % (song_file, song_book_name)) - if song_number: - self.assertEquals(importer.songNumber, song_number, u'songNumber for %s should be %s' - % (song_file, song_number)) - if verse_order_list: - self.assertEquals(importer.verseOrderList, [], u'verseOrderList for %s should be %s' - % (song_file, verse_order_list)) - mocked_finish.assert_called_with() - - # Open the DB and MB files if they exist - # import_source_mb = self.import_source.replace('.DB', '.MB') - # if not (os.path.isfile(self.import_source) or os.path.isfile(import_source_mb)): - # return - # db_size = os.path.getsize(self.import_source) - # if db_size < 0x800: - # return - # TODO: Write doImport Tests \ No newline at end of file + for song_data in SONG_TEST_DATA: + print mocked_title.mocked_calls() + title = song_data[u'title'] + author_calls = song_data[u'authors'] + song_copyright = song_data[u'copyright'] + ccli_number = song_data[u'ccli_number'] + add_verse_calls = song_data[u'verses'] + verse_order_list = song_data[u'verse_order_list'] + self.assertIn(title, importer._title_assignment_list, u'title for %s should be "%s"' % (title, title)) + for author in author_calls: + mocked_add_author.assert_any_call(author) + if song_copyright: + self.assertEqual(importer.copyright, song_copyright) + if ccli_number: + self.assertEquals(importer.ccliNumber, ccli_number, u'ccliNumber for %s should be %s' + % (title, ccli_number)) + for verse_text, verse_tag in add_verse_calls: + mocked_add_verse.assert_any_call(verse_text, verse_tag) + if verse_order_list: + self.assertEquals(importer.verseOrderList, verse_order_list, u'verseOrderList for %s should be %s' + % (title, verse_order_list)) + mocked_finish.assert_called_with() From cc1d3b3c5b3cf0353eead2fc09e8c8f5136553e1 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Tue, 16 Apr 2013 18:06:42 +0100 Subject: [PATCH 09/16] finished tests --- .../openlp_plugins/songs/test_ewimport.py | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index 2155d63e9..ab72230ff 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -28,6 +28,12 @@ class EasyWorshipSongImportLogger(EasyWorshipSongImport): def title(self, title): self._title_assignment_list.append(title) +class TestFieldDesc: + def __init__(self, name, field_type, size): + self.name = name + self.type = field_type + self.size = size + TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) SONG_TEST_DATA = [{u'title': u'Amazing Grace', u'authors': [u'John Newton'], @@ -57,15 +63,9 @@ SONG_TEST_DATA = [{u'title': u'Amazing Grace', (u'There\'s a garden where Jesus is waiting,\nAnd He bids you to come meet Him there,\n' u'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', u'v3')], u'verse_order_list': []}] - - -class TestFieldDesc: - def __init__(self, name, field_type, size): - self.name = name - self.type = field_type - self.size = size - TEST_DATA_ENCODING = u'cp1252' +CODE_PAGE_MAPPINGS = [(852, u'cp1250'), (737, u'cp1253'), (775, u'cp1257'), (855, u'cp1251'), (857, u'cp1254'), + (866, u'cp1251'), (869, u'cp1253'), (862, u'cp1255'), (874, u'cp874')] TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), TestFieldDesc(u'Text Percentage Bottom', 3, 2), TestFieldDesc(u'RecID', 4, 4), TestFieldDesc(u'Default Background', 9, 1), TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'BK Bitmap', 13, 10), TestFieldDesc(u'Last Modified', 21, 10)] @@ -137,7 +137,7 @@ class TestEasyWorshipSongImport(TestCase): for field_name in existing_fields: self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) - def find_field_non_exists_test(self): + def find_non_existing_field_test(self): """ Test finding an non-existing field in a given list using the :mod:`findField` """ @@ -181,12 +181,12 @@ class TestEasyWorshipSongImport(TestCase): importer = EasyWorshipSongImport(mocked_manager) importer.encoding = TEST_DATA_ENCODING - # WHEN: Supplied with string with just NULL bytes, or an int with the value 0 + # WHEN: Supplied with some test data and known results importer.fields = TEST_FIELDS importer.fieldDescs = TEST_FIELD_DESCS field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (6, None), (7, None)] - # THEN: getField should return None + # THEN: getField should return the known results for field_index, result in field_results: self.assertEquals(importer.getField(field_index), result, u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) @@ -225,7 +225,7 @@ class TestEasyWorshipSongImport(TestCase): def do_import_source_test(self): """ - Test the :mod:`doImport` module + Test the :mod:`doImport` module opens the correct files """ # GIVEN: A mocked out SongImport class, a mocked out "manager" with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ @@ -246,7 +246,7 @@ class TestEasyWorshipSongImport(TestCase): """ Test the :mod:`doImport` module handles invalid database files correctly """ - # GIVEN: A mocked out SongImport class, a mocked out "manager" + # GIVEN: A mocked out SongImport class, os.path and a mocked out "manager" with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path: mocked_manager = MagicMock() @@ -288,6 +288,31 @@ class TestEasyWorshipSongImport(TestCase): mocked_open().close.reset_mock() self.assertIs(mocked_open().seek.called, False, u'db_file.seek should not have been called.') + def code_page_to_encoding_test(self): + """ + Test the :mod:`doImport` converts the code page to the encoding correctly + """ + # GIVEN: A mocked out SongImport class, a mocked out "manager" + with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ + patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path, \ + patch(u'__builtin__.open'), patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct, \ + patch(u'openlp.plugins.songs.lib.ewimport.retrieve_windows_encoding') as mocked_retrieve_windows_encoding: + mocked_manager = MagicMock() + importer = EasyWorshipSongImport(mocked_manager) + mocked_os_path.isfile.return_value = True + mocked_os_path.getsize.return_value = 0x800 + importer.import_source = u'Songs.DB' + + # WHEN: Unpacking the code page + for code_page, encoding in CODE_PAGE_MAPPINGS: + struct_unpack_return_values = [(0, 0x800, 2, 0, 0), (code_page, )] + mocked_struct.unpack.side_effect = struct_unpack_return_values + mocked_retrieve_windows_encoding.return_value = False + + # THEN: doImport should return None having called retrieve_windows_encoding with the correct encoding. + self.assertIsNone(importer.doImport(), u'doImport should return None when db_size is less than 0x800') + mocked_retrieve_windows_encoding.assert_call(encoding) + def file_import_test(self): """ Test the actual import of real song files and check that the imported data is correct. From 09955c0ca2814e941eff285d220739232c3ae949 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Wed, 17 Apr 2013 22:10:19 +0100 Subject: [PATCH 10/16] Moved constants. --- .../openlp_plugins/songs/test_ewimport.py | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index ab72230ff..3c91e3d7a 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -11,29 +11,6 @@ from mock import patch, MagicMock from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry -class EasyWorshipSongImportLogger(EasyWorshipSongImport): - """ - This class logs changes in the title instance variable - """ - _title_assignment_list = [] - - def __init__(self, manager): - EasyWorshipSongImport.__init__(self, manager) - - @property - def title(self): - return self._title_assignment_list[-1] - - @title.setter - def title(self, title): - self._title_assignment_list.append(title) - -class TestFieldDesc: - def __init__(self, name, field_type, size): - self.name = name - self.type = field_type - self.size = size - TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) SONG_TEST_DATA = [{u'title': u'Amazing Grace', u'authors': [u'John Newton'], @@ -63,6 +40,30 @@ SONG_TEST_DATA = [{u'title': u'Amazing Grace', (u'There\'s a garden where Jesus is waiting,\nAnd He bids you to come meet Him there,\n' u'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', u'v3')], u'verse_order_list': []}] + +class EasyWorshipSongImportLogger(EasyWorshipSongImport): + """ + This class logs changes in the title instance variable + """ + _title_assignment_list = [] + + def __init__(self, manager): + EasyWorshipSongImport.__init__(self, manager) + + @property + def title(self): + return self._title_assignment_list[-1] + + @title.setter + def title(self, title): + self._title_assignment_list.append(title) + +class TestFieldDesc: + def __init__(self, name, field_type, size): + self.name = name + self.type = field_type + self.size = size + TEST_DATA_ENCODING = u'cp1252' CODE_PAGE_MAPPINGS = [(852, u'cp1250'), (737, u'cp1253'), (775, u'cp1257'), (855, u'cp1251'), (857, u'cp1254'), (866, u'cp1251'), (869, u'cp1253'), (862, u'cp1255'), (874, u'cp874')] From 774cf24bfddb7726c4801627c93d69b9e4f774f5 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Sun, 21 Apr 2013 20:15:49 +0100 Subject: [PATCH 11/16] renamed type attribute --- openlp/plugins/songs/lib/ewimport.py | 26 +++++++++---------- .../openlp_plugins/songs/test_ewimport.py | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 4f8d7d970..287310cb3 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -50,7 +50,7 @@ NOTE_REGEX = re.compile(r'\(.*?\)') class FieldDescEntry: def __init__(self, name, field_type, size): self.name = name - self.type = field_type + self.field_type = field_type self.size = size @@ -231,25 +231,25 @@ class EasyWorshipSongImport(SongImport): # Begin with empty field struct list fsl = ['>'] for field_desc in field_descs: - if field_desc.type == 1: + if field_desc.field_type == 1: # string fsl.append('%ds' % field_desc.size) - elif field_desc.type == 3: + elif field_desc.field_type == 3: # 16-bit int fsl.append('H') - elif field_desc.type == 4: + elif field_desc.field_type == 4: # 32-bit int fsl.append('I') - elif field_desc.type == 9: + elif field_desc.field_type == 9: # Logical fsl.append('B') - elif field_desc.type == 0x0c: + elif field_desc.field_type == 0x0c: # Memo fsl.append('%ds' % field_desc.size) - elif field_desc.type == 0x0d: + elif field_desc.field_type == 0x0d: # Blob fsl.append('%ds' % field_desc.size) - elif field_desc.type == 0x15: + elif field_desc.field_type == 0x15: # Timestamp fsl.append('Q') else: @@ -267,19 +267,19 @@ class EasyWorshipSongImport(SongImport): elif field == 0: return None # Format the field depending on the field type - if field_desc.type == 1: + if field_desc.field_type == 1: # string return field.rstrip('\0').decode(self.encoding) - elif field_desc.type == 3: + elif field_desc.field_type == 3: # 16-bit int return field ^ 0x8000 - elif field_desc.type == 4: + elif field_desc.field_type == 4: # 32-bit int return field ^ 0x80000000 - elif field_desc.type == 9: + elif field_desc.field_type == 9: # Logical return (field ^ 0x80 == 1) - elif field_desc.type == 0x0c or field_desc.type == 0x0d: + elif field_desc.field_type == 0x0c or field_desc.field_type == 0x0d: # Memo or Blob block_start, blob_size = struct.unpack_from(' Date: Sun, 23 Jun 2013 07:14:03 +0100 Subject: [PATCH 12/16] HEAD --- .../openlp_plugins/songs/test_ewimport.py | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index 48caa7574..fc134e008 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -124,18 +124,18 @@ class TestEasyWorshipSongImport(TestCase): """ Test finding an existing field in a given list using the :mod:`findField` """ - # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions + # GIVEN: A mocked out SongImport class, a mocked out "manager" and a list of field descriptions. with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): mocked_manager = MagicMock() importer = EasyWorshipSongImport(mocked_manager) importer.fieldDescs = TEST_FIELD_DESCS - # WHEN: Given a field name that exists + # WHEN: Called with a field name that exists existing_fields = [u'Title', u'Text Percentage Bottom', u'RecID', u'Default Background', u'Words', u'BK Bitmap', u'Last Modified'] - - # THEN: The item corresponding the index returned should have the same name attribute for field_name in existing_fields: + + # THEN: The item corresponding the index returned should have the same name attribute self.assertEquals(importer.fieldDescs[importer.findField(field_name)].name, field_name) def find_non_existing_field_test(self): @@ -148,48 +148,51 @@ class TestEasyWorshipSongImport(TestCase): importer = EasyWorshipSongImport(mocked_manager) importer.fieldDescs = TEST_FIELD_DESCS - # WHEN: Given a field name that does not exist + # WHEN: Called with a field name that does not exist non_existing_fields = [u'BK Gradient Shading', u'BK Gradient Variant', u'Favorite', u'Copyright'] - - # THEN: The importer object should not be None for field_name in non_existing_fields: + + # THEN: The importer object should not be None self.assertRaises(IndexError, importer.findField, field_name) def set_record_struct_test(self): """ Test the :mod:`setRecordStruct` module """ - # GIVEN: A mocked out SongImport class, a mocked out struct class, and a mocked out "manager" + # GIVEN: A mocked out SongImport class, a mocked out struct class, and a mocked out "manager" and a list of + # field descriptions with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'), \ patch(u'openlp.plugins.songs.lib.ewimport.struct') as mocked_struct: mocked_manager = MagicMock() importer = EasyWorshipSongImport(mocked_manager) - # WHEN: Called with a list of field descriptions + # WHEN: setRecordStruct is called with a list of field descriptions + return_value = importer.setRecordStruct(TEST_FIELD_DESCS) - # THEN: setRecordStruct should return None and structStruct should be called with a value representing + # THEN: setRecordStruct should return None and Struct should be called with a value representing # the list of field descriptions - self.assertIsNone(importer.setRecordStruct(TEST_FIELD_DESCS), u'setRecordStruct should return None') + self.assertIsNone(return_value, u'setRecordStruct should return None') mocked_struct.Struct.assert_called_with('>50sHIB250s250s10sQ') def get_field_test(self): """ Test the :mod:`getField` module """ - # GIVEN: A mocked out SongImport class, a mocked out "manager" and an encoding + # GIVEN: A mocked out SongImport class, a mocked out "manager", an encoding and some test data and known results with patch(u'openlp.plugins.songs.lib.ewimport.SongImport'): mocked_manager = MagicMock() importer = EasyWorshipSongImport(mocked_manager) importer.encoding = TEST_DATA_ENCODING - - # WHEN: Supplied with some test data and known results importer.fields = TEST_FIELDS importer.fieldDescs = TEST_FIELD_DESCS field_results = [(0, 'A Heart Like Thine'), (1, 100), (2, 102L), (3, True), (6, None), (7, None)] - # THEN: getField should return the known results + # WHEN: Called with test data for field_index, result in field_results: - self.assertEquals(importer.getField(field_index), result, + return_value = importer.getField(field_index) + + # THEN: getField should return the known results + self.assertEquals(return_value, result, u'getField should return "%s" when called with "%s"' % (result, TEST_FIELDS[field_index])) def get_memo_field_test(self): From 6fabfef7d8dba10ed9888465c249144231da0135 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Thu, 27 Jun 2013 09:36:00 +0100 Subject: [PATCH 13/16] Made changes as suggested in previous merger comments --- .../openlp_plugins/songs/test_ewimport.py | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index fc134e008..b0669e560 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -12,34 +12,35 @@ from mock import patch, MagicMock from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) -SONG_TEST_DATA = [{u'title': u'Amazing Grace', - u'authors': [u'John Newton'], - u'copyright': u'Public Domain', - u'ccli_number': 0, - u'verses': - [(u'Amazing grace how sweet the sound,\nThat saved a wretch like me;\n' - u'I once was lost, but now am found\nWas blind, but now I see.', u'v1'), - (u'T\'was grace that taught my heart to fear,\nAnd grace my fears relieved;\n' - u'How precious did that grace appear\nThe hour I first believed.', u'v2'), - (u'Through many dangers, toil and snares,\nI have already come;\n' - u'\'Tis grace has brought me safe thus far,\nAnd grace will lead me home.', u'v3'), - (u'When we\'ve been there ten thousand years\nBright shining as the sun,\n' - u'We\'ve no less days to sing God\'s praise\nThan when we\'ve first begun.', u'v4')], - u'verse_order_list': []}, - {u'title': u'Beautiful Garden Of Prayer', - u'authors': [u'Eleanor Allen Schroll James H. Fillmore'], - u'copyright': u'Public Domain', - u'ccli_number': 0, - u'verses': - [(u'O the beautiful garden, the garden of prayer,\nO the beautiful garden of prayer.\n' - u'There my Savior awaits, and He opens the gates\nTo the beautiful garden of prayer.', u'c1'), - (u'There\'s a garden where Jesus is waiting,\nThere\'s a place that is wondrously fair.\n' - u'For it glows with the light of His presence,\n\'Tis the beautiful garden of prayer.', u'v1'), - (u'There\'s a garden where Jesus is waiting,\nAnd I go with my burden and care.\n' - u'Just to learn from His lips, words of comfort,\nIn the beautiful garden of prayer.', u'v2'), - (u'There\'s a garden where Jesus is waiting,\nAnd He bids you to come meet Him there,\n' - u'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', u'v3')], - u'verse_order_list': []}] +SONG_TEST_DATA = [ + {u'title': u'Amazing Grace', + u'authors': [u'John Newton'], + u'copyright': u'Public Domain', + u'ccli_number': 0, + u'verses': + [(u'Amazing grace how sweet the sound,\nThat saved a wretch like me;\n' + u'I once was lost, but now am found\nWas blind, but now I see.', u'v1'), + (u'T\'was grace that taught my heart to fear,\nAnd grace my fears relieved;\n' + u'How precious did that grace appear\nThe hour I first believed.', u'v2'), + (u'Through many dangers, toil and snares,\nI have already come;\n' + u'\'Tis grace has brought me safe thus far,\nAnd grace will lead me home.', u'v3'), + (u'When we\'ve been there ten thousand years\nBright shining as the sun,\n' + u'We\'ve no less days to sing God\'s praise\nThan when we\'ve first begun.', u'v4')], + u'verse_order_list': []}, + {u'title': u'Beautiful Garden Of Prayer', + u'authors': [u'Eleanor Allen Schroll James H. Fillmore'], + u'copyright': u'Public Domain', + u'ccli_number': 0, + u'verses': + [(u'O the beautiful garden, the garden of prayer,\nO the beautiful garden of prayer.\n' + u'There my Savior awaits, and He opens the gates\nTo the beautiful garden of prayer.', u'c1'), + (u'There\'s a garden where Jesus is waiting,\nThere\'s a place that is wondrously fair.\n' + u'For it glows with the light of His presence,\n\'Tis the beautiful garden of prayer.', u'v1'), + (u'There\'s a garden where Jesus is waiting,\nAnd I go with my burden and care.\n' + u'Just to learn from His lips, words of comfort,\nIn the beautiful garden of prayer.', u'v2'), + (u'There\'s a garden where Jesus is waiting,\nAnd He bids you to come meet Him there,\n' + u'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', u'v3')], + u'verse_order_list': []}] class EasyWorshipSongImportLogger(EasyWorshipSongImport): """ From e9c56b6cc53cf60f0552d2cf2cef27b59471bdad Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Fri, 28 Jun 2013 22:16:44 +0100 Subject: [PATCH 14/16] Corrected if statment and added enumeration class. --- openlp/plugins/songs/lib/ewimport.py | 51 ++++++++++--------- .../openlp_plugins/songs/test_ewimport.py | 12 +++-- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 287310cb3..2f7681b7e 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -54,6 +54,19 @@ class FieldDescEntry: self.size = size +class FieldType(object): + """ + An enumeration class for different field types that can be expected in an EasyWorship song file. + """ + String = 1 + Int16 = 3 + Int32 = 4 + Logical = 9 + Memo = 0x0c + Blob = 0x0d + Timestamp = 0x15 + + class EasyWorshipSongImport(SongImport): """ The :class:`EasyWorshipSongImport` class provides OpenLP with the @@ -65,7 +78,7 @@ class EasyWorshipSongImport(SongImport): def doImport(self): # Open the DB and MB files if they exist import_source_mb = self.import_source.replace('.DB', '.MB') - if not (os.path.isfile(self.import_source) or os.path.isfile(import_source_mb)): + if not (os.path.isfile(self.import_source) or not os.path.isfile(import_source_mb)): return db_size = os.path.getsize(self.import_source) if db_size < 0x800: @@ -231,26 +244,19 @@ class EasyWorshipSongImport(SongImport): # Begin with empty field struct list fsl = ['>'] for field_desc in field_descs: - if field_desc.field_type == 1: - # string + if field_desc.field_type == FieldType.String: fsl.append('%ds' % field_desc.size) - elif field_desc.field_type == 3: - # 16-bit int + elif field_desc.field_type == FieldType.Int16: fsl.append('H') - elif field_desc.field_type == 4: - # 32-bit int + elif field_desc.field_type == FieldType.Int32: fsl.append('I') - elif field_desc.field_type == 9: - # Logical + elif field_desc.field_type == FieldType.Logical: fsl.append('B') - elif field_desc.field_type == 0x0c: - # Memo + elif field_desc.field_type == FieldType.Memo: fsl.append('%ds' % field_desc.size) - elif field_desc.field_type == 0x0d: - # Blob + elif field_desc.field_type == FieldType.Blob: fsl.append('%ds' % field_desc.size) - elif field_desc.field_type == 0x15: - # Timestamp + elif field_desc.field_type == FieldType.Timestamp: fsl.append('Q') else: fsl.append('%ds' % field_desc.size) @@ -267,20 +273,15 @@ class EasyWorshipSongImport(SongImport): elif field == 0: return None # Format the field depending on the field type - if field_desc.field_type == 1: - # string + if field_desc.field_type == FieldType.String: return field.rstrip('\0').decode(self.encoding) - elif field_desc.field_type == 3: - # 16-bit int + elif field_desc.field_type == FieldType.Int16: return field ^ 0x8000 - elif field_desc.field_type == 4: - # 32-bit int + elif field_desc.field_type == FieldType.Int32: return field ^ 0x80000000 - elif field_desc.field_type == 9: - # Logical + elif field_desc.field_type == FieldType.Logical: return (field ^ 0x80 == 1) - elif field_desc.field_type == 0x0c or field_desc.field_type == 0x0d: - # Memo or Blob + elif field_desc.field_type == FieldType.Memo or field_desc.field_type == FieldType.Blob: block_start, blob_size = struct.unpack_from(' Date: Tue, 2 Jul 2013 22:10:28 +0100 Subject: [PATCH 15/16] changed test path to be "more" compatable with other os's --- tests/functional/openlp_plugins/songs/test_ewimport.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index 34a4b21b4..2190a7cfd 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -11,7 +11,8 @@ from mock import patch, MagicMock from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry, FieldType -TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs')) +TEST_PATH = os.path.abspath( + os.path.join(os.path.dirname(__file__), u'..', u'..', u'..', u'resources', u'easyworshipsongs')) SONG_TEST_DATA = [ {u'title': u'Amazing Grace', u'authors': [u'John Newton'], From d1b2cd67c98046bb08c3f46fe0feed345e0ef9b9 Mon Sep 17 00:00:00 2001 From: phill-ridout Date: Sat, 6 Jul 2013 15:40:49 +0100 Subject: [PATCH 16/16] Fixed if statment logic. Fixed test --- openlp/plugins/songs/lib/ewimport.py | 2 +- tests/functional/openlp_plugins/songs/test_ewimport.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 2f7681b7e..b548b32e2 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -78,7 +78,7 @@ class EasyWorshipSongImport(SongImport): def doImport(self): # Open the DB and MB files if they exist import_source_mb = self.import_source.replace('.DB', '.MB') - if not (os.path.isfile(self.import_source) or not os.path.isfile(import_source_mb)): + if not os.path.isfile(self.import_source) or not os.path.isfile(import_source_mb): return db_size = os.path.getsize(self.import_source) if db_size < 0x800: diff --git a/tests/functional/openlp_plugins/songs/test_ewimport.py b/tests/functional/openlp_plugins/songs/test_ewimport.py index 2190a7cfd..0e6956461 100644 --- a/tests/functional/openlp_plugins/songs/test_ewimport.py +++ b/tests/functional/openlp_plugins/songs/test_ewimport.py @@ -240,7 +240,7 @@ class TestEasyWorshipSongImport(TestCase): patch(u'openlp.plugins.songs.lib.ewimport.os.path') as mocked_os_path: mocked_manager = MagicMock() importer = EasyWorshipSongImport(mocked_manager) - mocked_os_path.isfile.return_value = False + mocked_os_path.isfile.side_effect = [True, False] # WHEN: Supplied with an import source importer.import_source = u'Songs.DB'