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 math
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtGui, Qt, QtWidgets
from openlp.core.common import translate
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,
before checking the existence of the file.
:param file_path: The path to the file. The file **must** exist!
:param thumb_path: The path to the thumb.
:return: True, False if the image has changed since the thumb was created.
:param openlp.core.common.path.Path file_path: The path to the file. The file **must** exist!
:param openlp.core.common.path.Path thumb_path: The path to the thumb.
: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():
return False
image_date = file_path.stat().st_mtime

View File

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

View File

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

View File

@ -197,7 +197,7 @@ class PresentationMediaItem(MediaManagerItem):
if not (preview_path and preview_path.exists()):
icon = build_icon(':/general/general_delete.png')
else:
if validate_thumb(preview_path, thumbnail_path):
if validate_thumb(Path(preview_path), Path(thumbnail_path)):
icon = build_icon(thumbnail_path)
else:
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
if self.controller.mudrawbin:
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)],
startupinfo=self.startupinfo)
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)
if not (last_image_path and last_image_path.is_file()):
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):
"""

View File

@ -595,61 +595,46 @@ class TestLib(TestCase):
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
with patch('openlp.core.lib.os') as mocked_os:
file_path = 'path/to/file'
thumb_path = 'path/to/thumb'
mocked_os.path.exists.return_value = False
with patch.object(Path, 'exists', return_value=False) as mocked_path_exists:
file_path = Path('path', 'to', 'file')
thumb_path = Path('path', 'to', 'thumb')
# 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 False
mocked_os.path.exists.assert_called_with(thumb_path)
assert result is False, 'The result should be False'
thumb_path.exists.assert_called_once_with()
self.assertFalse(result, 'The result should be False')
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
"""
# GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb
with patch('openlp.core.lib.os') as mocked_os:
file_path = 'path/to/file'
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 = [file_mocked_stat, thumb_mocked_stat]
with patch.object(Path, 'exists'), patch.object(Path, 'stat'):
# GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
file_path = MagicMock(**{'stat.return_value': MagicMock(st_mtime=10)})
thumb_path = MagicMock(**{'exists.return_value': True, 'stat.return_value': MagicMock(st_mtime=11)})
# 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
# mocked_os.path.exists.assert_called_with(thumb_path)
# THEN: `validate_thumb` should return True
self.assertTrue(result)
def test_validate_thumb_file_exists_and_older(self):
"""
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
with patch('openlp.core.lib.os') as mocked_os:
file_path = 'path/to/file'
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
# GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
file_path = MagicMock(**{'stat.return_value': MagicMock(st_mtime=10)})
thumb_path = MagicMock(**{'exists.return_value': True, 'stat.return_value': MagicMock(st_mtime=9)})
# WHEN: we run the validate_thumb() function
result = validate_thumb(file_path, thumb_path)
# 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 False
mocked_os.path.exists.assert_called_with(thumb_path)
mocked_os.stat.assert_any_call(file_path)
mocked_os.stat.assert_any_call(thumb_path)
assert result is False, 'The result should be False'
# THEN: `validate_thumb` should return False
thumb_path.stat.assert_called_once_with()
self.assertFalse(result, 'The result should be False')
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.mock import MagicMock
import os
import shutil
from tempfile import mkdtemp
from openlp.core.common import Settings
from openlp.plugins.presentations.lib.impresscontroller import \
ImpressController, ImpressDocument, TextType
from openlp.core.common.path import Path
from openlp.plugins.presentations.lib.impresscontroller import ImpressController, ImpressDocument, TextType
from openlp.plugins.presentations.presentationplugin import __default_settings__
from tests.utils.constants import TEST_RESOURCES_PATH
@ -82,7 +81,7 @@ class TestImpressDocument(TestCase):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
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.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 openlp.core.common import Registry
from openlp.core.common.path import Path
from openlp.plugins.presentations.lib.mediaitem import PresentationMediaItem
from tests.helpers.testmixin import TestMixin
@ -92,17 +93,18 @@ class TestMediaItem(TestCase, TestMixin):
"""
# GIVEN: A mocked controller, and mocked os.path.getmtime
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.supports = ['tmp']
self.media_item.controllers = {
'Mocked': mocked_controller
}
presentation_file = 'file.tmp'
with patch('openlp.plugins.presentations.lib.mediaitem.os.path.getmtime') as mocked_getmtime, \
patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists:
mocked_getmtime.side_effect = [100, 200]
mocked_exists.return_value = True
thmub_path = MagicMock(st_mtime=100)
file_path = MagicMock(st_mtime=400)
with patch.object(Path, 'stat', side_effect=[thmub_path, file_path]), \
patch.object(Path, 'exists', return_value=True):
presentation_file = Path('file.tmp')
# WHEN: calling clean_up_thumbnails
self.media_item.clean_up_thumbnails(presentation_file, True)
@ -123,9 +125,8 @@ class TestMediaItem(TestCase, TestMixin):
self.media_item.controllers = {
'Mocked': mocked_controller
}
presentation_file = 'file.tmp'
with patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists:
mocked_exists.return_value = False
presentation_file = Path('file.tmp')
with patch.object(Path, 'exists', return_value=False):
# WHEN: calling clean_up_thumbnails
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.core.common import Settings
from openlp.core.common.path import Path
from openlp.core.lib import ScreenList
from tests.utils.constants import TEST_RESOURCES_PATH
@ -66,8 +67,8 @@ class TestPdfController(TestCase, TestMixin):
self.desktop.screenGeometry.return_value = SCREEN['size']
self.screens = ScreenList.create(self.desktop)
Settings().extend_default_settings(__default_settings__)
self.temp_folder = mkdtemp()
self.thumbnail_folder = mkdtemp()
self.temp_folder = Path(mkdtemp())
self.thumbnail_folder = Path(mkdtemp())
self.mock_plugin = MagicMock()
self.mock_plugin.settings_section = self.temp_folder
@ -77,8 +78,8 @@ class TestPdfController(TestCase, TestMixin):
"""
del self.screens
self.destroy_settings()
shutil.rmtree(self.thumbnail_folder)
shutil.rmtree(self.temp_folder)
shutil.rmtree(str(self.thumbnail_folder))
shutil.rmtree(str(self.temp_folder))
def test_constructor(self):
"""
@ -98,7 +99,7 @@ class TestPdfController(TestCase, TestMixin):
Test loading of a Pdf using the PdfController
"""
# 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
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
"""
# 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
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
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
if controller.gsbin:
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.
"""
import os
import shutil
from tempfile import mkdtemp
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.core.common import is_win
from openlp.core.common.path import Path
from tests.helpers.testmixin import TestMixin
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
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()
# WHEN reading the titles and notes
@ -201,13 +201,13 @@ class TestPptviewDocument(TestCase):
"""
# 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.object(Path, 'exists') as mocked_path_exists, \
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \
mocked_dir_exists:
mocked_exists.return_value = False
mocked_path_exists.return_value = False
mocked_dir_exists.return_value = False
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()
# 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.
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.')
def test_create_titles_and_notes_invalid_file(self):
@ -228,7 +228,7 @@ class TestPptviewDocument(TestCase):
mocked_is_zf.return_value = False
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.file_path = Path(TEST_RESOURCES_PATH, 'presentations', 'test.ppt')
doc.save_titles_and_notes = MagicMock()
# WHEN: reading the titles and notes

View File

@ -23,9 +23,8 @@
Functional tests to test the PresentationController and PresentationDocument
classes and related methods.
"""
import os
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.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument
@ -40,7 +39,7 @@ class TestPresentationController(TestCase):
def setUp(self):
self.get_thumbnail_folder_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder',
return_value=Path())
return_value=Path())
self.get_thumbnail_folder_patcher.start()
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
@ -67,23 +66,18 @@ class TestPresentationController(TestCase):
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()
with patch('builtins.open', mocked_open), patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
with patch('openlp.plugins.presentations.lib.presentationcontroller.Path.write_text') as mocked_write_text, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder:
titles = ['uno', 'dos']
notes = ['one', 'two']
# 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)
# 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')
mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), mode='wt', encoding='utf-8')
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')
self.assertEqual(mocked_write_text.call_count, 3, 'There should be exactly three files written')
mocked_write_text.assert_has_calls([call('uno\ndos'), call('one'), call('two')])
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
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('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = 'test'
patch('openlp.plugins.presentations.lib.presentationcontroller.Path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = Path('test')
mocked_exists.return_value = True
# 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.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')
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')
self.assertEqual(mocked_read_text.call_count, 3, 'Three files should be read')
def test_get_titles_and_notes_with_file_not_found(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, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = 'test'
mocked_exists.return_value = False
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')
# WHEN: calling get_titles_and_notes
result_titles, result_notes = self.document.get_titles_and_notes()
# 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.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(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):
"""
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, \
patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \
patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists:
mocked_get_thumbnail_folder.return_value = 'test'
mocked_exists.return_value = True
mocked_open.side_effect = IOError()
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 = IOError()
mocked_get_thumbnail_folder.return_value = Path('test')
# WHEN: calling 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')
self.get_thumbnail_folder_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder')
self.os_patcher = patch('openlp.plugins.presentations.lib.presentationcontroller.os')
self._setup_patcher = \
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument._setup')
self.mock_check_directory_exists = self.check_directory_exists_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_controller = MagicMock()
self.mock_get_thumbnail_folder.return_value = 'returned/path/'
self.mock_get_thumbnail_folder.return_value = Path('returned/path/')
def tearDown(self):
"""
@ -199,7 +183,6 @@ class TestPresentationDocument(TestCase):
"""
self.check_directory_exists_patcher.stop()
self.get_thumbnail_folder_patcher.stop()
self.os_patcher.stop()
self._setup_patcher.stop()
def test_initialise_presentation_document(self):
@ -227,7 +210,7 @@ class TestPresentationDocument(TestCase):
PresentationDocument(self.mock_controller, 'Name')
# 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()