Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete user upload after creating civ for file #3761

Merged
merged 11 commits into from
Jan 8, 2025
1 change: 1 addition & 0 deletions app/grandchallenge/components/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,7 @@ def add_file_to_object(
civ.full_clean()
civ.save()
user_upload.copy_object(to_field=civ.file)
user_upload.delete()
except ValidationError as e:
error_handler.handle_error(
interface=interface,
Expand Down
5 changes: 5 additions & 0 deletions app/tests/algorithms_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ComponentInterfaceValue,
InterfaceKindChoices,
)
from grandchallenge.uploads.models import UserUpload
from tests.algorithms_tests.factories import (
AlgorithmImageFactory,
AlgorithmJobFactory,
Expand Down Expand Up @@ -211,6 +212,10 @@ def test_create_job_with_multiple_new_inputs(
assert job.time_limit == 600
assert job.inputs.count() == 6

assert not UserUpload.objects.filter(
pk=algorithm_with_multiple_inputs.file_upload.pk
).exists()

assert sorted(
[
int.pk
Expand Down
6 changes: 6 additions & 0 deletions app/tests/algorithms_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -969,6 +970,7 @@ def test_create_job_with_json_file(
file.name.split("/")[-1]
in Job.objects.get().inputs.first().file.name
)
assert not UserUpload.objects.filter(pk=upload.pk).exists()


@pytest.mark.django_db
Expand Down Expand Up @@ -1196,6 +1198,10 @@ def test_create_job_with_multiple_new_inputs(
assert job.time_limit == 600
assert job.inputs.count() == 6

assert not UserUpload.objects.filter(
pk=algorithm_with_multiple_inputs.file_upload.pk
).exists()

assert sorted(
[
int.pk
Expand Down
8 changes: 8 additions & 0 deletions app/tests/archives_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from grandchallenge.components.models import ComponentInterface, InterfaceKind
from grandchallenge.notifications.models import Notification
from grandchallenge.subdomains.utils import reverse
from grandchallenge.uploads.models import UserUpload
from tests.algorithms_tests.factories import AlgorithmJobFactory, Job
from tests.archives_tests.factories import (
ArchiveFactory,
Expand Down Expand Up @@ -448,6 +449,7 @@ def test_api_archive_item_add_and_update_non_image_file(
assert item.values.count() == 1
civ = item.values.get()
assert civ.interface.slug == ci.slug
assert not UserUpload.objects.filter(pk=upload.pk).exists()

# partial update civ
upload2 = create_upload_from_file(
Expand Down Expand Up @@ -481,6 +483,7 @@ def test_api_archive_item_add_and_update_non_image_file(
new_civ = item.values.get()
assert new_civ.interface.slug == ci.slug
assert new_civ != civ
assert not UserUpload.objects.filter(pk=upload2.pk).exists()


@pytest.mark.django_db
Expand Down Expand Up @@ -638,6 +641,7 @@ def test_api_archive_item_add_and_update_json_file(
assert item.values.count() == 1
civ = item.values.get()
assert civ.interface.slug == ci.slug
assert not UserUpload.objects.filter(pk=upload.pk).exists()

# update civ
with tempfile.NamedTemporaryFile(mode="w+", suffix=".json") as file:
Expand Down Expand Up @@ -673,6 +677,7 @@ def test_api_archive_item_add_and_update_json_file(
new_civ = item.values.get()
assert new_civ.interface.slug == ci.slug
assert new_civ != civ
assert not UserUpload.objects.filter(pk=upload2.pk).exists()


@pytest.mark.django_db
Expand Down Expand Up @@ -933,6 +938,7 @@ def test_archive_item_add_file(
)
},
)
assert not UserUpload.objects.filter(pk=upload.pk).exists()
assert response.status_code == 302
assert "test" in ArchiveItem.objects.get().values.first().file.name

Expand Down Expand Up @@ -976,6 +982,7 @@ def test_archive_item_add_json_file(
)
assert response.status_code == 302
assert file.name.split("/")[-1] in item.values.first().file.name
assert not UserUpload.objects.filter(pk=upload.pk).exists()

item2 = ArchiveItemFactory(archive=archive)
civ = ComponentInterfaceValueFactory(
Expand Down Expand Up @@ -1174,6 +1181,7 @@ def test_archive_item_create_view(
assert ai2.values.get(interface=ci_img2).image == image
assert ai2.values.get(interface=ci_json).file.read() == b'{"foo": "bar"}'
assert ai2.values.get(interface=ci_json2).value == {"some": "content"}
assert not UserUpload.objects.filter(pk=upload.pk).exists()


@pytest.mark.django_db
Expand Down
2 changes: 2 additions & 0 deletions app/tests/components_tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from grandchallenge.core.celery import _retry, acks_late_micro_short_task
from grandchallenge.notifications.models import Notification
from grandchallenge.reader_studies.models import InteractiveAlgorithmChoices
from grandchallenge.uploads.models import UserUpload
from tests.algorithms_tests.factories import (
AlgorithmFactory,
AlgorithmImageFactory,
Expand Down Expand Up @@ -536,6 +537,7 @@ def test_add_file_to_object(
linked_task=linked_task,
)

assert not UserUpload.objects.filter(pk=us.pk).exists()
assert ComponentInterfaceValue.objects.filter(interface=ci).count() == 1
assert "some_async_task" in str(callbacks)

Expand Down
2 changes: 2 additions & 0 deletions app/tests/reader_studies_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Question,
)
from grandchallenge.subdomains.utils import reverse
from grandchallenge.uploads.models import UserUpload
from tests.cases_tests import RESOURCE_PATH
from tests.components_tests.factories import (
ComponentInterfaceFactory,
Expand Down Expand Up @@ -568,6 +569,7 @@ def do_update():
user=user,
method=client.post,
)
assert not UserUpload.objects.filter(pk=upload.pk).exists()
assert response.status_code == 302
assert response.headers["HX-Redirect"] == reverse(
"reader-studies:display_sets", kwargs={"slug": rs.slug}
Expand Down
Loading