Fix two bugs in the MediaShout importer

- Fix a bug where the "Themes" table was never read due to an incorrect "if" statement
- Fix a bug where the "Groups" table was presumed to exist, but doesn't always (bug #35)
This commit is contained in:
Raoul Snyman 2020-09-23 15:25:37 -07:00
parent ac161d79ed
commit 74a72877d8
Signed by: raoul
GPG Key ID: F55BCED79626AE9C
2 changed files with 42 additions and 5 deletions

View File

@ -76,13 +76,15 @@ class MediaShoutImport(SongImport):
cursor.execute('SELECT Type, Number, POrder FROM PlayOrder WHERE Record = ? ORDER BY POrder', cursor.execute('SELECT Type, Number, POrder FROM PlayOrder WHERE Record = ? ORDER BY POrder',
float(song.Record)) float(song.Record))
verse_order = cursor.fetchall() verse_order = cursor.fetchall()
if cursor.tables(table='TableName', tableType='TABLE').fetchone(): topics = []
if cursor.tables(table='Themes', tableType='TABLE').fetchone():
cursor.execute('SELECT Name FROM Themes INNER JOIN SongThemes ON SongThemes.ThemeId = Themes.ThemeId ' cursor.execute('SELECT Name FROM Themes INNER JOIN SongThemes ON SongThemes.ThemeId = Themes.ThemeId '
'WHERE SongThemes.Record = ?', float(song.Record)) 'WHERE SongThemes.Record = ?', float(song.Record))
topics = cursor.fetchall() topics += cursor.fetchall()
cursor.execute('SELECT Name FROM Groups INNER JOIN SongGroups ON SongGroups.GroupId = Groups.GroupId ' if cursor.tables(table='Groups', tableType='TABLE').fetchone():
'WHERE SongGroups.Record = ?', float(song.Record)) cursor.execute('SELECT Name FROM Groups INNER JOIN SongGroups ON SongGroups.GroupId = Groups.GroupId '
topics += cursor.fetchall() 'WHERE SongGroups.Record = ?', float(song.Record))
topics += cursor.fetchall()
self.process_song(song, verses, verse_order, topics) self.process_song(song, verses, verse_order, topics)
def process_song(self, song, verses, verse_order, topics): def process_song(self, song, verses, verse_order, topics):

View File

@ -116,6 +116,41 @@ class TestMediaShoutImport(TestCase):
assert expected_execute_calls == mocked_cursor.execute.call_args_list assert expected_execute_calls == mocked_cursor.execute.call_args_list
mocked_process_song.assert_called_once_with(song, [verse], [play_order], [theme, group]) mocked_process_song.assert_called_once_with(song, [verse], [play_order], [theme, group])
@patch('openlp.plugins.songs.lib.importers.mediashout.pyodbc')
def test_do_import_without_topics(self, mocked_pyodbc):
"""
Test the MediaShoutImport do_import method with the Themes and Groups tables don't exist
"""
SongRecord = namedtuple('SongRecord', 'Record, Title, Author, Copyright, SongID, CCLI, Notes')
VerseRecord = namedtuple('VerseRecord', 'Type, Number, Text')
PlayOrderRecord = namedtuple('PlayOrderRecord', 'Type, Number, POrder')
song = SongRecord(1, 'Amazing Grace', 'William Wilberforce', 'Public Domain', 1, '654321', '')
verse = VerseRecord('Verse', 1, 'Amazing grace, how sweet the sound\nThat saved a wretch like me')
play_order = PlayOrderRecord('Verse', 1, 1)
# GIVEN: A MediaShoutImport instance and a bunch of stuff mocked out
importer = MediaShoutImport(MagicMock(), file_path='mediashout.db')
mocked_cursor = MagicMock()
mocked_cursor.fetchall.side_effect = [[song], [verse], [play_order]]
mocked_cursor.tables.fetchone.return_value = False
mocked_connection = MagicMock()
mocked_connection.cursor.return_value = mocked_cursor
mocked_pyodbc.connect.return_value = mocked_connection
# WHEN: do_import is called
with patch.object(importer, 'import_wizard'), \
patch.object(importer, 'process_song') as mocked_process_song:
importer.do_import()
# THEN: The songs should have been imported
expected_execute_calls = [
call('SELECT Record, Title, Author, Copyright, SongID, CCLI, Notes FROM Songs ORDER BY Title'),
call('SELECT Type, Number, Text FROM Verses WHERE Record = ? ORDER BY Type, Number', 1.0),
call('SELECT Type, Number, POrder FROM PlayOrder WHERE Record = ? ORDER BY POrder', 1.0)
]
assert expected_execute_calls == mocked_cursor.execute.call_args_list
mocked_process_song.assert_called_once_with(song, [verse], [play_order])
@patch('openlp.plugins.songs.lib.importers.mediashout.pyodbc') @patch('openlp.plugins.songs.lib.importers.mediashout.pyodbc')
def test_do_import_breaks_on_stop(self, mocked_pyodbc): def test_do_import_breaks_on_stop(self, mocked_pyodbc):
""" """