openlp/tests/functional/openlp_plugins/bibles/test_swordimport.py

111 lines
5.0 KiB
Python
Raw Normal View History

2016-04-15 20:34:20 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2017-12-29 09:15:48 +00:00
# Copyright (c) 2008-2018 OpenLP Developers #
2016-04-15 20:34:20 +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; 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 #
###############################################################################
"""
This module contains tests for the SWORD Bible importer.
"""
2016-05-31 21:40:13 +00:00
from unittest import TestCase, skipUnless
from unittest.mock import MagicMock, patch
2016-04-15 20:34:20 +00:00
2018-10-02 04:39:42 +00:00
from openlp.plugins.bibles.lib.db import BibleDB
from tests.utils import load_external_result_data
from tests.utils.constants import RESOURCE_PATH
try:
from openlp.plugins.bibles.lib.importers.sword import SwordBible
2016-05-31 21:40:13 +00:00
HAS_PYSWORD = True
except ImportError:
2016-05-31 21:40:13 +00:00
HAS_PYSWORD = False
2016-04-15 20:34:20 +00:00
2017-12-22 22:35:09 +00:00
TEST_PATH = RESOURCE_PATH / 'bibles'
2016-04-15 20:34:20 +00:00
2016-05-31 21:40:13 +00:00
@skipUnless(HAS_PYSWORD, 'pysword not installed')
2016-04-15 20:34:20 +00:00
class TestSwordImport(TestCase):
"""
Test the functions in the :mod:`swordimport` module.
"""
def setUp(self):
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
2016-04-15 20:34:20 +00:00
self.registry_patcher.start()
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
self.manager_patcher.start()
def tearDown(self):
self.registry_patcher.stop()
self.manager_patcher.stop()
2016-05-31 21:40:13 +00:00
def test_create_importer(self):
2016-04-15 20:34:20 +00:00
"""
Test creating an instance of the Sword file importer
"""
# GIVEN: A mocked out "manager"
mocked_manager = MagicMock()
# WHEN: An importer object is created
2017-10-10 19:09:20 +00:00
importer = SwordBible(mocked_manager, path='.', name='.', file_path=None, sword_key='', sword_path='')
2016-04-15 20:34:20 +00:00
# THEN: The importer should be an instance of BibleDB
2017-12-22 15:50:45 +00:00
assert isinstance(importer, BibleDB)
2016-04-15 20:34:20 +00:00
@patch('openlp.plugins.bibles.lib.importers.sword.SwordBible.application')
@patch('openlp.plugins.bibles.lib.importers.sword.modules')
2016-10-04 13:48:54 +00:00
def test_simple_import(self, mocked_pysword_modules, mocked_application):
2016-04-15 20:34:20 +00:00
"""
Test that a simple SWORD import works
"""
# GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions
# get_book_ref_id_by_name, create_verse, create_book, session and get_language.
# Also mocked pysword structures
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
2017-10-10 19:09:20 +00:00
importer = SwordBible(mocked_manager, path='.', name='.', file_path=None, sword_key='', sword_path='')
2017-12-22 22:21:38 +00:00
test_data = load_external_result_data(TEST_PATH / 'dk1933.json')
2016-04-15 20:34:20 +00:00
importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock()
importer.create_book = MagicMock()
importer.session = MagicMock()
2016-10-04 13:48:54 +00:00
importer.get_language = MagicMock(return_value='Danish')
2016-04-15 20:34:20 +00:00
mocked_bible = MagicMock()
mocked_genesis = MagicMock()
mocked_genesis.name = 'Genesis'
mocked_genesis.num_chapters = 1
books = {'ot': [mocked_genesis]}
mocked_structure = MagicMock()
mocked_structure.get_books.return_value = books
mocked_bible.get_structure.return_value = mocked_structure
mocked_bible.get_iter.return_value = [verse[1] for verse in test_data['verses']]
mocked_module = MagicMock()
mocked_module.get_bible_from_module.return_value = mocked_bible
mocked_pysword_modules.SwordModules.return_value = mocked_module
# WHEN: Importing bible file
importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file.
2017-12-22 15:50:45 +00:00
assert importer.create_verse.called is True
2016-04-15 20:34:20 +00:00
for verse_tag, verse_text in test_data['verses']:
importer.create_verse.assert_any_call(importer.create_book().id, 1, int(verse_tag), verse_text)