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

112 lines
5.1 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 #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 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.
"""
import os
import json
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
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
from openlp.plugins.bibles.lib.db import BibleDB
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', '..', 'resources', 'bibles'))
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
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='')
# THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB)
@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()
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='')
result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb')
test_data = json.loads(result_file.read().decode())
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.
self.assertTrue(importer.create_verse.called)
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)