Added tests for import of OpenSong bible format

This commit is contained in:
Tomas Groth 2014-05-21 11:30:36 +02:00
parent 4039cf1103
commit c0b167ca78
3 changed files with 194 additions and 8 deletions

View File

@ -0,0 +1,140 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# 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 OpenSong Bible importer.
"""
import os
from unittest import TestCase
from tests.functional import MagicMock, patch
from openlp.plugins.bibles.lib.opensong import OpenSongBible
from openlp.plugins.bibles.lib.db import BibleDB
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', '..', 'resources', 'bibles'))
OPENSONG_TEST_DATA = {
'opensong-dk1933.xml': {
'book': 'Genesis',
'chapter': 1,
'verses': [
(1, 'I Begyndelsen skabte Gud Himmelen og Jorden.'),
(2, 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. '
'Men Guds Ånd svævede over Vandene.'),
(3, 'Og Gud sagde: "Der blive Lys!" Og der blev Lys.'),
(4, 'Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,'),
(5, 'og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, '
'og det blev Morgen, første Dag.'),
(6, 'Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"'),
(7, 'Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen '
'fra Vandet over Hvælvingen;'),
(8, 'og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.'),
(9, 'Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" '
'Og således skete det;'),
(10, 'og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, '
'at det var godt.')
]
}
}
class TestOpenSongImport(TestCase):
"""
Test the functions in the :mod:`opensongimport` module.
"""
def setUp(self):
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
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()
def create_importer_test(self):
"""
Test creating an instance of the OpenSong file importer
"""
# GIVEN: A mocked out "manager"
mocked_manager = MagicMock()
# WHEN: An importer object is created
importer = OpenSongBible(mocked_manager, path='.', name='.', filename='')
# THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB)
def file_import_test(self):
"""
Test the actual import of real song files
"""
# 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.
with patch('openlp.plugins.bibles.lib.opensong.OpenSongBible.application'):
for bible_file in OPENSONG_TEST_DATA:
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
importer = OpenSongBible(mocked_manager, path='.', name='.', filename='')
importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock()
importer.create_book = MagicMock()
importer.session = MagicMock()
importer.get_language = MagicMock()
importer.get_language.return_value = 'Danish'
# WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, 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 OPENSONG_TEST_DATA[bible_file]['verses']:
importer.create_verse.assert_any_call(importer.create_book().id, 1, verse_tag, verse_text)
def zefania_import_error_test(self):
"""
Test that we give an error message if trying to import a zefania bible
"""
# GIVEN: A mocked out "manager" and mocked out critical_error_message_box and an import
with patch('openlp.plugins.bibles.lib.opensong.critical_error_message_box') as \
mocked_critical_error_message_box:
mocked_manager = MagicMock()
importer = OpenSongBible(mocked_manager, path='.', name='.', filename='')
# WHEN: An trying to import a zefania bible
importer.filename = os.path.join(TEST_PATH, 'zefania-dk1933.xml')
importer.do_import()
# THEN: The importer should have "shown" an error message
mocked_critical_error_message_box.assert_called_with(message='Incorrect Bible file type supplied. '
'This looks like a Zefania XML bible, '
'please use the Zefania import option.')

View File

@ -83,21 +83,22 @@ class TestZefaniaImport(TestCase):
""" """
Test creating an instance of the Zefania file importer Test creating an instance of the Zefania file importer
""" """
# GIVEN: A mocked out BibleDB class, and a mocked out "manager" # GIVEN: A mocked out "manager"
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='')
# THEN: The importer should be an instance of SongImport # THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB) self.assertIsInstance(importer, BibleDB)
def file_import_test(self): def file_import_test(self):
""" """
Test the actual import of real song files Test the actual import of real song files
""" """
# GIVEN: Test files with a mocked out "manager" and a mocked out "import_wizard" # GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions
with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application') as brdb: # get_book_ref_id_by_name, create_verse, create_book, session and get_language.
with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application'):
for bible_file in ZEFANIA_TEST_DATA: for bible_file in ZEFANIA_TEST_DATA:
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
@ -112,10 +113,9 @@ class TestZefaniaImport(TestCase):
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.filename = os.path.join(TEST_PATH, bible_file)
importer.do_import('Dansk Version') importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.
self.assertTrue(importer.create_verse.called) self.assertTrue(importer.create_verse.called)
for verse_tag, verse_text in ZEFANIA_TEST_DATA['zefania-dk1933.xml']['verses']: for verse_tag, verse_text in ZEFANIA_TEST_DATA[bible_file]['verses']:
importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text) importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text)

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Visit the online documentation for Zefania XML Markup-->
<!--http://bgfdb.de/zefaniaxml/bml/-->
<!--Download another Zefania XML files from-->
<!--http://sourceforge.net/projects/zefania-sharp-->
<bible>
<!-- The INFORMATION-tag is a leftover from the original zefania bible, but is ignored by opensong format -->
<INFORMATION>
<title>Danish Version</title>
<creator>
</creator>
<subject>The Holy Bible</subject>
<description>The electronic edition of this Bible comes from the Danish 1933 edition. The Old Testament is an update from the 1931 edition, and the New Testament is an update from the 1907 edition.</description>
<publisher>Free Bible Software Group</publisher>
<contributors>
The Unbound Bible
Biola University: Administrative Computing
13800 Biola Ave.
La Mirada, CA 90639
United States of America
562-903-4722
</contributors>
<date>2009-01-20</date>
<format>Zefania XML Bible Markup Language</format>
<identifier>DAN</identifier>
<source>ftp://unboundftp.biola.edu/pub/danish1933_utf8.zip</source>
<language>DAN</language>
<coverage>provide the bible to the world</coverage>
<rights></rights>
<type />
</INFORMATION>
<b n="Genesis">
<c n="1">
<v n="1">I Begyndelsen skabte Gud Himmelen og Jorden.</v>
<v n="2">Og Jorden var øde og tom, og der var Mørke over Verdensdybet. Men Guds Ånd svævede over Vandene.</v>
<v n="3">Og Gud sagde: "Der blive Lys!" Og der blev Lys.</v>
<v n="4">Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,</v>
<v n="5">og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, og det blev Morgen, første Dag.</v>
<v n="6">Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"</v>
<v n="7">Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen fra Vandet over Hvælvingen;</v>
<v n="8">og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.</v>
<v n="9">Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" Og således skete det;</v>
<v n="10">og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, at det var godt.</v>
</c>
</b>
</bible>