openlp/tests/functional/openlp_plugins/songs/test_worshipcenterproimport.py

246 lines
15 KiB
Python
Raw Normal View History

2013-08-31 18:17:38 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2013-08-31 18:17:38 +00:00
"""
This module contains tests for the WorshipCenter Pro song importer.
"""
2016-05-31 21:40:13 +00:00
from unittest import TestCase, skipUnless
from unittest.mock import patch, MagicMock
2016-05-31 21:40:13 +00:00
try:
import pyodbc
from openlp.core.common import Registry
from openlp.plugins.songs.lib.importers.worshipcenterpro import WorshipCenterProImport
CAN_RUN_TESTS = True
except ImportError:
CAN_RUN_TESTS = False
2015-10-27 15:15:22 +00:00
2013-08-31 18:17:38 +00:00
class TestRecord(object):
"""
Microsoft Access Driver is not available on non Microsoft Systems for this reason the :class:`TestRecord` is used
to simulate a recordset that would be returned by pyobdc.
"""
2016-05-31 21:40:13 +00:00
def __init__(self, id_, field, value):
2013-08-31 18:17:38 +00:00
# The case of the following instance variables is important as it needs to be the same as the ones in use in the
# WorshipCenter Pro database.
2016-05-31 21:40:13 +00:00
self.ID = id_
2013-08-31 18:17:38 +00:00
self.Field = field
self.Value = value
2016-05-31 21:40:13 +00:00
if CAN_RUN_TESTS:
class WorshipCenterProImportLogger(WorshipCenterProImport):
"""
This class logs changes in the title instance variable
"""
_title_assignment_list = []
2013-08-31 18:17:38 +00:00
2016-05-31 21:40:13 +00:00
def __init__(self, manager):
2017-09-30 20:16:30 +00:00
WorshipCenterProImport.__init__(self, manager, file_paths=[])
2013-08-31 18:17:38 +00:00
2016-05-31 21:40:13 +00:00
@property
def title(self):
return self._title_assignment_list[-1]
2013-08-31 18:17:38 +00:00
2016-05-31 21:40:13 +00:00
@title.setter
def title(self, title):
self._title_assignment_list.append(title)
2013-08-31 18:17:38 +00:00
RECORDSET_TEST_DATA = [TestRecord(1, 'TITLE', 'Amazing Grace'),
TestRecord(1, 'AUTHOR', 'John Newton'),
TestRecord(1, 'CCLISONGID', '12345'),
TestRecord(1, 'COMMENTS', 'The original version'),
TestRecord(1, 'COPY', 'Public Domain'),
2014-04-02 18:51:21 +00:00
TestRecord(
1, 'LYRICS',
'Amazing grace! How&crlf;sweet the sound&crlf;That saved a wretch like me!&crlf;'
'I once was lost,&crlf;but now am found;&crlf;Was blind, but now I see.&crlf;&crlf;'
'\'Twas grace that&crlf;taught my heart to fear,&crlf;And grace my fears relieved;&crlf;'
'How precious did&crlf;that grace appear&crlf;The hour I first believed.&crlf;&crlf;'
'Through many dangers,&crlf;toils and snares,&crlf;I have already come;&crlf;'
'\'Tis grace hath brought&crlf;me safe thus far,&crlf;'
'And grace will lead me home.&crlf;&crlf;The Lord has&crlf;promised good to me,&crlf;'
'His Word my hope secures;&crlf;He will my Shield&crlf;and Portion be,&crlf;'
'As long as life endures.&crlf;&crlf;Yea, when this flesh&crlf;and heart shall fail,&crlf;'
'And mortal life shall cease,&crlf;I shall possess,&crlf;within the veil,&crlf;'
'A life of joy and peace.&crlf;&crlf;The earth shall soon&crlf;dissolve like snow,&crlf;'
'The sun forbear to shine;&crlf;But God, Who called&crlf;me here below,&crlf;'
'Shall be forever mine.&crlf;&crlf;When we\'ve been there&crlf;ten thousand years,&crlf;'
'Bright shining as the sun,&crlf;We\'ve no less days to&crlf;sing God\'s praise&crlf;'
'Than when we\'d first begun.&crlf;&crlf;'),
2013-08-31 18:17:38 +00:00
TestRecord(2, 'TITLE', 'Beautiful Garden Of Prayer, The'),
2014-04-02 18:51:21 +00:00
TestRecord(
2, 'LYRICS',
'There\'s a garden where&crlf;Jesus is waiting,&crlf;'
'There\'s a place that&crlf;is wondrously fair,&crlf;For it glows with the&crlf;'
'light of His presence.&crlf;\'Tis the beautiful&crlf;garden of prayer.&crlf;&crlf;'
'Oh, the beautiful garden,&crlf;the garden of prayer!&crlf;Oh, the beautiful&crlf;'
'garden of prayer!&crlf;There my Savior awaits,&crlf;and He opens the gates&crlf;'
'To the beautiful&crlf;garden of prayer.&crlf;&crlf;There\'s a garden where&crlf;'
'Jesus is waiting,&crlf;And I go with my&crlf;burden and care,&crlf;'
'Just to learn from His&crlf;lips words of comfort&crlf;In the beautiful&crlf;'
'garden of prayer.&crlf;&crlf;There\'s a garden where&crlf;Jesus is waiting,&crlf;'
'And He bids you to come,&crlf;meet Him there;&crlf;Just to bow and&crlf;'
2015-10-27 22:36:52 +00:00
'receive a new blessing&crlf;In the beautiful&crlf;garden of prayer.&crlf;&crlf;')]
2013-08-31 18:17:38 +00:00
SONG_TEST_DATA = [{'title': 'Amazing Grace',
'verses': [
('Amazing grace! How\nsweet the sound\nThat saved a wretch like me!\nI once was lost,\n'
'but now am found;\nWas blind, but now I see.'),
('\'Twas grace that\ntaught my heart to fear,\nAnd grace my fears relieved;\nHow precious did\n'
'that grace appear\nThe hour I first believed.'),
('Through many dangers,\ntoils and snares,\nI have already come;\n\'Tis grace hath brought\n'
'me safe thus far,\nAnd grace will lead me home.'),
('The Lord has\npromised good to me,\nHis Word my hope secures;\n'
'He will my Shield\nand Portion be,\nAs long as life endures.'),
('Yea, when this flesh\nand heart shall fail,\nAnd mortal life shall cease,\nI shall possess,\n'
'within the veil,\nA life of joy and peace.'),
('The earth shall soon\ndissolve like snow,\nThe sun forbear to shine;\nBut God, Who called\n'
'me here below,\nShall be forever mine.'),
('When we\'ve been there\nten thousand years,\nBright shining as the sun,\n'
'We\'ve no less days to\nsing God\'s praise\nThan when we\'d first begun.')],
2015-10-27 22:36:52 +00:00
'author': 'John Newton',
'comments': 'The original version',
'copyright': 'Public Domain'},
2014-04-02 18:51:21 +00:00
{'title': 'Beautiful Garden Of Prayer, The',
2013-08-31 18:17:38 +00:00
'verses': [
('There\'s a garden where\nJesus is waiting,\nThere\'s a place that\nis wondrously fair,\n'
'For it glows with the\nlight of His presence.\n\'Tis the beautiful\ngarden of prayer.'),
('Oh, the beautiful garden,\nthe garden of prayer!\nOh, the beautiful\ngarden of prayer!\n'
'There my Savior awaits,\nand He opens the gates\nTo the beautiful\ngarden of prayer.'),
('There\'s a garden where\nJesus is waiting,\nAnd I go with my\nburden and care,\n'
'Just to learn from His\nlips words of comfort\nIn the beautiful\ngarden of prayer.'),
('There\'s a garden where\nJesus is waiting,\nAnd He bids you to come,\nmeet Him there;\n'
'Just to bow and\nreceive a new blessing\nIn the beautiful\ngarden of prayer.')]}]
2014-04-02 18:51:21 +00:00
2016-05-31 21:40:13 +00:00
@skipUnless(CAN_RUN_TESTS, 'Not Windows, skipping test')
2013-08-31 18:17:38 +00:00
class TestWorshipCenterProSongImport(TestCase):
"""
Test the functions in the :mod:`worshipcenterproimport` module.
"""
def setUp(self):
"""
Create the registry
"""
Registry.create()
2016-05-31 21:40:13 +00:00
def test_create_importer(self):
2013-08-31 18:17:38 +00:00
"""
Test creating an instance of the WorshipCenter Pro file importer
"""
# GIVEN: A mocked out SongImport class, and a mocked out "manager"
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.worshipcenterpro.SongImport'):
2013-08-31 18:17:38 +00:00
mocked_manager = MagicMock()
# WHEN: An importer object is created
2017-09-30 20:16:30 +00:00
importer = WorshipCenterProImport(mocked_manager, file_paths=[])
2013-08-31 18:17:38 +00:00
# THEN: The importer object should not be None
self.assertIsNotNone(importer, 'Import should not be none')
2016-05-31 21:40:13 +00:00
def test_pyodbc_exception(self):
2013-08-31 18:17:38 +00:00
"""
Test that exceptions raised by pyodbc are handled
"""
# GIVEN: A mocked out SongImport class, a mocked out pyodbc module, a mocked out translate method,
2014-03-05 18:58:22 +00:00
# a mocked "manager" and a mocked out log_error method.
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.worshipcenterpro.SongImport'), \
patch('openlp.plugins.songs.lib.importers.worshipcenterpro.pyodbc.connect') as mocked_pyodbc_connect, \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.worshipcenterpro.translate') as mocked_translate:
2013-08-31 18:17:38 +00:00
mocked_manager = MagicMock()
mocked_log_error = MagicMock()
mocked_translate.return_value = 'Translated Text'
2017-09-30 20:16:30 +00:00
importer = WorshipCenterProImport(mocked_manager, file_paths=[])
2014-03-05 18:58:22 +00:00
importer.log_error = mocked_log_error
2013-08-31 18:17:38 +00:00
importer.import_source = 'import_source'
pyodbc_errors = [pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError]
mocked_pyodbc_connect.side_effect = pyodbc_errors
2014-03-06 20:40:08 +00:00
# WHEN: Calling the do_import method
2013-08-31 18:17:38 +00:00
for effect in pyodbc_errors:
2014-03-06 20:40:08 +00:00
return_value = importer.do_import()
2013-08-31 18:17:38 +00:00
2014-03-06 20:40:08 +00:00
# THEN: do_import should return None, and pyodbc, translate & log_error are called with known calls
self.assertIsNone(return_value, 'do_import should return None when pyodbc raises an exception.')
2014-04-02 18:51:21 +00:00
mocked_pyodbc_connect.assert_called_with('DRIVER={Microsoft Access Driver (*.mdb)};DBQ=import_source')
2013-08-31 18:17:38 +00:00
mocked_translate.assert_called_with('SongsPlugin.WorshipCenterProImport',
2014-04-02 18:51:21 +00:00
'Unable to connect the WorshipCenter Pro database.')
2013-08-31 18:17:38 +00:00
mocked_log_error.assert_called_with('import_source', 'Translated Text')
2016-05-31 21:40:13 +00:00
def test_song_import(self):
2013-08-31 18:17:38 +00:00
"""
Test that a simulated WorshipCenter Pro recordset is imported correctly
"""
# GIVEN: A mocked out SongImport class, a mocked out pyodbc module with a simulated recordset, a mocked out
2014-03-05 18:58:22 +00:00
# translate method, a mocked "manager", add_verse method & mocked_finish method.
2014-07-04 09:31:06 +00:00
with patch('openlp.plugins.songs.lib.importers.worshipcenterpro.SongImport'), \
patch('openlp.plugins.songs.lib.importers.worshipcenterpro.pyodbc') as mocked_pyodbc, \
2014-07-04 09:31:06 +00:00
patch('openlp.plugins.songs.lib.importers.worshipcenterpro.translate') as mocked_translate:
2013-08-31 18:17:38 +00:00
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
mocked_add_verse = MagicMock()
2015-10-27 22:36:52 +00:00
mocked_parse_author = MagicMock()
mocked_add_comment = MagicMock()
mocked_add_copyright = MagicMock()
2013-08-31 18:17:38 +00:00
mocked_finish = MagicMock()
mocked_pyodbc.connect().cursor().fetchall.return_value = RECORDSET_TEST_DATA
mocked_translate.return_value = 'Translated Text'
importer = WorshipCenterProImportLogger(mocked_manager)
importer.import_source = 'import_source'
importer.import_wizard = mocked_import_wizard
2014-03-05 18:58:22 +00:00
importer.add_verse = mocked_add_verse
importer.parse_author = mocked_parse_author
importer.add_comment = mocked_add_comment
importer.add_copyright = mocked_add_copyright
2013-08-31 18:17:38 +00:00
importer.stop_import_flag = False
importer.finish = mocked_finish
2014-03-06 20:40:08 +00:00
# WHEN: Calling the do_import method
return_value = importer.do_import()
2013-08-31 18:17:38 +00:00
2014-04-02 19:35:09 +00:00
# THEN: do_import should return None, and pyodbc, import_wizard, importer.title and add_verse are called
# with known calls
2014-03-06 20:40:08 +00:00
self.assertIsNone(return_value, 'do_import should return None when pyodbc raises an exception.')
2013-08-31 18:17:38 +00:00
mocked_pyodbc.connect.assert_called_with('DRIVER={Microsoft Access Driver (*.mdb)};DBQ=import_source')
mocked_pyodbc.connect().cursor.assert_any_call()
mocked_pyodbc.connect().cursor().execute.assert_called_with('SELECT ID, Field, Value FROM __SONGDATA')
mocked_pyodbc.connect().cursor().fetchall.assert_any_call()
mocked_import_wizard.progress_bar.setMaximum.assert_called_with(2)
add_verse_call_count = 0
for song_data in SONG_TEST_DATA:
title_value = song_data['title']
self.assertIn(title_value, importer._title_assignment_list,
2014-04-02 18:51:21 +00:00
'title should have been set to %s' % title_value)
2013-08-31 18:17:38 +00:00
verse_calls = song_data['verses']
add_verse_call_count += len(verse_calls)
for call in verse_calls:
mocked_add_verse.assert_any_call(call, 'v')
if 'author' in song_data:
mocked_parse_author.assert_any_call(song_data['author'])
if 'comments' in song_data:
mocked_add_comment.assert_any_call(song_data['comments'])
if 'copyright' in song_data:
mocked_add_copyright.assert_any_call(song_data['copyright'])
2013-08-31 18:17:38 +00:00
self.assertEqual(mocked_add_verse.call_count, add_verse_call_count,
2014-04-02 18:51:21 +00:00
'Incorrect number of calls made to add_verse')