Added test for ews import.

This commit is contained in:
Tomas Groth 2014-04-04 19:57:03 +02:00
parent 0ee684d3a8
commit 5c405050f1
3 changed files with 66 additions and 3 deletions

View File

@ -91,17 +91,24 @@ class EasyWorshipSongImport(SongImport):
Import the songs from service file Import the songs from service file
The full spec of the ews files can be found here: The full spec of the ews files can be found here:
https://github.com/meinders/lithium-ews/blob/master/docs/ews%20file%20format.md https://github.com/meinders/lithium-ews/blob/master/docs/ews%20file%20format.md
or here: http://wiki.openlp.org/Development:EasyWorship_EWS_Format
:return: :return:
""" """
# Open ews file if it exists # Open ews file if it exists
if not os.path.isfile(self.import_source): if not os.path.isfile(self.import_source):
log.debug('Given ews file does not exists.')
return return
# Make sure there is room for at least a header and one entry # Make sure there is room for at least a header and one entry
if os.path.getsize(self.import_source) < 892: if os.path.getsize(self.import_source) < 892:
log.debug('Given ews file is to small to contain valid data.')
return return
# Take a stab at how text is encoded # Take a stab at how text is encoded
self.encoding = 'cp1252' self.encoding = 'cp1252'
self.encoding = retrieve_windows_encoding(self.encoding)
if not self.encoding:
log.debug('No encoding set.')
return
self.ews_file = open(self.import_source, 'rb') self.ews_file = open(self.import_source, 'rb')
# Get file version # Get file version
type, = struct.unpack('<38s', self.ews_file.read(38)) type, = struct.unpack('<38s', self.ews_file.read(38))
@ -115,13 +122,14 @@ class EasyWorshipSongImport(SongImport):
elif version == '1.6': elif version == '1.6':
file_pos = 40 file_pos = 40
else: else:
log.debug('Given ews file is of unknown version.')
return return
entry_count = self.get_i32(file_pos) entry_count = self.get_i32(file_pos)
entry_length = self.get_i16(file_pos+4) entry_length = self.get_i16(file_pos+4)
file_pos += 6 file_pos += 6
self.import_wizard.progress_bar.setMaximum(entry_count) self.import_wizard.progress_bar.setMaximum(entry_count)
# Loop over songs # Loop over songs
for x in range(1, entry_count): for i in range(entry_count):
# Load entry metadata # Load entry metadata
self.set_defaults() self.set_defaults()
self.title = self.get_string(file_pos, 50) self.title = self.get_string(file_pos, 50)

View File

@ -69,6 +69,21 @@ SONG_TEST_DATA = [
'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', 'v3')], 'Just to bow and receive a new blessing,\nIn the beautiful garden of prayer.', 'v3')],
'verse_order_list': []}] 'verse_order_list': []}]
EWS_SONG_TEST_DATA =\
{ 'title' : 'Vi pløjed og vi så\'de',
'authors' : ['Matthias Claudius'],
'verses' :
[('Vi pløjed og vi så\'de\nvor sæd i sorten jord,\nså bad vi ham os hjælpe,\nsom højt i Himlen bor,\n'
'og han lod snefald hegne\nmod frosten barsk og hård,\nhan lod det tø og regne\nog varme mildt i vår.',
'v1'),
('Alle gode gaver\nde kommer ovenned,\nså tak da Gud, ja, pris dog Gud\nfor al hans kærlighed!', 'c1'),
('Han er jo den, hvis vilje\nopholder alle ting,\nhan klæder markens lilje\nog runder himlens ring,\n'
'ham lyder vind og vove,\nham rører ravnes nød,\nhvi skulle ej hans småbørn\nda og få dagligt brød?', 'v2'),
('Ja, tak, du kære Fader,\nså mild, så rig, så rund,\nfor korn i hæs og lader,\nfor godt i allen stund!\n'
'Vi kan jo intet give,\nsom nogen ting er værd,\nmen tag vort stakkels hjerte,\nså ringe som det er!',
'v3')],
}
class EasyWorshipSongImportLogger(EasyWorshipSongImport): class EasyWorshipSongImportLogger(EasyWorshipSongImport):
""" """
@ -349,9 +364,9 @@ class TestEasyWorshipSongImport(TestCase):
self.assertIsNone(importer.do_import(), 'do_import should return None when db_size is less than 0x800') self.assertIsNone(importer.do_import(), 'do_import should return None when db_size is less than 0x800')
mocked_retrieve_windows_encoding.assert_call(encoding) mocked_retrieve_windows_encoding.assert_call(encoding)
def file_import_test(self): def db_file_import_test(self):
""" """
Test the actual import of real song files and check that the imported data is correct. Test the actual import of real song database 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", # GIVEN: Test files with a mocked out SongImport class, a mocked out "manager", a mocked out "import_wizard",
@ -402,3 +417,43 @@ class TestEasyWorshipSongImport(TestCase):
self.assertEquals(importer.verse_order_list, verse_order_list, 'verse_order_list for %s should be %s' self.assertEquals(importer.verse_order_list, verse_order_list, 'verse_order_list for %s should be %s'
% (title, verse_order_list)) % (title, verse_order_list))
mocked_finish.assert_called_with() mocked_finish.assert_called_with()
def ews_file_import_test(self):
"""
Test the actual import of song from ews file and check that the imported data is correct.
"""
# GIVEN: Test files with a mocked out SongImport class, a mocked out "manager", a mocked out "import_wizard",
# and mocked out "author", "add_copyright", "add_verse", "finish" methods.
with patch('openlp.plugins.songs.lib.ewimport.SongImport'), \
patch('openlp.plugins.songs.lib.ewimport.retrieve_windows_encoding') as mocked_retrieve_windows_encoding:
mocked_retrieve_windows_encoding.return_value = 'cp1252'
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
mocked_add_author = MagicMock()
mocked_add_verse = MagicMock()
mocked_finish = MagicMock()
mocked_title = MagicMock()
mocked_finish.return_value = True
importer = EasyWorshipSongImportLogger(mocked_manager)
importer.import_wizard = mocked_import_wizard
importer.stop_import_flag = False
importer.add_author = mocked_add_author
importer.add_verse = mocked_add_verse
importer.title = mocked_title
importer.finish = mocked_finish
importer.topics = []
# WHEN: Importing ews file
importer.import_source = os.path.join(TEST_PATH, 'test1.ews')
# THEN: do_import should return none, the song data should be as expected, and finish should have been
# called.
title = EWS_SONG_TEST_DATA['title']
self.assertIsNone(importer.do_import(), 'do_import should return None when it has completed')
self.assertIn(title, importer._title_assignment_list, 'title for should be "%s"' % title)
mocked_add_author.assert_any_call(EWS_SONG_TEST_DATA['authors'][0])
for verse_text, verse_tag in EWS_SONG_TEST_DATA['verses']:
mocked_add_verse.assert_any_call(verse_text, verse_tag)
mocked_finish.assert_called_with()

Binary file not shown.