diff --git a/openlp/plugins/images/imageplugin.py b/openlp/plugins/images/imageplugin.py index 2dfb5e582..5b41f6761 100644 --- a/openlp/plugins/images/imageplugin.py +++ b/openlp/plugins/images/imageplugin.py @@ -75,7 +75,7 @@ class ImagePlugin(Plugin): Plugin.app_startup(self) # Convert old settings-based image list to the database files_from_config = Settings().get_files_from_config(self) - if len(files_from_config) > 0: + if files_from_config: log.debug(u'Importing images list from old config: %s' % files_from_config) self.mediaItem.save_new_images_list(files_from_config) @@ -87,7 +87,7 @@ class ImagePlugin(Plugin): The Settings object containing the old settings. """ files_from_config = settings.get_files_from_config(self) - if len(files_from_config) > 0: + if files_from_config: log.debug(u'Importing images list from old config: %s' % files_from_config) self.mediaItem.save_new_images_list(files_from_config) diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 604244c85..7a98a18d2 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -227,7 +227,7 @@ class ImageMediaItem(MediaManagerItem): QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes: self.recursively_delete_group(item_data) self.manager.delete_object(ImageGroups, row_item.data(0, QtCore.Qt.UserRole).id) - if item_data.parent_id is 0: + if item_data.parent_id == 0: self.listView.takeTopLevelItem(self.listView.indexOfTopLevelItem(row_item)) else: row_item.parent().removeChild(row_item) @@ -256,7 +256,7 @@ class ImageMediaItem(MediaManagerItem): group.setText(0, image_group.group_name) group.setData(0, QtCore.Qt.UserRole, image_group) group.setIcon(0, folder_icon) - if parent_group_id is 0: + if parent_group_id == 0: self.listView.addTopLevelItem(group) else: group_list[parent_group_id].addChild(group) @@ -276,7 +276,7 @@ class ImageMediaItem(MediaManagerItem): ``prefix`` A string containing the prefix that will be added in front of the groupname for each level of the tree """ - if parent_group_id is 0: + if parent_group_id == 0: combobox.clear() combobox.top_level_group_added = False image_groups = self.manager.get_all_objects(ImageGroups, ImageGroups.parent_id == parent_group_id) @@ -349,7 +349,7 @@ class ImageMediaItem(MediaManagerItem): item_name.setIcon(0, icon) item_name.setToolTip(0, imageFile.filename) item_name.setData(0, QtCore.Qt.UserRole, imageFile) - if imageFile.group_id is 0: + if imageFile.group_id == 0: self.listView.addTopLevelItem(item_name) else: group_items[imageFile.group_id].addChild(item_name) @@ -392,7 +392,7 @@ class ImageMediaItem(MediaManagerItem): # Find out if a group must be pre-selected preselect_group = None selected_items = self.listView.selectedItems() - if len(selected_items) > 0: + if selected_items: selected_item = selected_items[0] if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageFilenames): selected_item = selected_item.parent() @@ -475,7 +475,7 @@ class ImageMediaItem(MediaManagerItem): imageFile.filename = unicode(filename) self.manager.save_object(imageFile) self.main_window.increment_progress_bar() - if reload_list: + if reload_list and images_list: self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename)) def dnd_move_internal(self, target): @@ -606,7 +606,7 @@ class ImageMediaItem(MediaManagerItem): # Find out if a group must be pre-selected preselect_group = 0 selected_items = self.listView.selectedItems() - if len(selected_items) > 0: + if selected_items: selected_item = selected_items[0] if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageFilenames): selected_item = selected_item.parent() diff --git a/tests/functional/openlp_plugins/images/__init__.py b/tests/functional/openlp_plugins/images/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py new file mode 100644 index 000000000..eae30a113 --- /dev/null +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 +""" +This module contains tests for the lib submodule of the Images plugin. +""" + +from unittest import TestCase + +from mock import MagicMock, patch + +from openlp.core.lib import Registry +from openlp.plugins.images.lib.db import ImageFilenames +from openlp.plugins.images.lib.mediaitem import ImageMediaItem + + +class TestImageMediaItem(TestCase): + """ + This is a test case to test various methods in the ImageMediaItem class. + """ + + def setUp(self): + self.mocked_main_window = MagicMock() + Registry.create() + Registry().register(u'service_list', MagicMock()) + Registry().register(u'main_window', self.mocked_main_window) + mocked_parent = MagicMock() + mocked_plugin = MagicMock() + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.__init__') as mocked_init: + mocked_init.return_value = None + self.mediaitem = ImageMediaItem(mocked_parent, mocked_plugin) + + def save_new_images_list_empty_list_test(self): + """ + Test that the save_new_images_list() method handles empty lists gracefully + """ + # GIVEN: An empty image_list + image_list = [] + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: + self.mediaitem.manager = MagicMock() + + # WHEN: We run save_new_images_list with the empty list + self.mediaitem.save_new_images_list(image_list) + + # THEN: The save_object() method should not have been called + assert self.mediaitem.manager.save_object.call_count == 0, \ + u'The save_object() method should not have been called' + + def save_new_images_list_single_image_with_reload_test(self): + """ + Test that the save_new_images_list() calls loadFullList() when reload_list is set to True + """ + # GIVEN: A list with 1 image + image_list = [ u'test_image.jpg' ] + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: + ImageFilenames.filename = '' + self.mediaitem.manager = MagicMock() + + # WHEN: We run save_new_images_list with reload_list=True + self.mediaitem.save_new_images_list(image_list, reload_list=True) + + # THEN: loadFullList() should have been called + assert mocked_loadFullList.call_count == 1, u'loadFullList() should have been called' + + # CLEANUP: Remove added attribute from ImageFilenames + delattr(ImageFilenames, 'filename') + + def save_new_images_list_single_image_without_reload_test(self): + """ + Test that the save_new_images_list() doesn't call loadFullList() when reload_list is set to False + """ + # GIVEN: A list with 1 image + image_list = [ u'test_image.jpg' ] + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: + self.mediaitem.manager = MagicMock() + + # WHEN: We run save_new_images_list with reload_list=False + self.mediaitem.save_new_images_list(image_list, reload_list=False) + + # THEN: loadFullList() should not have been called + assert mocked_loadFullList.call_count == 0, u'loadFullList() should not have been called' + + def save_new_images_list_multiple_images_test(self): + """ + Test that the save_new_images_list() saves all images in the list + """ + # GIVEN: A list with 3 images + image_list = [ u'test_image_1.jpg', u'test_image_2.jpg', u'test_image_3.jpg' ] + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: + self.mediaitem.manager = MagicMock() + + # WHEN: We run save_new_images_list with the list of 3 images + self.mediaitem.save_new_images_list(image_list, reload_list=False) + + # THEN: loadFullList() should not have been called + assert self.mediaitem.manager.save_object.call_count == 3, \ + u'loadFullList() should have been called three times' + + def save_new_images_list_other_objects_in_list_test(self): + """ + Test that the save_new_images_list() ignores everything in the provided list except strings + """ + # GIVEN: A list with images and objects + image_list = [ u'test_image_1.jpg', None, True, ImageFilenames(), 'test_image_2.jpg' ] + with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: + self.mediaitem.manager = MagicMock() + + # WHEN: We run save_new_images_list with the list of images and objects + self.mediaitem.save_new_images_list(image_list, reload_list=False) + + # THEN: loadFullList() should not have been called + assert self.mediaitem.manager.save_object.call_count == 2, \ + u'loadFullList() should have been called only once'