1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection #
6
# --------------------------------------------------------------------------- #
7
# Copyright (c) 2008-2016 OpenLP Developers #
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it #
10
# under the terms of the GNU General Public License as published by the Free #
11
# Software Foundation; version 2 of the License. #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
18
# You should have received a copy of the GNU General Public License along #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
###############################################################################
23
This module contains tests for the SWORD Bible importer.
28
from unittest import TestCase, SkipTest
30
from tests.functional import MagicMock, patch
32
from openlp.plugins.bibles.lib.sword import SwordBible
34
raise SkipTest('PySword is not installed, skipping SWORD test.')
35
from openlp.plugins.bibles.lib.db import BibleDB
37
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
38
'..', '..', '..', 'resources', 'bibles'))
41
class TestSwordImport(TestCase):
43
Test the functions in the :mod:`swordimport` module.
47
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
48
self.registry_patcher.start()
49
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
50
self.manager_patcher.start()
53
self.registry_patcher.stop()
54
self.manager_patcher.stop()
56
def create_importer_test(self):
58
Test creating an instance of the Sword file importer
60
# GIVEN: A mocked out "manager"
61
mocked_manager = MagicMock()
63
# WHEN: An importer object is created
64
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='')
66
# THEN: The importer should be an instance of BibleDB
67
self.assertIsInstance(importer, BibleDB)
69
@patch('openlp.plugins.bibles.lib.sword.SwordBible.application')
70
@patch('openlp.plugins.bibles.lib.sword.modules')
71
@patch('openlp.plugins.bibles.lib.db.BiblesResourcesDB')
72
def simple_import_test(self, mocked_bible_res_db, mocked_pysword_modules, mocked_application):
74
Test that a simple SWORD import works
76
# GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions
77
# get_book_ref_id_by_name, create_verse, create_book, session and get_language.
78
# Also mocked pysword structures
79
mocked_manager = MagicMock()
80
mocked_import_wizard = MagicMock()
81
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='')
82
result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb')
83
test_data = json.loads(result_file.read().decode())
84
importer.wizard = mocked_import_wizard
85
importer.get_book_ref_id_by_name = MagicMock()
86
importer.create_verse = MagicMock()
87
importer.create_book = MagicMock()
88
importer.session = MagicMock()
89
mocked_bible_res_db.get_language.return_value = 'Danish'
90
mocked_bible = MagicMock()
91
mocked_genesis = MagicMock()
92
mocked_genesis.name = 'Genesis'
93
mocked_genesis.num_chapters = 1
94
books = {'ot': [mocked_genesis]}
95
mocked_structure = MagicMock()
96
mocked_structure.get_books.return_value = books
97
mocked_bible.get_structure.return_value = mocked_structure
98
mocked_bible.get_iter.return_value = [verse[1] for verse in test_data['verses']]
99
mocked_module = MagicMock()
100
mocked_module.get_bible_from_module.return_value = mocked_bible
101
mocked_pysword_modules.SwordModules.return_value = mocked_module
103
# WHEN: Importing bible file
106
# THEN: The create_verse() method should have been called with each verse in the file.
107
self.assertTrue(importer.create_verse.called)
108
for verse_tag, verse_text in test_data['verses']:
109
importer.create_verse.assert_any_call(importer.create_book().id, 1, int(verse_tag), verse_text)