def delete_document(self, spot, document_type): document = Document.objects.filter(spot=spot, type=document_type).first() if document: objstore = ObjectStore(settings.OBJECTSTORE_CONNECTION_CONFIG) objstore.delete(document) document.delete() else: logger.error(f"Delete document was called for spot {spot.id} and document_type {document_type}, " f"but it does not exist in our database")
def test_delete(self, mocked_get_container_path, mocked_get_connection): """ Test and assert that delete_object is called with the correct arguments """ mocked_get_container_path.return_value = 'container/path/' objstore = ObjectStore(config='this is the config') connection = Connection() connection.delete_object = Mock() mocked_get_connection.return_value = connection document = Document(filename='doc_file_name.pdf', type=Document.DocumentType.Ontwerp) objstore.delete(document) connection.delete_object.assert_called_with('container/path/', 'doc_file_name.pdf')
def test_delete_non_existing_file(self, mocked_get_container_path, mocked_get_connection): """ Test and assert that delete_object is called with the correct arguments """ mocked_get_container_path.return_value = 'container/path/' objstore = ObjectStore(config='this is the config') connection = Connection() connection.delete_object = Mock(side_effect=ClientException('')) mocked_get_connection.return_value = connection document = Document(id=1, filename='doc_file_name.pdf', type=Document.DocumentType.Ontwerp) with self.assertLogs(level='INFO') as logs: objstore.delete(document) connection.delete_object.assert_called_with('container/path/', 'doc_file_name.pdf') self.assertIn( 'INFO:storage.objectstore:Failed to delete object for document id 1', logs.output)