openlp/tests/functional/openlp_plugins/presentations/test_presentationcontroller.py

230 lines
10 KiB
Python
Raw Normal View History

2014-10-06 19:41:29 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2020 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2014-10-06 19:41:29 +00:00
"""
Functional tests to test the PresentationController and PresentationDocument
classes and related methods.
"""
from pathlib import Path
from unittest import TestCase
2017-09-17 19:43:15 +00:00
from unittest.mock import MagicMock, call, patch
2014-10-06 19:41:29 +00:00
from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument
2018-10-02 04:39:42 +00:00
2014-10-06 19:41:29 +00:00
FOLDER_TO_PATCH = 'openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder'
class TestPresentationController(TestCase):
"""
Test the PresentationController.
"""
def setUp(self):
2015-11-25 21:47:56 +00:00
self.get_thumbnail_folder_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder',
2017-09-18 06:32:19 +00:00
return_value=Path())
2015-11-25 21:47:56 +00:00
self.get_thumbnail_folder_patcher.start()
2014-10-06 19:41:29 +00:00
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
self.presentation = PresentationController(mocked_plugin)
self.document = PresentationDocument(self.presentation, '')
2015-11-25 21:47:56 +00:00
def tearDown(self):
self.get_thumbnail_folder_patcher.stop()
2016-05-31 21:40:13 +00:00
def test_constructor(self):
2014-10-06 19:41:29 +00:00
"""
Test the Constructor
"""
# GIVEN: A mocked plugin
# WHEN: The PresentationController is created
# THEN: The name of the presentation controller should be correct
2017-12-22 17:15:30 +00:00
assert 'PresentationController' == self.presentation.name, \
'The name of the presentation controller should be correct'
2014-10-06 19:41:29 +00:00
2016-05-31 21:40:13 +00:00
def test_save_titles_and_notes(self):
2014-10-06 19:41:29 +00:00
"""
Test PresentationDocument.save_titles_and_notes method with two valid lists
"""
# GIVEN: two lists of length==2 and a mocked open and get_thumbnail_folder
2017-09-17 19:43:15 +00:00
with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.write_text') as mocked_write_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
2014-10-06 19:41:29 +00:00
titles = ['uno', 'dos']
notes = ['one', 'two']
# WHEN: calling save_titles_and_notes
2017-09-17 19:43:15 +00:00
mocked_get_thumbnail_folder.return_value = Path('test')
2014-10-06 19:41:29 +00:00
self.document.save_titles_and_notes(titles, notes)
# THEN: the last call to open should have been for slideNotes2.txt
2017-12-22 21:04:29 +00:00
assert mocked_write_text.call_count == 3, 'There should be exactly three files written'
2017-09-17 19:43:15 +00:00
mocked_write_text.assert_has_calls([call('uno\ndos'), call('one'), call('two')])
2014-10-06 19:41:29 +00:00
2016-05-31 21:40:13 +00:00
def test_save_titles_and_notes_with_None(self):
2014-10-06 19:41:29 +00:00
"""
Test PresentationDocument.save_titles_and_notes method with no data
"""
# GIVEN: None and an empty list and a mocked open and get_thumbnail_folder
with patch('builtins.open') as mocked_open, patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
titles = None
notes = None
# WHEN: calling save_titles_and_notes
mocked_get_thumbnail_folder.return_value = 'test'
self.document.save_titles_and_notes(titles, notes)
# THEN: No file should have been created
2017-12-22 21:04:29 +00:00
assert mocked_open.call_count == 0, 'No file should be created'
2014-10-06 19:41:29 +00:00
2016-05-31 21:40:13 +00:00
def test_get_titles_and_notes(self):
2014-10-06 19:41:29 +00:00
"""
Test PresentationDocument.get_titles_and_notes method
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
2017-09-17 19:43:15 +00:00
with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text',
return_value='uno\ndos\n') as mocked_read_text, \
2014-10-06 19:41:29 +00:00
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
2017-09-17 19:43:15 +00:00
patch('openlp.plugins.presentations.lib.presentationcontroller.Path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = Path('test')
2014-10-06 19:41:29 +00:00
mocked_exists.return_value = True
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
# THEN: it should return two items for the titles and two empty strings for the notes
2017-12-22 21:04:29 +00:00
assert type(result_titles) is list, 'result_titles should be of type list'
assert len(result_titles) == 2, 'There should be two items in the titles'
assert type(result_notes) is list, 'result_notes should be of type list'
assert len(result_notes) == 2, 'There should be two items in the notes'
assert mocked_read_text.call_count == 3, 'Three files should be read'
2014-10-06 19:41:29 +00:00
2016-05-31 21:40:13 +00:00
def test_get_titles_and_notes_with_file_not_found(self):
2014-10-06 19:41:29 +00:00
"""
Test PresentationDocument.get_titles_and_notes method with file not found
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
2017-09-17 19:43:15 +00:00
with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text') as mocked_read_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
mocked_read_text.side_effect = FileNotFoundError()
mocked_get_thumbnail_folder.return_value = Path('test')
2014-10-06 19:41:29 +00:00
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
# THEN: it should return two empty lists
2017-12-22 17:15:30 +00:00
assert isinstance(result_titles, list), 'result_titles should be of type list'
2017-12-22 17:51:59 +00:00
assert len(result_titles) == 0, 'there be no titles'
2017-12-22 17:15:30 +00:00
assert isinstance(result_notes, list), 'result_notes should be a list'
2017-12-22 17:51:59 +00:00
assert len(result_notes) == 0, 'but the list should be empty'
2014-10-06 19:41:29 +00:00
2016-05-31 21:40:13 +00:00
def test_get_titles_and_notes_with_file_error(self):
2014-10-06 19:41:29 +00:00
"""
Test PresentationDocument.get_titles_and_notes method with file errors
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
2017-09-17 19:43:15 +00:00
with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text') as mocked_read_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
mocked_read_text.side_effect = OSError()
2017-09-17 19:43:15 +00:00
mocked_get_thumbnail_folder.return_value = Path('test')
2014-10-06 19:41:29 +00:00
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
# THEN: it should return two empty lists
2017-12-22 22:20:04 +00:00
assert type(result_titles) is list, 'result_titles should be a list'
2014-10-06 19:41:29 +00:00
class TestPresentationDocument(TestCase):
"""
Test the PresentationDocument Class
"""
def setUp(self):
"""
Set up the patches and mocks need for all tests.
"""
2017-10-07 07:05:07 +00:00
self.create_paths_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.create_paths')
2014-10-06 19:41:29 +00:00
self.get_thumbnail_folder_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder')
self._setup_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument._setup')
2017-10-07 07:05:07 +00:00
self.mock_create_paths = self.create_paths_patcher.start()
2014-10-06 19:41:29 +00:00
self.mock_get_thumbnail_folder = self.get_thumbnail_folder_patcher.start()
self.mock_setup = self._setup_patcher.start()
self.mock_controller = MagicMock()
2017-09-17 19:43:15 +00:00
self.mock_get_thumbnail_folder.return_value = Path('returned/path/')
2014-10-06 19:41:29 +00:00
def tearDown(self):
"""
Stop the patches
"""
2017-10-07 07:05:07 +00:00
self.create_paths_patcher.stop()
2014-10-06 19:41:29 +00:00
self.get_thumbnail_folder_patcher.stop()
self._setup_patcher.stop()
2016-05-31 21:40:13 +00:00
def test_initialise_presentation_document(self):
2014-10-06 19:41:29 +00:00
"""
Test the PresentationDocument __init__ method when initialising the PresentationDocument Class
"""
2014-10-21 20:08:37 +00:00
# GIVEN: A mocked setup method and mocked controller
2014-10-06 19:41:29 +00:00
self.mock_setup.reset()
# WHEN: Creating an instance of PresentationDocument
PresentationDocument(self.mock_controller, 'Name')
2014-10-21 20:08:37 +00:00
# THEN: PresentationDocument._setup should have been called with the argument 'Name'
2014-10-06 19:41:29 +00:00
self.mock_setup.assert_called_once_with('Name')
2016-05-31 21:40:13 +00:00
def test_presentation_document_setup(self):
2014-10-06 19:41:29 +00:00
"""
Test the PresentationDocument _setup method when initialising the PresentationDocument Class
"""
self._setup_patcher.stop()
2017-10-07 07:05:07 +00:00
# GIVEN: A mocked controller, patched create_paths and get_thumbnail_folder methods
2014-10-06 19:41:29 +00:00
# WHEN: Creating an instance of PresentationDocument
PresentationDocument(self.mock_controller, 'Name')
2017-10-07 07:05:07 +00:00
# THEN: create_paths should have been called with 'returned/path/'
self.mock_create_paths.assert_called_once_with(Path('returned', 'path/'))
2014-10-06 19:41:29 +00:00
self._setup_patcher.start()
2016-05-31 21:40:13 +00:00
def test_load_presentation(self):
2014-10-06 19:41:29 +00:00
"""
2014-10-21 20:08:37 +00:00
Test the PresentationDocument.load_presentation method.
2014-10-06 19:41:29 +00:00
"""
# GIVEN: An instance of PresentationDocument
instance = PresentationDocument(self.mock_controller, 'Name')
# WHEN: Calling load_presentation()
result = instance.load_presentation()
2014-10-21 20:08:37 +00:00
# THEN: load_presentation should return false
2017-12-22 22:20:04 +00:00
assert result is False, "PresentationDocument.load_presentation should return false."