From 616ba9914c651421de5bd43cbae4646a5c5ce839 Mon Sep 17 00:00:00 2001 From: Ammar Ammar <43293485+ammar257ammar@users.noreply.github.com> Date: Fri, 20 Dec 2024 08:49:38 +0000 Subject: [PATCH] test job creation with file input --- app/tests/algorithms_tests/test_views.py | 40 +++++++++++++++++++----- app/tests/conftest.py | 1 + 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/tests/algorithms_tests/test_views.py b/app/tests/algorithms_tests/test_views.py index 8c28348bf4..293c059a93 100644 --- a/app/tests/algorithms_tests/test_views.py +++ b/app/tests/algorithms_tests/test_views.py @@ -26,6 +26,7 @@ ) from grandchallenge.components.schemas import GPUTypeChoices from grandchallenge.subdomains.utils import reverse +from grandchallenge.uploads.models import UserUpload from tests.algorithms_tests.factories import ( AlgorithmFactory, AlgorithmImageFactory, @@ -1357,11 +1358,34 @@ def test_create_job_is_idempotent( assert Job.objects.count() == 1 @override_settings(task_eager_propagates=True, task_always_eager=True) - def test_create_job_with_faulty_file_input( + @pytest.mark.django_db + @pytest.mark.parametrize( + "file_json_content,job_status,error_message,detailed_error_message,ci_count,upload_exists", + [ + ( + b'{"Foo": "bar"}', + Job.CANCELLED, + "One or more of the inputs failed validation.", + { + "ci_json_file": "JSON does not fulfill schema: instance is not of type 'array'" + }, + 0, + True, + ), + (b'[{"Foo": "bar"}]', Job.PROVISIONED, "", {}, 1, False), + ], + ) + def test_create_job_with_file_input( self, client, django_capture_on_commit_callbacks, algorithm_with_multiple_inputs, + file_json_content, + error_message, + detailed_error_message, + ci_count, + upload_exists, + job_status, ): # configure file input algorithm_with_multiple_inputs.algorithm.inputs.set( @@ -1371,7 +1395,7 @@ def test_create_job_with_faulty_file_input( filename="file.json", creator=algorithm_with_multiple_inputs.editor ) presigned_urls = file_upload.generate_presigned_urls(part_numbers=[1]) - response = put(presigned_urls["1"], data=b'{"Foo": "bar"}') + response = put(presigned_urls["1"], data=file_json_content) file_upload.complete_multipart_upload( parts=[{"ETag": response.headers["ETag"], "PartNumber": 1}] ) @@ -1394,15 +1418,15 @@ def test_create_job_with_faulty_file_input( assert Job.objects.count() == 1 job = Job.objects.get() # but in cancelled state and with an error message - assert job.status == Job.CANCELLED + assert job.status == job_status + assert error_message == job.error_message + assert job.detailed_error_message == detailed_error_message assert ( - "One or more of the inputs failed validation." == job.error_message + UserUpload.objects.filter(pk=file_upload.pk).exists() + == upload_exists ) - assert job.detailed_error_message == { - algorithm_with_multiple_inputs.ci_json_file.title: "JSON does not fulfill schema: instance is not of type 'array'" - } # and no CIVs should have been created - assert ComponentInterfaceValue.objects.count() == 0 + assert ComponentInterfaceValue.objects.count() == ci_count @override_settings(task_eager_propagates=True, task_always_eager=True) def test_create_job_with_faulty_json_input( diff --git a/app/tests/conftest.py b/app/tests/conftest.py index 14ecdcdd37..5b362f9011 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -548,6 +548,7 @@ def algorithm_with_multiple_inputs(): }, ) ci_json_file = ComponentInterfaceFactory( + title="ci_json_file", kind=InterfaceKind.InterfaceKindChoices.ANY, store_in_database=False, schema={