test fixes

This commit is contained in:
Philip Ridout 2017-09-17 20:43:15 +01:00
parent 8ed5903ced
commit 7f98003d54
12 changed files with 83 additions and 114 deletions

View File

@ -29,7 +29,7 @@ import os
import re import re
import math import math
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, Qt, QtWidgets
from openlp.core.common import translate from openlp.core.common import translate
from openlp.core.common.path import Path from openlp.core.common.path import Path
@ -221,12 +221,11 @@ def validate_thumb(file_path, thumb_path):
Validates whether an file's thumb still exists and if is up to date. **Note**, you must **not** call this function, Validates whether an file's thumb still exists and if is up to date. **Note**, you must **not** call this function,
before checking the existence of the file. before checking the existence of the file.
:param file_path: The path to the file. The file **must** exist! :param openlp.core.common.path.Path file_path: The path to the file. The file **must** exist!
:param thumb_path: The path to the thumb. :param openlp.core.common.path.Path thumb_path: The path to the thumb.
:return: True, False if the image has changed since the thumb was created. :return: Has the image changed since the thumb was created?
:rtype: bool
""" """
file_path = Path(file_path)
thumb_path = Path(thumb_path)
if not thumb_path.exists(): if not thumb_path.exists():
return False return False
image_date = file_path.stat().st_mtime image_date = file_path.stat().st_mtime

View File

@ -483,7 +483,7 @@ class ThemeManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ThemeManage
name = text_name name = text_name
thumb = os.path.join(self.thumb_path, '{name}.png'.format(name=text_name)) thumb = os.path.join(self.thumb_path, '{name}.png'.format(name=text_name))
item_name = QtWidgets.QListWidgetItem(name) item_name = QtWidgets.QListWidgetItem(name)
if validate_thumb(theme, thumb): if validate_thumb(Path(theme), Path(thumb)):
icon = build_icon(thumb) icon = build_icon(thumb)
else: else:
icon = create_thumb(theme, thumb) icon = create_thumb(theme, thumb)

View File

@ -360,7 +360,7 @@ class ImageMediaItem(MediaManagerItem):
if not os.path.exists(image_file.filename): if not os.path.exists(image_file.filename):
icon = build_icon(':/general/general_delete.png') icon = build_icon(':/general/general_delete.png')
else: else:
if validate_thumb(image_file.filename, thumb): if validate_thumb(Path(image_file.filename), Path(thumb)):
icon = build_icon(thumb) icon = build_icon(thumb)
else: else:
icon = create_thumb(image_file.filename, thumb) icon = create_thumb(image_file.filename, thumb)

View File

@ -197,7 +197,7 @@ class PresentationMediaItem(MediaManagerItem):
if not (preview_path and preview_path.exists()): if not (preview_path and preview_path.exists()):
icon = build_icon(':/general/general_delete.png') icon = build_icon(':/general/general_delete.png')
else: else:
if validate_thumb(preview_path, thumbnail_path): if validate_thumb(Path(preview_path), Path(thumbnail_path)):
icon = build_icon(thumbnail_path) icon = build_icon(thumbnail_path)
else: else:
icon = create_thumb(str(preview_path), str(thumbnail_path)) icon = create_thumb(str(preview_path), str(thumbnail_path))

View File

@ -261,7 +261,8 @@ class PdfDocument(PresentationDocument):
# The %03d in the file name is handled by each binary # The %03d in the file name is handled by each binary
if self.controller.mudrawbin: if self.controller.mudrawbin:
log.debug('loading presentation using mudraw') log.debug('loading presentation using mudraw')
runlog = check_output([str(self.controller.mudrawbin), '-w', str(size.width()), '-h', str(size.height()), runlog = check_output([str(self.controller.mudrawbin), '-w', str(size.width()),
'-h', str(size.height()),
'-o', str(temp_dir_path / 'mainslide%03d.png'), str(self.file_path)], '-o', str(temp_dir_path / 'mainslide%03d.png'), str(self.file_path)],
startupinfo=self.startupinfo) startupinfo=self.startupinfo)
elif self.controller.mutoolbin: elif self.controller.mutoolbin:

View File

@ -168,7 +168,7 @@ class PresentationDocument(object):
last_image_path = self.get_thumbnail_path(self.get_slide_count(), True) last_image_path = self.get_thumbnail_path(self.get_slide_count(), True)
if not (last_image_path and last_image_path.is_file()): if not (last_image_path and last_image_path.is_file()):
return False return False
return validate_thumb(self.file_path, last_image_path) return validate_thumb(Path(self.file_path), Path(last_image_path))
def close_presentation(self): def close_presentation(self):
""" """

View File

@ -595,61 +595,46 @@ class TestLib(TestCase):
Test the validate_thumb() function when the thumbnail does not exist Test the validate_thumb() function when the thumbnail does not exist
""" """
# GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb # GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb
with patch('openlp.core.lib.os') as mocked_os: with patch.object(Path, 'exists', return_value=False) as mocked_path_exists:
file_path = 'path/to/file' file_path = Path('path', 'to', 'file')
thumb_path = 'path/to/thumb' thumb_path = Path('path', 'to', 'thumb')
mocked_os.path.exists.return_value = False
# WHEN: we run the validate_thumb() function # WHEN: we run the validate_thumb() function
result = validate_thumb(file_path, thumb_path) result = validate_thumb(file_path, thumb_path)
# THEN: we should have called a few functions, and the result should be False # THEN: we should have called a few functions, and the result should be False
mocked_os.path.exists.assert_called_with(thumb_path) thumb_path.exists.assert_called_once_with()
assert result is False, 'The result should be False' self.assertFalse(result, 'The result should be False')
def test_validate_thumb_file_exists_and_newer(self): def test_validate_thumb_file_exists_and_newer(self):
""" """
Test the validate_thumb() function when the thumbnail exists and has a newer timestamp than the file Test the validate_thumb() function when the thumbnail exists and has a newer timestamp than the file
""" """
# GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb with patch.object(Path, 'exists'), patch.object(Path, 'stat'):
with patch('openlp.core.lib.os') as mocked_os: # GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
file_path = 'path/to/file' file_path = MagicMock(**{'stat.return_value': MagicMock(st_mtime=10)})
thumb_path = 'path/to/thumb' thumb_path = MagicMock(**{'exists.return_value': True, 'stat.return_value': MagicMock(st_mtime=11)})
file_mocked_stat = MagicMock()
file_mocked_stat.st_mtime = datetime.now()
thumb_mocked_stat = MagicMock()
thumb_mocked_stat.st_mtime = datetime.now() + timedelta(seconds=10)
mocked_os.path.exists.return_value = True
mocked_os.stat.side_effect = [file_mocked_stat, thumb_mocked_stat]
# WHEN: we run the validate_thumb() function # WHEN: we run the validate_thumb() function
result = validate_thumb(file_path, thumb_path)
# THEN: we should have called a few functions, and the result should be True # THEN: `validate_thumb` should return True
# mocked_os.path.exists.assert_called_with(thumb_path) self.assertTrue(result)
def test_validate_thumb_file_exists_and_older(self): def test_validate_thumb_file_exists_and_older(self):
""" """
Test the validate_thumb() function when the thumbnail exists but is older than the file Test the validate_thumb() function when the thumbnail exists but is older than the file
""" """
# GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb # GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
with patch('openlp.core.lib.os') as mocked_os: file_path = MagicMock(**{'stat.return_value': MagicMock(st_mtime=10)})
file_path = 'path/to/file' thumb_path = MagicMock(**{'exists.return_value': True, 'stat.return_value': MagicMock(st_mtime=9)})
thumb_path = 'path/to/thumb'
file_mocked_stat = MagicMock()
file_mocked_stat.st_mtime = datetime.now()
thumb_mocked_stat = MagicMock()
thumb_mocked_stat.st_mtime = datetime.now() - timedelta(seconds=10)
mocked_os.path.exists.return_value = True
mocked_os.stat.side_effect = lambda fname: file_mocked_stat if fname == file_path else thumb_mocked_stat
# WHEN: we run the validate_thumb() function # WHEN: we run the validate_thumb() function
result = validate_thumb(file_path, thumb_path) result = validate_thumb(file_path, thumb_path)
# THEN: we should have called a few functions, and the result should be False # THEN: `validate_thumb` should return False
mocked_os.path.exists.assert_called_with(thumb_path) thumb_path.stat.assert_called_once_with()
mocked_os.stat.assert_any_call(file_path) self.assertFalse(result, 'The result should be False')
mocked_os.stat.assert_any_call(thumb_path)
assert result is False, 'The result should be False'
def test_replace_params_no_params(self): def test_replace_params_no_params(self):
""" """

View File

@ -24,13 +24,12 @@ Functional tests to test the Impress class and related methods.
""" """
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock from unittest.mock import MagicMock
import os
import shutil import shutil
from tempfile import mkdtemp from tempfile import mkdtemp
from openlp.core.common import Settings from openlp.core.common import Settings
from openlp.plugins.presentations.lib.impresscontroller import \ from openlp.core.common.path import Path
ImpressController, ImpressDocument, TextType from openlp.plugins.presentations.lib.impresscontroller import ImpressController, ImpressDocument, TextType
from openlp.plugins.presentations.presentationplugin import __default_settings__ from openlp.plugins.presentations.presentationplugin import __default_settings__
from tests.utils.constants import TEST_RESOURCES_PATH from tests.utils.constants import TEST_RESOURCES_PATH
@ -82,7 +81,7 @@ class TestImpressDocument(TestCase):
mocked_plugin = MagicMock() mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations' mocked_plugin.settings_section = 'presentations'
Settings().extend_default_settings(__default_settings__) Settings().extend_default_settings(__default_settings__)
self.file_name = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx') self.file_name = Path(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
self.ppc = ImpressController(mocked_plugin) self.ppc = ImpressController(mocked_plugin)
self.doc = ImpressDocument(self.ppc, self.file_name) self.doc = ImpressDocument(self.ppc, self.file_name)

View File

@ -26,6 +26,7 @@ from unittest import TestCase
from unittest.mock import patch, MagicMock, call from unittest.mock import patch, MagicMock, call
from openlp.core.common import Registry from openlp.core.common import Registry
from openlp.core.common.path import Path
from openlp.plugins.presentations.lib.mediaitem import PresentationMediaItem from openlp.plugins.presentations.lib.mediaitem import PresentationMediaItem
from tests.helpers.testmixin import TestMixin from tests.helpers.testmixin import TestMixin
@ -92,17 +93,18 @@ class TestMediaItem(TestCase, TestMixin):
""" """
# GIVEN: A mocked controller, and mocked os.path.getmtime # GIVEN: A mocked controller, and mocked os.path.getmtime
mocked_controller = MagicMock() mocked_controller = MagicMock()
mocked_doc = MagicMock() mocked_doc = MagicMock(**{'get_thumbnail_path.return_value': Path()})
mocked_controller.add_document.return_value = mocked_doc mocked_controller.add_document.return_value = mocked_doc
mocked_controller.supports = ['tmp'] mocked_controller.supports = ['tmp']
self.media_item.controllers = { self.media_item.controllers = {
'Mocked': mocked_controller 'Mocked': mocked_controller
} }
presentation_file = 'file.tmp'
with patch('openlp.plugins.presentations.lib.mediaitem.os.path.getmtime') as mocked_getmtime, \ thmub_path = MagicMock(st_mtime=100)
patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists: file_path = MagicMock(st_mtime=400)
mocked_getmtime.side_effect = [100, 200] with patch.object(Path, 'stat', side_effect=[thmub_path, file_path]), \
mocked_exists.return_value = True patch.object(Path, 'exists', return_value=True):
presentation_file = Path('file.tmp')
# WHEN: calling clean_up_thumbnails # WHEN: calling clean_up_thumbnails
self.media_item.clean_up_thumbnails(presentation_file, True) self.media_item.clean_up_thumbnails(presentation_file, True)
@ -123,9 +125,8 @@ class TestMediaItem(TestCase, TestMixin):
self.media_item.controllers = { self.media_item.controllers = {
'Mocked': mocked_controller 'Mocked': mocked_controller
} }
presentation_file = 'file.tmp' presentation_file = Path('file.tmp')
with patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists: with patch.object(Path, 'exists', return_value=False):
mocked_exists.return_value = False
# WHEN: calling clean_up_thumbnails # WHEN: calling clean_up_thumbnails
self.media_item.clean_up_thumbnails(presentation_file, True) self.media_item.clean_up_thumbnails(presentation_file, True)

View File

@ -32,6 +32,7 @@ from PyQt5 import QtCore, QtGui
from openlp.plugins.presentations.lib.pdfcontroller import PdfController, PdfDocument from openlp.plugins.presentations.lib.pdfcontroller import PdfController, PdfDocument
from openlp.core.common import Settings from openlp.core.common import Settings
from openlp.core.common.path import Path
from openlp.core.lib import ScreenList from openlp.core.lib import ScreenList
from tests.utils.constants import TEST_RESOURCES_PATH from tests.utils.constants import TEST_RESOURCES_PATH
@ -66,8 +67,8 @@ class TestPdfController(TestCase, TestMixin):
self.desktop.screenGeometry.return_value = SCREEN['size'] self.desktop.screenGeometry.return_value = SCREEN['size']
self.screens = ScreenList.create(self.desktop) self.screens = ScreenList.create(self.desktop)
Settings().extend_default_settings(__default_settings__) Settings().extend_default_settings(__default_settings__)
self.temp_folder = mkdtemp() self.temp_folder = Path(mkdtemp())
self.thumbnail_folder = mkdtemp() self.thumbnail_folder = Path(mkdtemp())
self.mock_plugin = MagicMock() self.mock_plugin = MagicMock()
self.mock_plugin.settings_section = self.temp_folder self.mock_plugin.settings_section = self.temp_folder
@ -77,8 +78,8 @@ class TestPdfController(TestCase, TestMixin):
""" """
del self.screens del self.screens
self.destroy_settings() self.destroy_settings()
shutil.rmtree(self.thumbnail_folder) shutil.rmtree(str(self.thumbnail_folder))
shutil.rmtree(self.temp_folder) shutil.rmtree(str(self.temp_folder))
def test_constructor(self): def test_constructor(self):
""" """
@ -98,7 +99,7 @@ class TestPdfController(TestCase, TestMixin):
Test loading of a Pdf using the PdfController Test loading of a Pdf using the PdfController
""" """
# GIVEN: A Pdf-file # GIVEN: A Pdf-file
test_file = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'pdf_test1.pdf') test_file = Path(TEST_RESOURCES_PATH, 'presentations', 'pdf_test1.pdf')
# WHEN: The Pdf is loaded # WHEN: The Pdf is loaded
controller = PdfController(plugin=self.mock_plugin) controller = PdfController(plugin=self.mock_plugin)
@ -118,7 +119,7 @@ class TestPdfController(TestCase, TestMixin):
Test loading of a Pdf and check size of generate pictures Test loading of a Pdf and check size of generate pictures
""" """
# GIVEN: A Pdf-file # GIVEN: A Pdf-file
test_file = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'pdf_test1.pdf') test_file = Path(TEST_RESOURCES_PATH, 'presentations', 'pdf_test1.pdf')
# WHEN: The Pdf is loaded # WHEN: The Pdf is loaded
controller = PdfController(plugin=self.mock_plugin) controller = PdfController(plugin=self.mock_plugin)
@ -131,7 +132,7 @@ class TestPdfController(TestCase, TestMixin):
# THEN: The load should succeed and pictures should be created and have been scales to fit the screen # THEN: The load should succeed and pictures should be created and have been scales to fit the screen
self.assertTrue(loaded, 'The loading of the PDF should succeed.') self.assertTrue(loaded, 'The loading of the PDF should succeed.')
image = QtGui.QImage(os.path.join(self.temp_folder, 'pdf_test1.pdf', 'mainslide001.png')) image = QtGui.QImage(os.path.join(str(self.temp_folder), 'pdf_test1.pdf', 'mainslide001.png'))
# Based on the converter used the resolution will differ a bit # Based on the converter used the resolution will differ a bit
if controller.gsbin: if controller.gsbin:
self.assertEqual(760, image.height(), 'The height should be 760') self.assertEqual(760, image.height(), 'The height should be 760')

View File

@ -22,7 +22,6 @@
""" """
This module contains tests for the pptviewcontroller module of the Presentations plugin. This module contains tests for the pptviewcontroller module of the Presentations plugin.
""" """
import os
import shutil import shutil
from tempfile import mkdtemp from tempfile import mkdtemp
from unittest import TestCase from unittest import TestCase
@ -30,6 +29,7 @@ from unittest.mock import MagicMock, patch
from openlp.plugins.presentations.lib.pptviewcontroller import PptviewDocument, PptviewController from openlp.plugins.presentations.lib.pptviewcontroller import PptviewDocument, PptviewController
from openlp.core.common import is_win from openlp.core.common import is_win
from openlp.core.common.path import Path
from tests.helpers.testmixin import TestMixin from tests.helpers.testmixin import TestMixin
from tests.utils.constants import TEST_RESOURCES_PATH from tests.utils.constants import TEST_RESOURCES_PATH
@ -184,7 +184,7 @@ class TestPptviewDocument(TestCase):
""" """
# GIVEN: mocked PresentationController.save_titles_and_notes and a pptx file # GIVEN: mocked PresentationController.save_titles_and_notes and a pptx file
doc = PptviewDocument(self.mock_controller, self.mock_presentation) doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx') doc.file_path = Path(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
doc.save_titles_and_notes = MagicMock() doc.save_titles_and_notes = MagicMock()
# WHEN reading the titles and notes # WHEN reading the titles and notes
@ -201,13 +201,13 @@ class TestPptviewDocument(TestCase):
""" """
# GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file # GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file
with patch('builtins.open') as mocked_open, \ with patch('builtins.open') as mocked_open, \
patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.exists') as mocked_exists, \ patch.object(Path, 'exists') as mocked_path_exists, \
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \ patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \
mocked_dir_exists: mocked_dir_exists:
mocked_exists.return_value = False mocked_path_exists.return_value = False
mocked_dir_exists.return_value = False mocked_dir_exists.return_value = False
doc = PptviewDocument(self.mock_controller, self.mock_presentation) doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = 'Idontexist.pptx' doc.file_path = Path('Idontexist.pptx')
doc.save_titles_and_notes = MagicMock() doc.save_titles_and_notes = MagicMock()
# WHEN: Reading the titles and notes # WHEN: Reading the titles and notes
@ -215,7 +215,7 @@ class TestPptviewDocument(TestCase):
# THEN: File existens should have been checked, and not have been opened. # THEN: File existens should have been checked, and not have been opened.
doc.save_titles_and_notes.assert_called_once_with(None, None) doc.save_titles_and_notes.assert_called_once_with(None, None)
mocked_exists.assert_any_call('Idontexist.pptx') mocked_path_exists.assert_called_with()
self.assertEqual(mocked_open.call_count, 0, 'There should be no calls to open a file.') self.assertEqual(mocked_open.call_count, 0, 'There should be no calls to open a file.')
def test_create_titles_and_notes_invalid_file(self): def test_create_titles_and_notes_invalid_file(self):
@ -228,7 +228,7 @@ class TestPptviewDocument(TestCase):
mocked_is_zf.return_value = False mocked_is_zf.return_value = False
mocked_open.filesize = 10 mocked_open.filesize = 10
doc = PptviewDocument(self.mock_controller, self.mock_presentation) doc = PptviewDocument(self.mock_controller, self.mock_presentation)
doc.file_path = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.ppt') doc.file_path = Path(TEST_RESOURCES_PATH, 'presentations', 'test.ppt')
doc.save_titles_and_notes = MagicMock() doc.save_titles_and_notes = MagicMock()
# WHEN: reading the titles and notes # WHEN: reading the titles and notes

View File

@ -23,9 +23,8 @@
Functional tests to test the PresentationController and PresentationDocument Functional tests to test the PresentationController and PresentationDocument
classes and related methods. classes and related methods.
""" """
import os
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock, mock_open, patch from unittest.mock import MagicMock, call, patch
from openlp.core.common.path import Path from openlp.core.common.path import Path
from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument
@ -67,23 +66,18 @@ class TestPresentationController(TestCase):
Test PresentationDocument.save_titles_and_notes method with two valid lists 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 # GIVEN: two lists of length==2 and a mocked open and get_thumbnail_folder
mocked_open = mock_open() with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.write_text') as mocked_write_text, \
with patch('builtins.open', mocked_open), patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder: patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
titles = ['uno', 'dos'] titles = ['uno', 'dos']
notes = ['one', 'two'] notes = ['one', 'two']
# WHEN: calling save_titles_and_notes # WHEN: calling save_titles_and_notes
mocked_get_thumbnail_folder.return_value = 'test' mocked_get_thumbnail_folder.return_value = Path('test')
self.document.save_titles_and_notes(titles, notes) self.document.save_titles_and_notes(titles, notes)
# THEN: the last call to open should have been for slideNotes2.txt # THEN: the last call to open should have been for slideNotes2.txt
mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), mode='wt', encoding='utf-8') self.assertEqual(mocked_write_text.call_count, 3, 'There should be exactly three files written')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), mode='wt', encoding='utf-8') mocked_write_text.assert_has_calls([call('uno\ndos'), call('one'), call('two')])
mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), mode='wt', encoding='utf-8')
self.assertEqual(mocked_open.call_count, 3, 'There should be exactly three files opened')
mocked_open().writelines.assert_called_once_with(['uno', 'dos'])
mocked_open().write.assert_any_call('one')
mocked_open().write.assert_any_call('two')
def test_save_titles_and_notes_with_None(self): def test_save_titles_and_notes_with_None(self):
""" """
@ -107,10 +101,11 @@ class TestPresentationController(TestCase):
""" """
# GIVEN: A mocked open, get_thumbnail_folder and exists # GIVEN: A mocked open, get_thumbnail_folder and exists
with patch('builtins.open', mock_open(read_data='uno\ndos\n')) as mocked_open, \ with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text',
return_value='uno\ndos\n') as mocked_read_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \ patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists: patch('openlp.plugins.presentations.lib.presentationcontroller.Path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = 'test' mocked_get_thumbnail_folder.return_value = Path('test')
mocked_exists.return_value = True mocked_exists.return_value = True
# WHEN: calling get_titles_and_notes # WHEN: calling get_titles_and_notes
@ -121,45 +116,36 @@ class TestPresentationController(TestCase):
self.assertEqual(len(result_titles), 2, 'There should be two items in the titles') 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.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(len(result_notes), 2, 'There should be two items in the notes')
self.assertEqual(mocked_open.call_count, 3, 'Three files should be opened') self.assertEqual(mocked_read_text.call_count, 3, 'Three files should be read')
mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), encoding='utf-8')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), encoding='utf-8')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), encoding='utf-8')
self.assertEqual(mocked_exists.call_count, 3, 'Three files should have been checked')
def test_get_titles_and_notes_with_file_not_found(self): def test_get_titles_and_notes_with_file_not_found(self):
""" """
Test PresentationDocument.get_titles_and_notes method with file not found Test PresentationDocument.get_titles_and_notes method with file not found
""" """
# GIVEN: A mocked open, get_thumbnail_folder and exists # GIVEN: A mocked open, get_thumbnail_folder and exists
with patch('builtins.open') as mocked_open, \ with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text') as mocked_read_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \ patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists: mocked_read_text.side_effect = FileNotFoundError()
mocked_get_thumbnail_folder.return_value = 'test' mocked_get_thumbnail_folder.return_value = Path('test')
mocked_exists.return_value = False
# WHEN: calling get_titles_and_notes # WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes() result_titles, result_notes = self.document.get_titles_and_notes()
# THEN: it should return two empty lists # THEN: it should return two empty lists
self.assertIs(type(result_titles), list, 'result_titles should be of type list') self.assertIsInstance(result_titles, list, 'result_titles should be of type list')
self.assertEqual(len(result_titles), 0, 'there be no titles') self.assertEqual(len(result_titles), 0, 'there be no titles')
self.assertIs(type(result_notes), list, 'result_notes should be a list') self.assertIsInstance(result_notes, list, 'result_notes should be a list')
self.assertEqual(len(result_notes), 0, 'but the list should be empty') 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')
def test_get_titles_and_notes_with_file_error(self): def test_get_titles_and_notes_with_file_error(self):
""" """
Test PresentationDocument.get_titles_and_notes method with file errors Test PresentationDocument.get_titles_and_notes method with file errors
""" """
# GIVEN: A mocked open, get_thumbnail_folder and exists # GIVEN: A mocked open, get_thumbnail_folder and exists
with patch('builtins.open') as mocked_open, \ with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.read_text') as mocked_read_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \ patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists: mocked_read_text.side_effect = IOError()
mocked_get_thumbnail_folder.return_value = 'test' mocked_get_thumbnail_folder.return_value = Path('test')
mocked_exists.return_value = True
mocked_open.side_effect = IOError()
# WHEN: calling get_titles_and_notes # WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes() result_titles, result_notes = self.document.get_titles_and_notes()
@ -180,18 +166,16 @@ class TestPresentationDocument(TestCase):
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists')
self.get_thumbnail_folder_patcher = \ self.get_thumbnail_folder_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder') patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder')
self.os_patcher = patch('openlp.plugins.presentations.lib.presentationcontroller.os')
self._setup_patcher = \ self._setup_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument._setup') patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument._setup')
self.mock_check_directory_exists = self.check_directory_exists_patcher.start() self.mock_check_directory_exists = self.check_directory_exists_patcher.start()
self.mock_get_thumbnail_folder = self.get_thumbnail_folder_patcher.start() self.mock_get_thumbnail_folder = self.get_thumbnail_folder_patcher.start()
self.mock_os = self.os_patcher.start()
self.mock_setup = self._setup_patcher.start() self.mock_setup = self._setup_patcher.start()
self.mock_controller = MagicMock() self.mock_controller = MagicMock()
self.mock_get_thumbnail_folder.return_value = 'returned/path/' self.mock_get_thumbnail_folder.return_value = Path('returned/path/')
def tearDown(self): def tearDown(self):
""" """
@ -199,7 +183,6 @@ class TestPresentationDocument(TestCase):
""" """
self.check_directory_exists_patcher.stop() self.check_directory_exists_patcher.stop()
self.get_thumbnail_folder_patcher.stop() self.get_thumbnail_folder_patcher.stop()
self.os_patcher.stop()
self._setup_patcher.stop() self._setup_patcher.stop()
def test_initialise_presentation_document(self): def test_initialise_presentation_document(self):
@ -227,7 +210,7 @@ class TestPresentationDocument(TestCase):
PresentationDocument(self.mock_controller, 'Name') PresentationDocument(self.mock_controller, 'Name')
# THEN: check_directory_exists should have been called with 'returned/path/' # THEN: check_directory_exists should have been called with 'returned/path/'
self.mock_check_directory_exists.assert_called_once_with(Path('returned', 'path')) self.mock_check_directory_exists.assert_called_once_with(Path('returned', 'path/'))
self._setup_patcher.start() self._setup_patcher.start()