From e3f147a226852be2a29d227d8fdd6b0016cf470e Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 19 Jan 2015 22:49:18 +0000 Subject: [PATCH 1/4] Fixes 1410456 and adds a couple tests Fixes: https://launchpad.net/bugs/1410456 --- openlp/plugins/images/lib/mediaitem.py | 4 +- tests/functional/__init__.py | 4 +- .../openlp_plugins/images/test_lib.py | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 9b3dd4d35..a1b3d7e71 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -371,7 +371,7 @@ class ImageMediaItem(MediaManagerItem): """ self.application.set_normal_cursor() self.load_list(files, target_group) - last_dir = os.path.split(str(files[0]))[0] + last_dir = os.path.split(files[0])[0] Settings().setValue(self.settings_section + '/last directory', last_dir) def load_list(self, images, target_group=None, initial_load=False): @@ -535,7 +535,7 @@ class ImageMediaItem(MediaManagerItem): if not items: return False # Determine service item title - if isinstance(items[0].data(0, QtCore.Qt.UserRole), ImageGroups): + if isinstance(items[0].data(0, QtCore.Qt.UserRole), ImageGroups) or len(items) == 1: service_item.title = items[0].text(0) else: service_item.title = str(self.plugin.name_strings['plural']) diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py index 1ec85ff80..5948f480f 100644 --- a/tests/functional/__init__.py +++ b/tests/functional/__init__.py @@ -35,9 +35,9 @@ import sys from PyQt4 import QtGui if sys.version_info[1] >= 3: - from unittest.mock import MagicMock, patch, mock_open, call + from unittest.mock import ANY, MagicMock, patch, mock_open, call else: - from mock import MagicMock, patch, mock_open, call + from mock import ANY, MagicMock, patch, mock_open, call # Only one QApplication can be created. Use QtGui.QApplication.instance() when you need to "create" a QApplication. application = QtGui.QApplication([]) diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py index d9f9f7603..2c9c94276 100644 --- a/tests/functional/openlp_plugins/images/test_lib.py +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -27,7 +27,7 @@ from unittest import TestCase from openlp.core.common import Registry from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups from openlp.plugins.images.lib.mediaitem import ImageMediaItem -from tests.functional import MagicMock, patch +from tests.functional import ANY, MagicMock, patch class TestImageMediaItem(TestCase): @@ -38,6 +38,7 @@ class TestImageMediaItem(TestCase): def setUp(self): self.mocked_main_window = MagicMock() Registry.create() + Registry().register('application', MagicMock()) Registry().register('service_list', MagicMock()) Registry().register('main_window', self.mocked_main_window) Registry().register('live_controller', MagicMock()) @@ -45,6 +46,43 @@ class TestImageMediaItem(TestCase): with patch('openlp.plugins.images.lib.mediaitem.MediaManagerItem._setup'), \ patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.setup_item'): self.media_item = ImageMediaItem(None, mocked_plugin) + self.media_item.settings_section = 'images' + + def validate_and_load_test(self): + """ + Test that the validate_and_load_test() method + """ + # GIVEN: A list of files + file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] + + with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + + # WHEN: Calling validate_and_load with the list of files + self.media_item.validate_and_load(file_list) + + # THEN: load_list should have been called with the file list and None, + # the dectory should have been saved to the settings + mocked_load_list.assert_called_once_with(file_list, None) + mocked_settings().setValue.assert_called_once_with(ANY, '/path1') + + def validate_and_load_group_test(self): + """ + Test that the validate_and_load_test() method + """ + # GIVEN: A list of files + file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] + + with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + + # WHEN: Calling validate_and_load with the list of files and a group + self.media_item.validate_and_load(file_list, 'group') + + # THEN: load_list should have been called with the file list and the group name, + # the dectory should have been saved to the settings + mocked_load_list.assert_called_once_with(file_list, 'group') + mocked_settings().setValue.assert_called_once_with(ANY, '/path1') def save_new_images_list_empty_list_test(self): """ From 63f68d344f596701477f1dd32326690fd7fb6a0b Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 19 Jan 2015 22:56:30 +0000 Subject: [PATCH 2/4] pep fixes --- tests/functional/openlp_plugins/images/test_lib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py index 2c9c94276..79ebda0b4 100644 --- a/tests/functional/openlp_plugins/images/test_lib.py +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -56,7 +56,7 @@ class TestImageMediaItem(TestCase): file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ - patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: # WHEN: Calling validate_and_load with the list of files self.media_item.validate_and_load(file_list) @@ -74,7 +74,7 @@ class TestImageMediaItem(TestCase): file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ - patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: # WHEN: Calling validate_and_load with the list of files and a group self.media_item.validate_and_load(file_list, 'group') From f1f0ab9c3d783e05c0939937af90ca5a10a22efd Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 19 Jan 2015 22:59:49 +0000 Subject: [PATCH 3/4] pep fixes --- tests/functional/openlp_plugins/images/test_lib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py index 79ebda0b4..c90459544 100644 --- a/tests/functional/openlp_plugins/images/test_lib.py +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -56,7 +56,7 @@ class TestImageMediaItem(TestCase): file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ - patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: # WHEN: Calling validate_and_load with the list of files self.media_item.validate_and_load(file_list) @@ -74,7 +74,7 @@ class TestImageMediaItem(TestCase): file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ - patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: # WHEN: Calling validate_and_load with the list of files and a group self.media_item.validate_and_load(file_list, 'group') From 88f69ea7c4989f7e11f8f9c3289269c1192e489c Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Tue, 20 Jan 2015 07:14:58 +0000 Subject: [PATCH 4/4] doc strings an typos --- tests/functional/openlp_plugins/images/test_lib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py index c90459544..9dfb324cc 100644 --- a/tests/functional/openlp_plugins/images/test_lib.py +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -50,7 +50,7 @@ class TestImageMediaItem(TestCase): def validate_and_load_test(self): """ - Test that the validate_and_load_test() method + Test that the validate_and_load_test() method when called without a group """ # GIVEN: A list of files file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] @@ -62,13 +62,13 @@ class TestImageMediaItem(TestCase): self.media_item.validate_and_load(file_list) # THEN: load_list should have been called with the file list and None, - # the dectory should have been saved to the settings + # the directory should have been saved to the settings mocked_load_list.assert_called_once_with(file_list, None) mocked_settings().setValue.assert_called_once_with(ANY, '/path1') def validate_and_load_group_test(self): """ - Test that the validate_and_load_test() method + Test that the validate_and_load_test() method when called with a group """ # GIVEN: A list of files file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] @@ -80,7 +80,7 @@ class TestImageMediaItem(TestCase): self.media_item.validate_and_load(file_list, 'group') # THEN: load_list should have been called with the file list and the group name, - # the dectory should have been saved to the settings + # the directory should have been saved to the settings mocked_load_list.assert_called_once_with(file_list, 'group') mocked_settings().setValue.assert_called_once_with(ANY, '/path1')