|
1 | | -from contextlib import nullcontext |
2 | | - |
3 | 1 | import pytest |
4 | 2 | from guardian.shortcuts import assign_perm |
5 | | -from rest_framework import serializers |
6 | 3 |
|
7 | 4 | from grandchallenge.cases.models import RawImageUploadSession |
8 | 5 | from grandchallenge.components.models import ( |
@@ -312,34 +309,55 @@ def test_civ_post_value_validation(kind): |
312 | 309 |
|
313 | 310 | @pytest.mark.django_db |
314 | 311 | @pytest.mark.parametrize("kind", InterfaceKinds.json) |
315 | | -@pytest.mark.parametrize( |
316 | | - "store_in_database, expected_error", |
317 | | - ( |
318 | | - (True, "value is required for interface kind {kind}"), |
319 | | - (False, "user_upload or file is required for interface kind {kind}"), |
320 | | - ), |
321 | | -) |
322 | | -def test_civ_post_value_or_user_upload_required_validation( |
323 | | - kind, store_in_database, expected_error |
324 | | -): |
325 | | - # setup |
| 312 | +def test_civ_post_value_required_validation(kind): |
326 | 313 | interface = ComponentInterfaceFactory( |
327 | 314 | kind=kind, |
328 | | - store_in_database=store_in_database, |
| 315 | + store_in_database=True, |
329 | 316 | ) |
330 | 317 |
|
331 | | - payload = {"interface": interface.slug} |
| 318 | + for kwargs in [ |
| 319 | + {"image": ""}, |
| 320 | + {"user_upload": ""}, |
| 321 | + {"upload_session": ""}, |
| 322 | + {"image_name": "foo", "user_uploads": [""]}, |
| 323 | + ]: |
| 324 | + payload = {"interface": interface.slug, **kwargs} |
332 | 325 |
|
333 | | - # test |
334 | | - serializer = ComponentInterfaceValuePostSerializer(data=payload) |
| 326 | + serializer = ComponentInterfaceValuePostSerializer(data=payload) |
335 | 327 |
|
336 | | - # verify |
337 | | - assert not serializer.is_valid() |
338 | | - assert ( |
339 | | - expected_error.format(kind=kind) |
340 | | - in serializer.errors["non_field_errors"] |
| 328 | + assert not serializer.is_valid() |
| 329 | + assert ( |
| 330 | + f"value is required for interface kind {kind}" |
| 331 | + in serializer.errors["non_field_errors"] |
| 332 | + ) |
| 333 | + |
| 334 | + |
| 335 | +@pytest.mark.django_db |
| 336 | +@pytest.mark.parametrize("kind", InterfaceKinds.json) |
| 337 | +def test_civ_post_file_or_user_upload_required_validation(kind): |
| 338 | + interface = ComponentInterfaceFactory( |
| 339 | + kind=kind, |
| 340 | + store_in_database=False, |
341 | 341 | ) |
342 | 342 |
|
| 343 | + for kwargs in [ |
| 344 | + {"image": ""}, |
| 345 | + {"value": "foo"}, |
| 346 | + {"upload_session": ""}, |
| 347 | + {"image_name": "foo", "user_uploads": [""]}, |
| 348 | + ]: |
| 349 | + payload = {"interface": interface.slug, **kwargs} |
| 350 | + |
| 351 | + # test |
| 352 | + serializer = ComponentInterfaceValuePostSerializer(data=payload) |
| 353 | + |
| 354 | + # verify |
| 355 | + assert not serializer.is_valid() |
| 356 | + assert ( |
| 357 | + f"user_upload or file is required for interface kind {kind}" |
| 358 | + in serializer.errors["non_field_errors"] |
| 359 | + ) |
| 360 | + |
343 | 361 |
|
344 | 362 | @pytest.mark.django_db |
345 | 363 | @pytest.mark.parametrize("kind", InterfaceKinds.json) |
@@ -398,32 +416,43 @@ def test_civ_post_image_or_upload_required_validation(kind): |
398 | 416 | # setup |
399 | 417 | interface = ComponentInterfaceFactory(kind=kind) |
400 | 418 |
|
401 | | - payload = {"interface": interface.slug} |
| 419 | + for kwargs in [ |
| 420 | + {"value": "foo"}, |
| 421 | + {"upload_session": ""}, |
| 422 | + {"image_name": "foo", "user_uploads": [""]}, |
| 423 | + ]: |
| 424 | + payload = {"interface": interface.slug, **kwargs} |
402 | 425 |
|
403 | | - # test |
404 | | - serializer = ComponentInterfaceValuePostSerializer(data=payload) |
| 426 | + # test |
| 427 | + serializer = ComponentInterfaceValuePostSerializer(data=payload) |
405 | 428 |
|
406 | | - # verify |
407 | | - assert not serializer.is_valid() |
408 | | - assert ( |
409 | | - f"upload_session or image are required for interface kind {kind}" |
410 | | - in serializer.errors["non_field_errors"] |
411 | | - ) |
| 429 | + # verify |
| 430 | + assert not serializer.is_valid() |
| 431 | + assert ( |
| 432 | + f"upload_session or image are required for interface kind {kind}" |
| 433 | + in serializer.errors["non_field_errors"] |
| 434 | + ) |
412 | 435 |
|
413 | 436 |
|
414 | 437 | @pytest.mark.django_db |
415 | 438 | @pytest.mark.parametrize("kind,", InterfaceKinds.dicom) |
416 | 439 | def test_civ_post_dicom_image_or_upload_required_validation(kind, rf): |
417 | 440 | interface = ComponentInterfaceFactory(kind=kind) |
418 | | - payload = {"interface": interface.slug} |
419 | 441 |
|
420 | | - serializer = ComponentInterfaceValuePostSerializer(data=payload) |
| 442 | + for kwargs in [ |
| 443 | + {"value": "foo"}, |
| 444 | + {"user_upload": ""}, |
| 445 | + {"upload_session": ""}, |
| 446 | + ]: |
| 447 | + payload = {"interface": interface.slug, **kwargs} |
421 | 448 |
|
422 | | - assert not serializer.is_valid() |
423 | | - assert ( |
424 | | - f"either user_uploads with image_name, or image are required for interface kind {kind}" |
425 | | - in serializer.errors["non_field_errors"] |
426 | | - ) |
| 449 | + serializer = ComponentInterfaceValuePostSerializer(data=payload) |
| 450 | + |
| 451 | + assert not serializer.is_valid() |
| 452 | + assert ( |
| 453 | + f"either user_uploads with image_name, or image are required for interface kind {kind}" |
| 454 | + in serializer.errors["non_field_errors"] |
| 455 | + ) |
427 | 456 |
|
428 | 457 | payload = {"interface": interface.slug, "image_name": "foobar"} |
429 | 458 |
|
@@ -733,55 +762,3 @@ def test_reformat_serialized_civ_data(): |
733 | 762 | assert civ_data_objects[3].dicom_upload_with_name.name == "a dcm" |
734 | 763 | assert civ_data_objects[3].dicom_upload_with_name.user_uploads == uploads |
735 | 764 | assert civ_data_objects[4].user_upload == uploads[0] |
736 | | - |
737 | | - |
738 | | -def test_reformat_serialized_civ_data_invalid_no_keys(): |
739 | | - data = [{"interface": ""}] |
740 | | - |
741 | | - with pytest.raises(serializers.ValidationError) as e: |
742 | | - reformat_serialized_civ_data(serialized_civ_data=data) |
743 | | - |
744 | | - assert ( |
745 | | - "You must provide at least one of ['image', 'value', 'file', " |
746 | | - "'user_upload', 'upload_session', ('image_name', 'user_uploads')]." |
747 | | - ) in str(e.value) |
748 | | - |
749 | | - |
750 | | -@pytest.mark.django_db |
751 | | -def test_reformat_serialized_civ_data_invalid_multiple_keys(): |
752 | | - data = [{"interface": "some_socket_slug", "value": "foo", "image": "foo"}] |
753 | | - |
754 | | - with pytest.raises(serializers.ValidationError) as e: |
755 | | - reformat_serialized_civ_data(serialized_civ_data=data) |
756 | | - |
757 | | - assert ( |
758 | | - "You can only provide one of ['image', 'value', 'file', " |
759 | | - "'user_upload', 'upload_session', ('image_name', 'user_uploads')] for each socket." |
760 | | - ) in str(e.value) |
761 | | - |
762 | | - # Using multiple keys (image_name and user_uploads) is necessary for DICOM |
763 | | - ci_dcm = ComponentInterfaceFactory( |
764 | | - kind=InterfaceKindChoices.DICOM_IMAGE_SET |
765 | | - ) |
766 | | - data = [ |
767 | | - {"interface": ci_dcm, "image_name": "foo", "user_uploads": ["foo"]} |
768 | | - ] |
769 | | - |
770 | | - with nullcontext(): |
771 | | - reformat_serialized_civ_data(serialized_civ_data=data) |
772 | | - |
773 | | - |
774 | | -@pytest.mark.django_db |
775 | | -def test_reformat_serialized_civ_data_invalid_dicom_keys(): |
776 | | - ci_dcm = ComponentInterfaceFactory( |
777 | | - kind=InterfaceKindChoices.DICOM_IMAGE_SET |
778 | | - ) |
779 | | - for key in ("image_name", "user_uploads"): |
780 | | - data = [{"interface": ci_dcm, key: "foo"}] |
781 | | - |
782 | | - with pytest.raises(serializers.ValidationError) as e: |
783 | | - reformat_serialized_civ_data(serialized_civ_data=data) |
784 | | - |
785 | | - assert ( |
786 | | - "You must provide 'image_name' and 'user_uploads' together." |
787 | | - ) in str(e.value) |
0 commit comments