Merged test_powerpointviewercontroller.py into test_pptviewcontroller.py

This commit is contained in:
Tomas Groth 2014-07-14 16:15:08 +02:00
parent 8fa23ac95d
commit 88fc41a5eb
5 changed files with 70 additions and 160 deletions

View File

@ -43,7 +43,7 @@ class TestLibModule(TestCase):
def setUp(self):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
self.file_name = os.path.join(TEST_PATH, 'test.pptx')
self.file_name = os.path.join(TEST_PATH, 'presentations', 'test.pptx')
self.ppc = ImpressController(mocked_plugin)
self.doc = ImpressDocument(self.ppc, self.file_name)

View File

@ -107,7 +107,7 @@ class TestPowerpointDocument(TestCase, TestMixin):
self.mock_controller = MagicMock()
self.mock_presentation = MagicMock()
self.mock_presentation_document_get_temp_folder.return_value = 'temp folder'
self.file_name = os.path.join(TEST_RESOURCES_PATH, "test.pptx")
self.file_name = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
self.real_controller = PowerpointController(self.mock_plugin)
def tearDown(self):

View File

@ -1,151 +0,0 @@
# -*- 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 #
###############################################################################
"""
Functional tests to test the PptviewController class and related methods.
"""
from unittest import TestCase, SkipTest
import os
if os.name != 'nt':
raise SkipTest('Not Windows, skipping test')
from mock import MagicMock, patch, mock_open
from openlp.plugins.presentations.lib.pptviewcontroller import PptviewController, PptviewDocument
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources'))
class TestLibModule(TestCase):
def setUp(self):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
self.ppc = PptviewController(mocked_plugin)
self.file_name = os.path.join(TEST_PATH, "test.pptx")
self.doc = PptviewDocument(self.ppc, self.file_name)
#add _test to the function name to enable test
def verify_installation(self):
"""
Test the installation of PowerpointViewer
"""
# GIVEN: A boolean value set to true
# WHEN: We "convert" it to a bool
is_installed = self.ppc.check_available()
# THEN: We should get back a True bool
self.assertEqual(is_installed, True, 'The result should be True')
# add _test to the following if necessary to enable test
# I don't have powerpointviewer to verify
def verify_loading_document(self):
"""
Test loading a document in PowerpointViewer
"""
# GIVEN: the filename
print(self.file_name)
# WHEN: loading the filename
self.doc = PptviewDocument(self.ppc, self.file_name)
self.doc.load_presentation()
result = self.doc.is_loaded()
# THEN: result should be true
self.assertEqual(result, True, 'The result should be True')
# disabled
def verify_titles(self):
"""
Test reading the titles from PowerpointViewer
"""
# GIVEN:
self.doc = PptviewDocument(self.ppc, self.file_name)
self.doc.create_titles_and_notes()
# WHEN reading the titles and notes
titles, notes = self.doc.get_titles_and_notes()
print("titles: ".join(titles))
print("notes: ".join(notes))
# THEN there should be exactly 5 titles and 5 notes
self.assertEqual(len(titles), 5, 'There should be five titles')
self.assertEqual(len(notes), 5, 'There should be five notes')
def create_titles_and_notes_test(self):
"""
Test PowerpointController.create_titles_and_notes
"""
# GIVEN: mocked PresentationController.save_titles_and_notes
self.doc.save_titles_and_notes = MagicMock()
# WHEN reading the titles and notes
self.doc.create_titles_and_notes()
# THEN save_titles_and_notes should have been called once with empty arrays
self.doc.save_titles_and_notes.assert_called_once_with(['Test 1\n', '\n', 'Test 2\n', 'Test 4\n', 'Test 3\n'],
['Notes for slide 1', 'Inserted', 'Notes for slide 2',
'Notes \nfor slide 4', 'Notes for slide 3'])
def create_titles_and_notes_nonexistent_file_test(self):
"""
Test PowerpointController.create_titles_and_notes with nonexistent file
"""
# GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file
with patch('builtins.open') as mocked_open, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.exists') as mocked_exists, \
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \
mocked_dir_exists:
mocked_exists.return_value = False
mocked_dir_exists.return_value = False
self.doc = PptviewDocument(self.ppc, 'Idontexist.pptx')
self.doc.save_titles_and_notes = MagicMock()
# WHEN: reading the titles and notes
self.doc.create_titles_and_notes()
# THEN:
self.doc.save_titles_and_notes.assert_called_once_with(None, None)
mocked_exists.assert_any_call('Idontexist.pptx')
self.assertEqual(mocked_open.call_count, 0, 'There should be no calls to open a file')
def create_titles_and_notes_invalid_file_test(self):
"""
Test PowerpointController.create_titles_and_notes with invalid file
"""
# GIVEN: mocked PresentationController.save_titles_and_notes and an invalid file
with patch('builtins.open', mock_open(read_data='this is a test')) as mocked_open, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.exists') as mocked_exists, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.zipfile.is_zipfile') as mocked_is_zf:
mocked_is_zf.return_value = False
mocked_exists.return_value = True
mocked_open.filesize = 10
self.doc = PptviewDocument(self.ppc, os.path.join(TEST_PATH, "test.ppt"))
self.doc.save_titles_and_notes = MagicMock()
# WHEN: reading the titles and notes
self.doc.create_titles_and_notes()
# THEN:
self.doc.save_titles_and_notes.assert_called_once_with(None, None)
self.assertEqual(mocked_is_zf.call_count, 1, 'is_zipfile should have been called once')

View File

@ -39,6 +39,7 @@ from unittest import TestCase
from tests.functional import MagicMock, patch
from tests.helpers.testmixin import TestMixin
from tests.utils.constants import TEST_RESOURCES_PATH
from openlp.plugins.presentations.lib.pptviewcontroller import PptviewDocument, PptviewController
@ -130,7 +131,7 @@ class TestPptviewDocument(TestCase):
"""
Set up the patches and mocks need for all tests.
"""
self.os_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.os')
self.os_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.isdir')
self.pptview_document_create_thumbnails_patcher = patch(
'openlp.plugins.presentations.lib.pptviewcontroller.PptviewDocument.create_thumbnails')
self.pptview_document_stop_presentation_patcher = patch(
@ -141,7 +142,6 @@ class TestPptviewDocument(TestCase):
'openlp.plugins.presentations.lib.pptviewcontroller.PresentationDocument._setup')
self.screen_list_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.ScreenList')
self.rect_patcher = MagicMock()
self.mock_os = self.os_patcher.start()
self.mock_pptview_document_create_thumbnails = self.pptview_document_create_thumbnails_patcher.start()
self.mock_pptview_document_stop_presentation = self.pptview_document_stop_presentation_patcher.start()
@ -149,11 +149,10 @@ class TestPptviewDocument(TestCase):
self.mock_presentation_document_setup = self.presentation_document_setup_patcher.start()
self.mock_rect = self.rect_patcher.start()
self.mock_screen_list = self.screen_list_patcher.start()
self.mock_controller = MagicMock()
self.mock_presentation = MagicMock()
self.mock_presentation_document_get_temp_folder.return_value = 'temp folder'
self.temp_folder = mkdtemp()
self.mock_presentation_document_get_temp_folder.return_value = self.temp_folder
def tearDown(self):
"""
@ -166,6 +165,7 @@ class TestPptviewDocument(TestCase):
self.presentation_document_setup_patcher.stop()
self.rect_patcher.stop()
self.screen_list_patcher.stop()
shutil.rmtree(self.temp_folder)
def load_presentation_succesfull_test(self):
"""
@ -175,7 +175,7 @@ class TestPptviewDocument(TestCase):
self.mock_os.reset()
# WHEN: The temporary directory exists and OpenPPT returns successfully (not -1)
self.mock_os.path.isdir.return_value = True
self.mock_os.return_value = True
self.mock_controller.process.OpenPPT.return_value = 0
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
instance.file_path = 'test\path.ppt'
@ -195,7 +195,7 @@ class TestPptviewDocument(TestCase):
self.mock_os.reset()
# WHEN: The temporary directory does not exist and OpenPPT returns unsuccessfully (-1)
self.mock_os.path.isdir.return_value = False
self.mock_os.return_value = False
self.mock_controller.process.OpenPPT.return_value = -1
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
instance.file_path = 'test\path.ppt'
@ -205,3 +205,64 @@ class TestPptviewDocument(TestCase):
# THEN: The temporary directory should be created and PptviewDocument.load_presentation should return False
self.mock_os.makedirs.assert_called_once_with('temp folder')
self.assertFalse(result)
def create_titles_and_notes_test(self):
"""
Test PowerpointController.create_titles_and_notes
"""
# GIVEN: mocked PresentationController.save_titles_and_notes and a pptx file
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
doc.save_titles_and_notes = MagicMock()
# WHEN reading the titles and notes
doc.create_titles_and_notes()
# THEN save_titles_and_notes should have been called once with empty arrays
doc.save_titles_and_notes.assert_called_once_with(['Test 1\n', '\n', 'Test 2\n', 'Test 4\n', 'Test 3\n'],
['Notes for slide 1', 'Inserted', 'Notes for slide 2',
'Notes \nfor slide 4', 'Notes for slide 3'])
def create_titles_and_notes_nonexistent_file_test(self):
"""
Test PowerpointController.create_titles_and_notes with nonexistent file
"""
# GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file
with patch('builtins.open') as mocked_open, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.exists') as mocked_exists, \
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \
mocked_dir_exists:
mocked_exists.return_value = False
mocked_dir_exists.return_value = False
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = 'Idontexist.pptx'
doc.save_titles_and_notes = MagicMock()
# WHEN: Reading the titles and notes
doc.create_titles_and_notes()
# THEN: File existens should have been checked, and not have been opened.
doc.save_titles_and_notes.assert_called_once_with(None, None)
mocked_exists.assert_any_call('Idontexist.pptx')
self.assertEqual(mocked_open.call_count, 0, 'There should be no calls to open a file.')
def create_titles_and_notes_invalid_file_test(self):
"""
Test PowerpointController.create_titles_and_notes with invalid file
"""
# GIVEN: mocked PresentationController.save_titles_and_notes and an invalid file
with patch('builtins.open') as mocked_open, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.zipfile.is_zipfile') as mocked_is_zf:
mocked_is_zf.return_value = False
#mocked_exists.return_value = True
mocked_open.filesize = 10
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.ppt')
doc.save_titles_and_notes = MagicMock()
# WHEN: reading the titles and notes
doc.create_titles_and_notes()
# THEN:
doc.save_titles_and_notes.assert_called_once_with(None, None)
self.assertEqual(mocked_is_zf.call_count, 1, 'is_zipfile should have been called once')