Skip to content

Commit

Permalink
Added method to delete data file when acquisition is deleted, added u…
Browse files Browse the repository at this point in the history
…nit tests
  • Loading branch information
jhazentia committed Jul 9, 2019
1 parent e46fbce commit 6e31c73
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/tasks/models/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
22 changes: 22 additions & 0 deletions src/tasks/tests/test_detail_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
17 changes: 17 additions & 0 deletions src/tasks/tests/test_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 6e31c73

Please sign in to comment.