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

167 lines
8.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# 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 #
###############################################################################
"""
2013-12-28 17:06:29 +00:00
Functional tests to test the PresentationController and PresentationDocument
classes and related methods.
"""
from unittest import TestCase
2013-12-28 17:06:29 +00:00
import os
from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument
from tests.functional import MagicMock, patch, mock_open
2013-12-30 08:35:05 +00:00
FOLDER_TO_PATCH = 'openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder'
2014-07-11 13:35:44 +00:00
class TestPresentationController(TestCase):
2013-12-28 20:03:45 +00:00
"""
Test the PresentationController.
"""
2013-12-28 17:06:29 +00:00
def setUp(self):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
self.presentation = PresentationController(mocked_plugin)
self.document = PresentationDocument(self.presentation, '')
2013-10-27 20:33:58 +00:00
def constructor_test(self):
"""
2013-10-27 20:33:58 +00:00
Test the Constructor
"""
2013-12-28 17:06:29 +00:00
# GIVEN: A mocked plugin
2013-12-28 17:06:29 +00:00
# WHEN: The PresentationController is created
2013-10-27 20:33:58 +00:00
# THEN: The name of the presentation controller should be correct
2013-12-28 17:06:29 +00:00
self.assertEqual('PresentationController', self.presentation.name,
2013-10-27 20:33:58 +00:00
'The name of the presentation controller should be correct')
2013-12-28 17:06:29 +00:00
def save_titles_and_notes_test(self):
"""
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
mocked_open = mock_open()
2013-12-30 08:35:05 +00:00
with patch('builtins.open', mocked_open), patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
2013-12-28 17:06:29 +00:00
titles = ['uno', 'dos']
notes = ['one', 'two']
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# WHEN: calling save_titles_and_notes
mocked_get_thumbnail_folder.return_value = 'test'
self.document.save_titles_and_notes(titles, notes)
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# THEN: the last call to open should have been for slideNotes2.txt
2013-12-30 08:35:05 +00:00
mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), mode='w')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), mode='w')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), mode='w')
self.assertEqual(mocked_open.call_count, 3, 'There should be exactly three files opened')
2013-12-28 17:06:29 +00:00
mocked_open().writelines.assert_called_once_with(['uno', 'dos'])
mocked_open().write.assert_called_any('one')
mocked_open().write.assert_called_any('two')
def save_titles_and_notes_with_None_test(self):
"""
Test PresentationDocument.save_titles_and_notes method with no data
"""
# GIVEN: None and an empty list and a mocked open and get_thumbnail_folder
2013-12-30 08:35:05 +00:00
with patch('builtins.open') as mocked_open, patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
2013-12-28 17:06:29 +00:00
titles = None
notes = None
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# WHEN: calling save_titles_and_notes
mocked_get_thumbnail_folder.return_value = 'test'
self.document.save_titles_and_notes(titles, notes)
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# THEN: No file should have been created
2013-12-30 08:35:05 +00:00
self.assertEqual(mocked_open.call_count, 0, 'No file should be created')
2013-12-28 17:06:29 +00:00
def get_titles_and_notes_test(self):
"""
Test PresentationDocument.get_titles_and_notes method
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
with patch('builtins.open', mock_open(read_data='uno\ndos\n')) as mocked_open, \
2014-07-11 13:35:44 +00:00
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
2013-12-28 17:06:29 +00:00
mocked_get_thumbnail_folder.return_value = 'test'
mocked_exists.return_value = True
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# THEN: it should return two items for the titles and two empty strings for the notes
2013-12-30 08:35:05 +00:00
self.assertIs(type(result_titles), list, 'result_titles should be of type list')
self.assertEqual(len(result_titles), 2, 'There should be two items in the titles')
self.assertIs(type(result_notes), list, 'result_notes should be of type list')
self.assertEqual(len(result_notes), 2, 'There should be two items in the notes')
self.assertEqual(mocked_open.call_count, 3, 'Three files should be opened')
2013-12-28 17:06:29 +00:00
mocked_open.assert_any_call(os.path.join('test', 'titles.txt'))
mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'))
mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'))
2013-12-30 08:35:05 +00:00
self.assertEqual(mocked_exists.call_count, 3, 'Three files should have been checked')
2013-12-28 17:06:29 +00:00
def get_titles_and_notes_with_file_not_found_test(self):
"""
Test PresentationDocument.get_titles_and_notes method with file not found
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
with patch('builtins.open') as mocked_open, \
2014-07-11 13:35:44 +00:00
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
2013-12-28 17:06:29 +00:00
mocked_get_thumbnail_folder.return_value = 'test'
mocked_exists.return_value = False
2013-12-30 08:35:05 +00:00
2014-07-15 10:02:56 +00:00
# WHEN: calling get_titles_and_notes
2013-12-28 17:06:29 +00:00
result_titles, result_notes = self.document.get_titles_and_notes()
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# THEN: it should return two empty lists
2013-12-30 08:35:05 +00:00
self.assertIs(type(result_titles), list, 'result_titles should be of type list')
self.assertEqual(len(result_titles), 0, 'there be no titles')
self.assertIs(type(result_notes), list, 'result_notes should be a list')
self.assertEqual(len(result_notes), 0, 'but the list should be empty')
self.assertEqual(mocked_open.call_count, 0, 'No calls to open files')
self.assertEqual(mocked_exists.call_count, 1, 'There should be one call to file exists')
2013-12-28 17:06:29 +00:00
def get_titles_and_notes_with_file_error_test(self):
"""
Test PresentationDocument.get_titles_and_notes method with file errors
"""
# GIVEN: A mocked open, get_thumbnail_folder and exists
with patch('builtins.open') as mocked_open, \
2014-07-11 13:35:44 +00:00
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
2013-12-28 17:06:29 +00:00
mocked_get_thumbnail_folder.return_value = 'test'
mocked_exists.return_value = True
mocked_open.side_effect = IOError()
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
2013-12-30 08:35:05 +00:00
2013-12-28 17:06:29 +00:00
# THEN: it should return two empty lists
2013-12-30 08:35:05 +00:00
self.assertIs(type(result_titles), list, 'result_titles should be a list')