|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from django.contrib.auth.models import Group |
| 7 | +from django.core.exceptions import ObjectDoesNotExist |
7 | 8 | from django.core.files.base import ContentFile |
8 | 9 | from guardian.shortcuts import assign_perm, remove_perm |
9 | 10 | from requests import put |
10 | 11 |
|
11 | 12 | from grandchallenge.archives.models import ArchiveItem |
12 | 13 | from grandchallenge.archives.views import ArchiveItemsList |
| 14 | +from grandchallenge.cases.widgets import ImageWidgetChoices |
13 | 15 | from grandchallenge.components.form_fields import INTERFACE_FORM_FIELD_PREFIX |
14 | 16 | from grandchallenge.components.models import ( |
15 | 17 | ComponentInterface, |
@@ -1251,6 +1253,49 @@ def test_archive_item_create_view( |
1251 | 1253 | assert not UserUpload.objects.filter(pk=upload.pk).exists() |
1252 | 1254 |
|
1253 | 1255 |
|
| 1256 | +@pytest.mark.django_db |
| 1257 | +def test_archive_item_create_view_with_empty_value( |
| 1258 | + client, settings, django_capture_on_commit_callbacks |
| 1259 | +): |
| 1260 | + settings.task_eager_propagates = (True,) |
| 1261 | + settings.task_always_eager = (True,) |
| 1262 | + |
| 1263 | + archive = ArchiveFactory() |
| 1264 | + editor = UserFactory() |
| 1265 | + archive.add_editor(editor) |
| 1266 | + existing_archive_item = ArchiveItemFactory(archive=archive) |
| 1267 | + ci_str = ComponentInterfaceFactory(kind=InterfaceKindChoices.STRING) |
| 1268 | + ci_img = ComponentInterfaceFactory(kind=InterfaceKindChoices.PANIMG_IMAGE) |
| 1269 | + civ_str = ci_str.create_instance(value="foo") |
| 1270 | + civ_img = ci_img.create_instance(image=ImageFactory()) |
| 1271 | + existing_archive_item.values.set([civ_str, civ_img]) |
| 1272 | + |
| 1273 | + with django_capture_on_commit_callbacks(execute=True): |
| 1274 | + response = get_view_for_user( |
| 1275 | + viewname="archives:item-create", |
| 1276 | + client=client, |
| 1277 | + reverse_kwargs={"slug": archive.slug}, |
| 1278 | + data={ |
| 1279 | + **get_interface_form_data( |
| 1280 | + interface_slug=ci_str.slug, data="bar" |
| 1281 | + ), |
| 1282 | + f"widget-choice-{INTERFACE_FORM_FIELD_PREFIX}{ci_img.slug}": ImageWidgetChoices.UNDEFINED.name, |
| 1283 | + "title": "archive-item title", |
| 1284 | + }, |
| 1285 | + user=editor, |
| 1286 | + method=client.post, |
| 1287 | + ) |
| 1288 | + |
| 1289 | + assert response.status_code == 302 |
| 1290 | + assert ArchiveItem.objects.count() == 2 |
| 1291 | + new_archive_item = ArchiveItem.objects.order_by("created").last() |
| 1292 | + assert new_archive_item.title == "archive-item title" |
| 1293 | + assert new_archive_item.values.count() == 1 |
| 1294 | + assert new_archive_item.values.get(interface=ci_str).value == "bar" |
| 1295 | + with pytest.raises(ObjectDoesNotExist): |
| 1296 | + new_archive_item.values.get(interface=ci_img) |
| 1297 | + |
| 1298 | + |
1254 | 1299 | @pytest.mark.django_db |
1255 | 1300 | def test_archive_item_bulk_delete_permissions(client): |
1256 | 1301 | user, editor = UserFactory.create_batch(2) |
|
0 commit comments