From 277e082c476c13cd80d40c424e1707f2d476b839 Mon Sep 17 00:00:00 2001
From: Vermaas <vermaas@astron.nl>
Date: Tue, 16 Jan 2024 09:12:35 +0100
Subject: [PATCH] add unittests

---
 atdb/taskdatabase/models.py                 | 11 ++++----
 atdb/taskdatabase/tests/test_path_to_lta.py | 31 +++++++++++++++++++++
 2 files changed, 37 insertions(+), 5 deletions(-)
 create mode 100644 atdb/taskdatabase/tests/test_path_to_lta.py

diff --git a/atdb/taskdatabase/models.py b/atdb/taskdatabase/models.py
index bd3fb485..57703466 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 00000000..fa4b0a93
--- /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
-- 
GitLab