# -*- coding: utf-8 -*- # vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 ############################################################################### # OpenLP - Open Source Lyrics Projection # # --------------------------------------------------------------------------- # # Copyright (c) 2008-2016 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 WordProject Bible importer. """ import os import json from unittest import TestCase from openlp.plugins.bibles.lib.importers.wordproject import WordProjectBible from openlp.plugins.bibles.lib.db import BibleDB from tests.functional import MagicMock, patch, call TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources', 'bibles')) INDEX_PAGE = """ The Holy Bible in the English language with audio narration - KJV

WordProject

facebook twitter google linkin

""" CHAPTER_PAGE = """ Creation of the world, Genesis Chapter 1

WordProject

facebook twitter google linkin

Genesis

Chapter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


Chapter 1

1 In the beginning God created the heaven and the earth.
2 And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.
3 And God said, Let there be light: and there was light.
4 And God saw the light, that it was good: and God divided the light from the darkness.
5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.
6 And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.
7 And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so.
8 And God called the firmament Heaven. And the evening and the morning were the second day.
9 And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so.
10 And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good.
11 And God said, Let the earth bring forth grass, the herb yielding seed, and the fruit tree yielding fruit after his kind, whose seed is in itself, upon the earth: and it was so.
12 And the earth brought forth grass, and herb yielding seed after his kind, and the tree yielding fruit, whose seed was in itself, after his kind: and God saw that it was good.
13 And the evening and the morning were the third day.
14 And God said, Let there be lights in the firmament of the heaven to divide the day from the night; and let them be for signs, and for seasons, and for days, and years:
15 And let them be for lights in the firmament of the heaven to give light upon the earth: and it was so.
16 And God made two great lights; the greater light to rule the day, and the lesser light to rule the night: he made the stars also.
17 And God set them in the firmament of the heaven to give light upon the earth,
18 And to rule over the day and over the night, and to divide the light from the darkness: and God saw that it was good.
19 And the evening and the morning were the fourth day.
20 And God said, Let the waters bring forth abundantly the moving creature that hath life, and fowl that may fly above the earth in the open firmament of heaven.
21 And God created great whales, and every living creature that moveth, which the waters brought forth abundantly, after their kind, and every winged fowl after his kind: and God saw that it was good.
22 And God blessed them, saying, Be fruitful, and multiply, and fill the waters in the seas, and let fowl multiply in the earth.
23 And the evening and the morning were the fifth day.
24 And God said, Let the earth bring forth the living creature after his kind, cattle, and creeping thing, and beast of the earth after his kind: and it was so.
25 And God made the beast of the earth after his kind, and cattle after their kind, and every thing that creepeth upon the earth after his kind: and God saw that it was good.
26 And God said, Let us make man in our image, after our likeness: and let them have dominion over the fish of the sea, and over the fowl of the air, and over the cattle, and over all the earth, and over every creeping thing that creepeth upon the earth.
27 So God created man in his own image, in the image of God created he him; male and female created he them.
28 And God blessed them, and God said unto them, Be fruitful, and multiply, and replenish the earth, and subdue it: and have dominion over the fish of the sea, and over the fowl of the air, and over every living thing that moveth upon the earth.
29 And God said, Behold, I have given you every herb bearing seed, which is upon the face of all the earth, and every tree, in the which is the fruit of a tree yielding seed; to you it shall be for meat.
30 And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein there is life, I have given every green herb for meat: and it was so.
31 And God saw every thing that he had made, and, behold, it was very good. And the evening and the morning were the sixth day.

""" class TestWordProjectImport(TestCase): """ Test the functions in the :mod:`wordprojectimport` module. """ def setUp(self): self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry') self.addCleanup(self.registry_patcher.stop) self.registry_patcher.start() self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager') self.addCleanup(self.manager_patcher.stop) self.manager_patcher.start() @patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_books(self, mocked_open, mocked_os): """ Test the process_books() method """ # GIVEN: A WordProject importer and a bunch of mocked things importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer.base_dir = '' importer.stop_import_flag = False importer.language_id = 'en' mocked_open.return_value.__enter__.return_value.read.return_value = INDEX_PAGE mocked_os.path.join.side_effect = lambda *x: ''.join(x) # WHEN: process_books() is called with patch.object(importer, 'find_and_create_book') as mocked_find_and_create_book, \ patch.object(importer, 'process_chapters') as mocked_process_chapters, \ patch.object(importer, 'session') as mocked_session: importer.process_books() # THEN: The right methods should have been called mocked_os.path.join.assert_called_once_with('', 'index.htm') mocked_open.assert_called_once_with('index.htm', encoding='utf-8', errors='ignore') assert mocked_find_and_create_book.call_count == 66, 'There should be 66 books' assert mocked_process_chapters.call_count == 66, 'There should be 66 books' assert mocked_session.commit.call_count == 66, 'There should be 66 books' @patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_chapters(self, mocked_open, mocked_os): """ Test the process_chapters() method """ # GIVEN: A WordProject importer and a bunch of mocked things importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer.base_dir = '' importer.stop_import_flag = False importer.language_id = 'en' mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_os.path.join.side_effect = lambda *x: ''.join(x) mocked_os.path.normpath.side_effect = lambda x: x mocked_db_book = MagicMock() mocked_db_book.name = 'Genesis' book_id = 1 book_link = '01/1.htm' # WHEN: process_chapters() is called with patch.object(importer, 'set_current_chapter') as mocked_set_current_chapter, \ patch.object(importer, 'process_verses') as mocked_process_verses: importer.process_chapters(mocked_db_book, book_id, book_link) # THEN: The right methods should have been called expected_set_current_chapter_calls = [call('Genesis', ch) for ch in range(1, 51)] expected_process_verses_calls = [call(mocked_db_book, 1, ch) for ch in range(1, 51)] mocked_os.path.join.assert_called_once_with('', '01/1.htm') mocked_open.assert_called_once_with('01/1.htm', encoding='utf-8', errors='ignore') assert mocked_set_current_chapter.call_args_list == expected_set_current_chapter_calls assert mocked_process_verses.call_args_list == expected_process_verses_calls @patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_verses(self, mocked_open, mocked_os): """ Test the process_verses() method """ # GIVEN: A WordProject importer and a bunch of mocked things importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer.base_dir = '' importer.stop_import_flag = False importer.language_id = 'en' mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_os.path.join.side_effect = lambda *x: '/'.join(x) mocked_db_book = MagicMock() mocked_db_book.name = 'Genesis' book_number = 1 chapter_number = 1 # WHEN: process_verses() is called with patch.object(importer, 'process_verse') as mocked_process_verse: importer.process_verses(mocked_db_book, book_number, chapter_number) # THEN: All the right methods should have been called mocked_os.path.join.assert_called_once_with('', '01', '1.htm') mocked_open.assert_called_once_with('/01/1.htm', encoding='utf-8', errors='ignore') assert mocked_process_verse.call_count == 31 def test_process_verse(self): """ Test the process_verse() method """ # GIVEN: An importer and a mocked method importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') mocked_db_book = MagicMock() mocked_db_book.id = 1 chapter_number = 1 verse_number = 1 verse_text = ' In the beginning, God created the heavens and the earth ' # WHEN: process_verse() is called with patch.object(importer, 'create_verse') as mocked_create_verse: importer.process_verse(mocked_db_book, chapter_number, verse_number, verse_text) # THEN: The create_verse() method should have been called mocked_create_verse.assert_called_once_with(1, 1, 1, 'In the beginning, God created the heavens and the earth') def test_process_verse_no_text(self): """ Test the process_verse() method when there's no text """ # GIVEN: An importer and a mocked method importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') mocked_db_book = MagicMock() mocked_db_book.id = 1 chapter_number = 1 verse_number = 1 verse_text = '' # WHEN: process_verse() is called with patch.object(importer, 'create_verse') as mocked_create_verse: importer.process_verse(mocked_db_book, chapter_number, verse_number, verse_text) # THEN: The create_verse() method should NOT have been called assert mocked_create_verse.call_count == 0 def test_do_import(self): """ Test the do_import() method """ # GIVEN: An importer and mocked methods importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') # WHEN: do_import() is called with patch.object(importer, '_unzip_file') as mocked_unzip_file, \ patch.object(importer, 'get_language_id') as mocked_get_language_id, \ patch.object(importer, 'process_books') as mocked_process_books, \ patch.object(importer, '_cleanup') as mocked_cleanup: mocked_get_language_id.return_value = 1 result = importer.do_import() # THEN: The correct methods should have been called mocked_unzip_file.assert_called_once_with() mocked_get_language_id.assert_called_once_with(None, bible_name='kj.zip') mocked_process_books.assert_called_once_with() mocked_cleanup.assert_called_once_with() assert result is True def test_do_import_no_language(self): """ Test the do_import() method when the language is not available """ # GIVEN: An importer and mocked methods importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') # WHEN: do_import() is called with patch.object(importer, '_unzip_file') as mocked_unzip_file, \ patch.object(importer, 'get_language_id') as mocked_get_language_id, \ patch.object(importer, 'process_books') as mocked_process_books, \ patch.object(importer, '_cleanup') as mocked_cleanup: mocked_get_language_id.return_value = None result = importer.do_import() # THEN: The correct methods should have been called mocked_unzip_file.assert_called_once_with() mocked_get_language_id.assert_called_once_with(None, bible_name='kj.zip') assert mocked_process_books.call_count == 0 mocked_cleanup.assert_called_once_with() assert result is False