From 6e31c732fea811dd817f612dc1e8c594c49c7200 Mon Sep 17 00:00:00 2001 From: Justin Haze Date: Tue, 9 Jul 2019 10:36:54 -0600 Subject: [PATCH] Added method to delete data file when acquisition is deleted, added unit tests --- src/tasks/models/acquisition.py | 10 ++++++++++ src/tasks/tests/test_detail_view.py | 22 ++++++++++++++++++++++ src/tasks/tests/test_list_view.py | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/tasks/models/acquisition.py b/src/tasks/models/acquisition.py index 8aec9968..4b37c2d7 100644 --- a/src/tasks/models/acquisition.py +++ b/src/tasks/models/acquisition.py @@ -3,6 +3,8 @@ from .task_result import TaskResult +from django.db.models.signals import pre_delete + class Acquisition(models.Model): """The data and metadata associated with a task. @@ -39,3 +41,11 @@ def __str__(self): self.task_result.task_id, self.recording_id, ) + + +def clean_up_data(sender, **kwargs): + acq = kwargs["instance"] + acq.data.delete(save=False) + + +pre_delete.connect(clean_up_data, sender=Acquisition) diff --git a/src/tasks/tests/test_detail_view.py b/src/tasks/tests/test_detail_view.py index e9dbf0cc..e82cba14 100644 --- a/src/tasks/tests/test_detail_view.py +++ b/src/tasks/tests/test_detail_view.py @@ -8,6 +8,10 @@ update_result_detail, ) +from tasks.models import Acquisition, TaskResult + +import os + def test_user_can_create_nonprivate_acquisition(user_client, test_scheduler): entry_name = simulate_acquisitions(user_client) @@ -225,3 +229,21 @@ def test_admin_cant_modify_own_results(admin_client, test_scheduler): response = update_result_detail(admin_client, entry_name, 1, new_result_detail) validate_response(response, status.HTTP_405_METHOD_NOT_ALLOWED) + + +def test_deleted_result_deletes_data_file(user_client, test_scheduler): + """A user should be able to delete results they own.""" + entry_name = simulate_acquisitions(user_client) + # schedule_entry = ScheduleEntry.objects.get(name=entry_name) + task_result = TaskResult.objects.get(schedule_entry__name=entry_name) + acquisition = Acquisition.objects.get(task_result__id=task_result.id) + data_file = acquisition.data.path + assert os.path.exists(data_file) + result_url = reverse_result_detail(entry_name, 1) + + first_response = user_client.delete(result_url, **HTTPS_KWARG) + second_response = user_client.delete(result_url, **HTTPS_KWARG) + + validate_response(first_response, status.HTTP_204_NO_CONTENT) + validate_response(second_response, status.HTTP_404_NOT_FOUND) + assert not os.path.exists(data_file) diff --git a/src/tasks/tests/test_list_view.py b/src/tasks/tests/test_list_view.py index 483b8787..e76f1729 100644 --- a/src/tasks/tests/test_list_view.py +++ b/src/tasks/tests/test_list_view.py @@ -10,6 +10,10 @@ simulate_acquisitions, ) +from tasks.models import Acquisition, TaskResult + +import os + def test_non_existent_entry(user_client): with pytest.raises(AssertionError): @@ -56,6 +60,19 @@ def test_delete_list(user_client): # If result does exist, expect 204 entry_name = create_task_results(1, user_client) + + url = reverse_result_list(entry_name) + response = user_client.delete(url, **HTTPS_KWARG) + validate_response(response, status.HTTP_204_NO_CONTENT) + + +def test_delete_list_data_files_deleted(user_client, test_scheduler): + entry_name = simulate_acquisitions(user_client) + task_result = TaskResult.objects.get(schedule_entry__name=entry_name) + acquisition = Acquisition.objects.get(task_result__id=task_result.id) + data_file = acquisition.data.path + assert os.path.exists(data_file) url = reverse_result_list(entry_name) response = user_client.delete(url, **HTTPS_KWARG) validate_response(response, status.HTTP_204_NO_CONTENT) + assert not os.path.exists(data_file)