From 81de433df4428146ede98b1d824bcc0f4cbf6ba4 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Fri, 13 Feb 2015 23:01:07 +0000 Subject: [PATCH] Added test for csvbible import --- openlp/core/common/settings.py | 6 +- openlp/plugins/bibles/lib/csvbible.py | 6 +- .../openlp_plugins/bibles/test_csvimport.py | 97 +++++++++++++++++++ tests/resources/bibles/dk1933-books.csv | 22 +++++ tests/resources/bibles/dk1933-verses.csv | 10 ++ 5 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 tests/functional/openlp_plugins/bibles/test_csvimport.py create mode 100644 tests/resources/bibles/dk1933-books.csv create mode 100644 tests/resources/bibles/dk1933-verses.csv diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 8d5abb7bc..cd121c2ef 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -216,7 +216,8 @@ class Settings(QtCore.QSettings): 'shortcuts/moveDown': [QtGui.QKeySequence(QtCore.Qt.Key_PageDown)], 'shortcuts/nextTrackItem': [], 'shortcuts/nextItem_live': [QtGui.QKeySequence(QtCore.Qt.Key_Down), QtGui.QKeySequence(QtCore.Qt.Key_PageDown)], - 'shortcuts/nextItem_preview': [QtGui.QKeySequence(QtCore.Qt.Key_Down), QtGui.QKeySequence(QtCore.Qt.Key_PageDown)], + 'shortcuts/nextItem_preview': [QtGui.QKeySequence(QtCore.Qt.Key_Down), + QtGui.QKeySequence(QtCore.Qt.Key_PageDown)], 'shortcuts/nextService': [QtGui.QKeySequence(QtCore.Qt.Key_Right)], 'shortcuts/newService': [], 'shortcuts/offlineHelpItem': [], @@ -230,7 +231,8 @@ class Settings(QtCore.QSettings): 'shortcuts/playSlidesLoop': [], 'shortcuts/playSlidesOnce': [], 'shortcuts/previousService': [QtGui.QKeySequence(QtCore.Qt.Key_Left)], - 'shortcuts/previousItem_preview': [QtGui.QKeySequence(QtCore.Qt.Key_Up), QtGui.QKeySequence(QtCore.Qt.Key_PageUp)], + 'shortcuts/previousItem_preview': [QtGui.QKeySequence(QtCore.Qt.Key_Up), + QtGui.QKeySequence(QtCore.Qt.Key_PageUp)], 'shortcuts/printServiceItem': [QtGui.QKeySequence('Ctrl+P')], 'shortcuts/songExportItem': [], 'shortcuts/songUsageStatus': [QtGui.QKeySequence(QtCore.Qt.Key_F4)], diff --git a/openlp/plugins/bibles/lib/csvbible.py b/openlp/plugins/bibles/lib/csvbible.py index ffe54efa5..0836acaab 100644 --- a/openlp/plugins/bibles/lib/csvbible.py +++ b/openlp/plugins/bibles/lib/csvbible.py @@ -98,15 +98,15 @@ class CSVBible(BibleDB): for line in books_reader: if self.stop_import_flag: break - self.wizard.increment_progress_bar(translate('BiblesPlugin.CSVBible', 'Importing books... %s') - % line[2]) + self.wizard.increment_progress_bar(translate('BiblesPlugin.CSVBible', 'Importing books... %s') + % line[2]) book_ref_id = self.get_book_ref_id_by_name(line[2], 67, language_id) if not book_ref_id: log.error('Importing books from "%s" failed' % self.books_file) return False book_details = BiblesResourcesDB.get_book_by_id(book_ref_id) self.create_book(line[2], book_ref_id, book_details['testament_id']) - book_list[int(line[0])] = line[2] + book_list.update({int(line[0]): line[2]}) self.application.process_events() except (IOError, IndexError): log.exception('Loading books from file failed') diff --git a/tests/functional/openlp_plugins/bibles/test_csvimport.py b/tests/functional/openlp_plugins/bibles/test_csvimport.py new file mode 100644 index 000000000..9fb3de9c2 --- /dev/null +++ b/tests/functional/openlp_plugins/bibles/test_csvimport.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2015 OpenLP Developers # +# --------------------------------------------------------------------------- # +# 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 CSV Bible importer. +""" + +import os +import json +from unittest import TestCase + +from tests.functional import MagicMock, patch +from openlp.plugins.bibles.lib.csvbible import CSVBible +from openlp.plugins.bibles.lib.db import BibleDB + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), + '..', '..', '..', 'resources', 'bibles')) + + +class TestCSVImport(TestCase): + """ + Test the functions in the :mod:`csvimport` 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 CSV file importer + """ + # GIVEN: A mocked out "manager" + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = CSVBible(mocked_manager, path='.', name='.', booksfile='.', versefile='.') + + # THEN: The importer should be an instance of BibleDB + self.assertIsInstance(importer, BibleDB) + + def file_import_test(self): + """ + Test the actual import of CSV Bible file + """ + # 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. + result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb') + test_data = json.loads(result_file.read().decode()) + books_file = os.path.join(TEST_PATH, 'dk1933-books.csv') + verses_file = os.path.join(TEST_PATH, 'dk1933-verses.csv') + with patch('openlp.plugins.bibles.lib.csvbible.CSVBible.application'): + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = CSVBible(mocked_manager, path='.', name='.', booksfile=books_file, versefile=verses_file) + 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' + importer.get_book = MagicMock() + + # 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.get_book().id, '1', verse_tag, verse_text) + importer.create_book.assert_any_call('1. Mosebog', importer.get_book_ref_id_by_name(), 1) + importer.create_book.assert_any_call('1. Krønikebog', importer.get_book_ref_id_by_name(), 1) diff --git a/tests/resources/bibles/dk1933-books.csv b/tests/resources/bibles/dk1933-books.csv new file mode 100644 index 000000000..43bd7e462 --- /dev/null +++ b/tests/resources/bibles/dk1933-books.csv @@ -0,0 +1,22 @@ +1,1,1. Mosebog,1Mos +2,1,2. Mosebog,2Mos +3,1,3. Mosebog,3Mos +4,1,4. Mosebog,4Mos +5,1,5. Mosebog,5Mos +6,1,Josvabogen,jos +7,1,Dommerbogen,dom +8,1,Ruths Bog,ruth +9,1,1. Samuelsbog,1Sam +10,1,2. Samuelsbog,2Sam +11,1,1. Kongebog,1kong +12,1,2. Kongebog,2kong +13,1,1. Krønikebog,1kron +14,1,2. Krønikebog,2kron +15,1,Ezras Bog,ezra +16,1,Nehemias' Bog,neh +17,1,Esters Bog,est +18,1,Jobs Bog,job +19,1,Salmernes Bog,sl +20,1,Ordsprogenes Bog,ordsp +21,1,Prædikerens Bog,prad +22,1,Højsangen,hojs diff --git a/tests/resources/bibles/dk1933-verses.csv b/tests/resources/bibles/dk1933-verses.csv new file mode 100644 index 000000000..bb065b177 --- /dev/null +++ b/tests/resources/bibles/dk1933-verses.csv @@ -0,0 +1,10 @@ +1,1,1,"I Begyndelsen skabte Gud Himmelen og Jorden." +1,1,2,"Og Jorden var øde og tom, og der var Mørke over Verdensdybet. Men Guds Ånd svævede over Vandene." +1,1,3,"Og Gud sagde: ""Der blive Lys!"" Og der blev Lys." +1,1,4,"Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket," +1,1,5,"og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, og det blev Morgen, første Dag." +1,1,6,"Derpå sagde Gud: ""Der blive en Hvælving midt i Vandene til at skille Vandene ad!""" +1,1,7,"Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen fra Vandet over Hvælvingen;" +1,1,8,"og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag." +1,1,9,"Derpå sagde Gud: ""Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!"" Og således skete det;" +1,1,10,"og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, at det var godt."