def on_delete_click_test(self, mocked_check_item_selected, mocked_delete_file): """ Test that on_delete_click() works """ # GIVEN: An ImageGroups object and mocked functions mocked_check_item_selected.return_value = True test_image = ImageFilenames() test_image.id = 1 test_image.group_id = 1 test_image.filename = 'imagefile.png' self.media_item.manager = MagicMock() self.media_item.service_path = '' self.media_item.list_view = MagicMock() mocked_row_item = MagicMock() mocked_row_item.data.return_value = test_image mocked_row_item.text.return_value = '' self.media_item.list_view.selectedItems.return_value = [ mocked_row_item ] # WHEN: Calling on_delete_click self.media_item.on_delete_click() # THEN: delete_file should have been called twice self.assertEquals(mocked_delete_file.call_count, 2, 'delete_file() should have been called twice')
def save_new_images_list(self, images_list, group_id=0, reload_list=True): """ Convert a list of image filenames to ImageFilenames objects and save them in the database. :param images_list: A List of strings containing image filenames :param group_id: The ID of the group to save the images in :param reload_list: This boolean is set to True when the list in the interface should be reloaded after saving the new images """ for filename in images_list: if not isinstance(filename, str): continue log.debug('Adding new image: %s', filename) image_file = ImageFilenames() image_file.group_id = group_id image_file.filename = str(filename) self.manager.save_object(image_file) self.main_window.increment_progress_bar() if reload_list and images_list: self.load_full_list(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename))
def test_create_item_from_id(self): """ Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames """ # GIVEN: An ImageFilenames that already exists in the database image_file = ImageFilenames() image_file.id = 1 image_file.filename = '/tmp/test_file_1.jpg' self.media_item.manager = MagicMock() self.media_item.manager.get_object_filtered.return_value = image_file ImageFilenames.filename = '' # WHEN: create_item_from_id() is called item = self.media_item.create_item_from_id(1) # THEN: A QTreeWidgetItem should be created with the above model object as it's data self.assertIsInstance(item, QtWidgets.QTreeWidgetItem) self.assertEqual('test_file_1.jpg', item.text(0)) item_data = item.data(0, QtCore.Qt.UserRole) self.assertIsInstance(item_data, ImageFilenames) self.assertEqual(1, item_data.id) self.assertEqual('/tmp/test_file_1.jpg', item_data.filename)
def save_new_images_list(self, image_paths, group_id=0, reload_list=True): """ Convert a list of image filenames to ImageFilenames objects and save them in the database. :param list[Path] image_paths: A List of file paths to image :param group_id: The ID of the group to save the images in :param reload_list: This boolean is set to True when the list in the interface should be reloaded after saving the new images """ for image_path in image_paths: if not isinstance(image_path, Path): continue log.debug('Adding new image: {name}'.format(name=image_path)) image_file = ImageFilenames() image_file.group_id = group_id image_file.file_path = image_path self.manager.save_object(image_file) self.main_window.increment_progress_bar() if reload_list and image_paths: self.load_full_list( self.manager.get_all_objects( ImageFilenames, order_by_ref=ImageFilenames.file_path))
def create_item_from_id_test(self): """ Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames """ # GIVEN: An ImageFilenames that already exists in the database image_file = ImageFilenames() image_file.id = 1 image_file.filename = '/tmp/test_file_1.jpg' self.media_item.manager = MagicMock() self.media_item.manager.get_object_filtered.return_value = image_file ImageFilenames.filename = '' # WHEN: create_item_from_id() is called item = self.media_item.create_item_from_id(1) # THEN: A QTreeWidgetItem should be created with the above model object as it's data self.assertIsInstance(item, QtGui.QTreeWidgetItem) self.assertEqual('test_file_1.jpg', item.text(0)) item_data = item.data(0, QtCore.Qt.UserRole) self.assertIsInstance(item_data, ImageFilenames) self.assertEqual(1, item_data.id) self.assertEqual('/tmp/test_file_1.jpg', item_data.filename)
def test_create_item_from_id(self): """ Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames """ # GIVEN: An ImageFilenames that already exists in the database image_file = ImageFilenames() image_file.id = 1 image_file.file_path = Path('/', 'tmp', 'test_file_1.jpg') self.media_item.manager = MagicMock() self.media_item.manager.get_object_filtered.return_value = image_file ImageFilenames.file_path = None # WHEN: create_item_from_id() is called item = self.media_item.create_item_from_id('1') # THEN: A QTreeWidgetItem should be created with the above model object as it's data assert isinstance(item, QtWidgets.QTreeWidgetItem) assert 'test_file_1.jpg' == item.text(0) item_data = item.data(0, QtCore.Qt.UserRole) assert isinstance(item_data, ImageFilenames) assert 1 == item_data.id assert Path('/', 'tmp', 'test_file_1.jpg') == item_data.file_path
def test_save_new_images_list_other_objects_in_list(self, mocked_load_full_list): """ Test that the save_new_images_list() ignores everything in the provided list except strings """ # GIVEN: A list with images and objects image_list = [Path('test_image_1.jpg'), None, True, ImageFilenames(), Path('test_image_2.jpg')] self.media_item.manager = MagicMock() # WHEN: We run save_new_images_list with the list of images and objects self.media_item.save_new_images_list(image_list, reload_list=False) # THEN: load_full_list() should not have been called assert self.media_item.manager.save_object.call_count == 2, 'load_full_list() should have been called only once'
def test_on_delete_click(self, mocked_check_item_selected, mocked_delete_file): """ Test that on_delete_click() works """ # GIVEN: An ImageGroups object and mocked functions mocked_check_item_selected.return_value = True test_image = ImageFilenames() test_image.id = 1 test_image.group_id = 1 test_image.filename = 'imagefile.png' self.media_item.manager = MagicMock() self.media_item.service_path = '' self.media_item.list_view = MagicMock() mocked_row_item = MagicMock() mocked_row_item.data.return_value = test_image mocked_row_item.text.return_value = '' self.media_item.list_view.selectedItems.return_value = [mocked_row_item] # WHEN: Calling on_delete_click self.media_item.on_delete_click() # THEN: delete_file should have been called twice self.assertEquals(mocked_delete_file.call_count, 2, 'delete_file() should have been called twice')
def _recursively_delete_group_side_effect(*args, **kwargs): """ Side effect method that creates custom return values for the recursively_delete_group method """ if args[1] == ImageFilenames and args[2]: # Create some fake objects that should be removed returned_object1 = ImageFilenames() returned_object1.id = 1 returned_object1.filename = '/tmp/test_file_1.jpg' returned_object2 = ImageFilenames() returned_object2.id = 2 returned_object2.filename = '/tmp/test_file_2.jpg' returned_object3 = ImageFilenames() returned_object3.id = 3 returned_object3.filename = '/tmp/test_file_3.jpg' return [returned_object1, returned_object2, returned_object3] if args[1] == ImageGroups and args[2]: # Change the parent_id that is matched so we don't get into an endless loop ImageGroups.parent_id = 0 # Create a fake group that will be used in the next run returned_object1 = ImageGroups() returned_object1.id = 1 return [returned_object1] return []