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 _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 []
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 []
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 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_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')