openlp/tests/helpers/songfileimport.py

168 lines
8.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# 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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2013-09-07 17:02:28 +00:00
"""
The :mod:`songfileimporthelper` modules provides a helper class and methods to easily enable testing the import of
song files from third party applications.
"""
import json
import logging
2018-10-02 04:39:42 +00:00
from unittest.mock import MagicMock, call, patch
2013-10-05 09:16:04 +00:00
log = logging.getLogger(__name__)
2014-03-05 18:58:22 +00:00
2020-03-10 16:59:25 +00:00
class SongImportTestHelper(object):
2013-09-07 17:02:28 +00:00
"""
This class is designed to be a helper class to reduce repetition when testing the import of song files.
2013-09-07 17:02:28 +00:00
"""
def __init__(self, *args, **kwargs):
2020-03-11 21:53:41 +00:00
self.importer_class_name = args[0]
self.importer_module_name = args[1]
2014-07-04 09:36:54 +00:00
self.importer_module = __import__('openlp.plugins.songs.lib.importers.%s' %
self.importer_module_name, fromlist=[self.importer_class_name])
2013-09-07 17:02:28 +00:00
self.importer_class = getattr(self.importer_module, self.importer_class_name)
def setUp(self):
"""
Patch and set up the mocks required.
"""
2014-07-04 09:36:54 +00:00
self.add_copyright_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.add_copyright' %
(self.importer_module_name, self.importer_class_name))
self.add_verse_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.add_verse' %
(self.importer_module_name, self.importer_class_name))
self.finish_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.finish' %
(self.importer_module_name, self.importer_class_name))
self.add_author_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.add_author' %
(self.importer_module_name, self.importer_class_name))
2014-07-04 09:31:06 +00:00
self.song_import_patcher = patch('openlp.plugins.songs.lib.importers.%s.SongImport' %
2014-07-03 17:57:13 +00:00
self.importer_module_name)
2013-09-07 17:02:28 +00:00
self.mocked_add_copyright = self.add_copyright_patcher.start()
self.mocked_add_verse = self.add_verse_patcher.start()
self.mocked_finish = self.finish_patcher.start()
self.mocked_add_author = self.add_author_patcher.start()
2013-09-07 17:02:28 +00:00
self.mocked_song_importer = self.song_import_patcher.start()
self.mocked_manager = MagicMock()
self.mocked_import_wizard = MagicMock()
self.mocked_finish.return_value = True
def tearDown(self):
"""
Clean up
"""
self.add_copyright_patcher.stop()
self.add_verse_patcher.stop()
self.finish_patcher.stop()
self.add_author_patcher.stop()
2013-09-07 17:02:28 +00:00
self.song_import_patcher.stop()
def __enter__(self):
"""Make this class into a context manager"""
self.setUp()
return self
def __exit__(self, exc_type, exc_value, traceback):
"""Make this class into a context manager"""
self.tearDown
2017-12-22 22:21:38 +00:00
def load_external_result_data(self, file_path):
2013-09-07 17:02:28 +00:00
"""
A method to load and return an object containing the song data from an external file.
2017-12-22 22:21:38 +00:00
:param pathlib.Path file_path: The path of the file to load
2013-09-07 17:02:28 +00:00
"""
2017-12-23 23:45:10 +00:00
return json.loads(file_path.read_bytes().decode())
2013-09-07 17:02:28 +00:00
def file_import(self, source_file_name, result_data):
"""
Import the given file and check that it has imported correctly
"""
2017-09-30 20:16:30 +00:00
importer = self.importer_class(self.mocked_manager, file_paths=[source_file_name])
2013-09-07 17:02:28 +00:00
importer.import_wizard = self.mocked_import_wizard
importer.stop_import_flag = False
importer.topics = []
# WHEN: Importing the source file
2014-06-25 15:03:00 +00:00
importer.import_source = source_file_name
2013-10-14 22:05:17 +00:00
add_verse_calls = self._get_data(result_data, 'verses')
author_calls = self._get_data(result_data, 'authors')
ccli_number = self._get_data(result_data, 'ccli_number')
comments = self._get_data(result_data, 'comments')
song_book_name = self._get_data(result_data, 'song_book_name')
song_copyright = self._get_data(result_data, 'copyright')
song_number = self._get_data(result_data, 'song_number')
title = self._get_data(result_data, 'title')
topics = self._get_data(result_data, 'topics')
verse_order_list = self._get_data(result_data, 'verse_order_list')
2013-09-07 17:02:28 +00:00
# THEN: do_import should return none, the song data should be as expected, and finish should have been called.
2017-12-23 09:09:45 +00:00
assert importer.do_import() is None, 'do_import should return None when it has completed'
# Debug information - will be displayed when the test fails
log.debug("Title imported: %s" % importer.title)
log.debug("Verses imported: %s" % self.mocked_add_verse.mock_calls)
log.debug("Verse order imported: %s" % importer.verse_order_list)
log.debug("Authors imported: %s" % self.mocked_add_author.mock_calls)
log.debug("CCLI No. imported: %s" % importer.ccli_number)
log.debug("Comments imported: %s" % importer.comments)
log.debug("Songbook imported: %s" % importer.song_book_name)
log.debug("Song number imported: %s" % importer.song_number)
log.debug("Song copyright imported: %s" % importer.song_number)
log.debug("Topics imported: %s" % importer.topics)
assert importer.title == title, \
'title for %s should be "%s" and is "%s"' % (source_file_name, title, importer.title)
2013-09-07 17:02:28 +00:00
for author in author_calls:
if isinstance(author, str):
self.mocked_add_author.assert_any_call(author)
else:
2015-12-20 19:28:14 +00:00
self.mocked_add_author.assert_any_call(*author)
2013-09-07 17:02:28 +00:00
if song_copyright:
self.mocked_add_copyright.assert_called_with(song_copyright)
if ccli_number:
2017-12-23 09:09:45 +00:00
assert importer.ccli_number == ccli_number, \
'ccli_number for %s should be %s' % (source_file_name, ccli_number)
expected_calls = []
2013-09-07 17:02:28 +00:00
for verse_text, verse_tag in add_verse_calls:
self.mocked_add_verse.assert_any_call(verse_text, verse_tag)
expected_calls.append(call(verse_text, verse_tag))
self.mocked_add_verse.assert_has_calls(expected_calls, any_order=False)
2013-09-07 17:02:28 +00:00
if topics:
2017-12-23 09:09:45 +00:00
assert importer.topics == topics, 'topics for %s should be %s' % (source_file_name, topics)
2013-09-07 17:02:28 +00:00
if comments:
2017-12-23 09:09:45 +00:00
assert importer.comments == comments, \
'comments for %s should be "%s"' % (source_file_name, comments)
2013-09-07 17:02:28 +00:00
if song_book_name:
2017-12-23 09:09:45 +00:00
assert importer.song_book_name == song_book_name, \
'song_book_name for %s should be "%s"' % (source_file_name, song_book_name)
2013-09-07 17:02:28 +00:00
if song_number:
2017-12-23 09:09:45 +00:00
assert importer.song_number == song_number, \
'song_number for %s should be %s' % (source_file_name, song_number)
2013-09-07 17:02:28 +00:00
if verse_order_list:
2017-12-23 09:09:45 +00:00
assert importer.verse_order_list == verse_order_list, \
'verse_order_list for %s should be %s and is %s' % (source_file_name,
verse_order_list, importer.verse_order_list)
2013-09-07 17:02:28 +00:00
self.mocked_finish.assert_called_with()
2013-10-14 22:05:17 +00:00
def _get_data(self, data, key):
2013-09-07 17:02:28 +00:00
if key in data:
return data[key]
return ''