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.file_path = Path('imagefile.png') self.media_item.manager = MagicMock() self.media_item.service_path = 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 assert 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.file_path = Path('/', 'tmp', 'test_file_1.jpg') returned_object2 = ImageFilenames() returned_object2.id = 2 returned_object2.file_path = Path('/', 'tmp', 'test_file_2.jpg') returned_object3 = ImageFilenames() returned_object3.id = 3 returned_object3.file_path = Path('/', '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 []
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 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