diff --git a/atdb/taskdatabase/models.py b/atdb/taskdatabase/models.py index bd3fb485ff468d090075e124b725861a9a84320b..5770346682804da63efee75f88eb5a4e6245e860 100644 --- a/atdb/taskdatabase/models.py +++ b/atdb/taskdatabase/models.py @@ -304,17 +304,18 @@ class Task(models.Model): return None @property - def path_to_lta(self): + def sasid_path_to_lta(self): """ check if any task belonging to this sas_id already has a 'path_to_lta' setting """ try: for task in Task.objects.filter(sas_id=self.sas_id): - if task.archive['path_to_lta']: - try: + try: + if task.archive['path_to_lta']: return task.archive['path_to_lta'] - except: - pass + except: + # if 'path_to_lta' is not found, or 'archive' is empty, continue to the next task + pass except: return None diff --git a/atdb/taskdatabase/tests/test_path_to_lta.py b/atdb/taskdatabase/tests/test_path_to_lta.py new file mode 100644 index 0000000000000000000000000000000000000000..fa4b0a9399b3b6f3e26e17318f3effe20aad14ae --- /dev/null +++ b/atdb/taskdatabase/tests/test_path_to_lta.py @@ -0,0 +1,31 @@ +from django.test import TestCase +from taskdatabase.models import Task, Workflow + +class TaskModelTestCase(TestCase): + def setUp(self): + # Create tasks for testing + + # the first 2 have no valid path set + self.task1 = Task.objects.create(sas_id=12345,archive={}) + self.task2 = Task.objects.create(sas_id=12345,archive={'path_to_lta': None}) + + # this task has a valid path_to_lta set + self.task3 = Task.objects.create(sas_id=12345,archive={'path_to_lta': '/sample/path'}) + + # this sasid has no path_to_lta set at all + self.task4 = Task.objects.create(sas_id=66666,archive={}) + self.task5 = Task.objects.create(sas_id=66666,archive={}) + + def test_path_to_lta_with_path(self): + + # if only one of the tasks has a path_to_lta, then the other tasks should also return that path + for task in Task.objects.filter(sas_id=12345): + result = task.sasid_path_to_lta + self.assertEqual(result, '/sample/path') + + def test_path_to_lta_without_path(self): + + # if one of the tasks has 'path_to_lta' set, then return None + for task in Task.objects.filter(sas_id=66666): + result = task.sasid_path_to_lta + self.assertEqual(result, None) \ No newline at end of file