From 8fb3991c376ebdc7690b04bf481d92f8fb5cdfc8 Mon Sep 17 00:00:00 2001
From: Vermaas <vermaas@astron.nl>
Date: Fri, 22 Mar 2024 14:48:44 +0100
Subject: [PATCH] add unit tests

---
 .../tests/test_views_diagram_page.py          |  12 --
 .../tests/test_views_discarded_page.py        |  12 --
 .../tests/test_views_failures_page.py         |  11 --
 .../tests/test_views_filter_page.py           |  12 --
 .../tests/test_views_finished_page.py         |  11 --
 .../tests/test_views_get_summary.py           | 104 ++++++++++++++++++
 6 files changed, 104 insertions(+), 58 deletions(-)
 create mode 100644 atdb/taskdatabase/tests/test_views_get_summary.py

diff --git a/atdb/taskdatabase/tests/test_views_diagram_page.py b/atdb/taskdatabase/tests/test_views_diagram_page.py
index aadaf830..49c1175e 100644
--- a/atdb/taskdatabase/tests/test_views_diagram_page.py
+++ b/atdb/taskdatabase/tests/test_views_diagram_page.py
@@ -4,18 +4,6 @@ from django.urls import reverse
 from taskdatabase.models import Task, Workflow
 class DiagramPageViewTest(TestCase):
 
-    @classmethod
-    def setUpTestData(cls):
-
-        # Set up non-modified objects used by all test methods
-        workflow = Workflow()
-        workflow.save()
-
-        # create a list of Tasks
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='stored', workflow = workflow)
-
     def test_url_exists_at_desired_location(self):
         response = self.client.get('/atdb/diagram/')
         self.assertEqual(response.status_code, 200)
diff --git a/atdb/taskdatabase/tests/test_views_discarded_page.py b/atdb/taskdatabase/tests/test_views_discarded_page.py
index 9e79f2a6..da0400cb 100644
--- a/atdb/taskdatabase/tests/test_views_discarded_page.py
+++ b/atdb/taskdatabase/tests/test_views_discarded_page.py
@@ -4,18 +4,6 @@ from django.urls import reverse
 from taskdatabase.models import Task, Workflow
 class DiscardedPageViewTest(TestCase):
 
-    @classmethod
-    def setUpTestData(cls):
-
-        # Set up non-modified objects used by all test methods
-        workflow = Workflow()
-        workflow.save()
-
-        # create a list of Tasks
-        Task.objects.get_or_create(sas_id=12345, status='discarded', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='discarded', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='stored', workflow = workflow)
-
     def test_url_exists_at_desired_location(self):
         response = self.client.get('/atdb/discarded')
         self.assertEqual(response.status_code, 200)
diff --git a/atdb/taskdatabase/tests/test_views_failures_page.py b/atdb/taskdatabase/tests/test_views_failures_page.py
index a2342724..662a4319 100644
--- a/atdb/taskdatabase/tests/test_views_failures_page.py
+++ b/atdb/taskdatabase/tests/test_views_failures_page.py
@@ -4,17 +4,6 @@ from django.urls import reverse
 from taskdatabase.models import Task, Workflow
 class FailuresPageViewTest(TestCase):
 
-    @classmethod
-    def setUpTestData(cls):
-
-        # Set up non-modified objects used by all test methods
-        workflow = Workflow()
-        workflow.save()
-
-        # create a list of Tasks
-        Task.objects.get_or_create(sas_id=12345, status='processed_failed', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='processed_failed', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='stored', workflow = workflow)
 
     def test_url_exists_at_desired_location(self):
         response = self.client.get('/atdb/failures')
diff --git a/atdb/taskdatabase/tests/test_views_filter_page.py b/atdb/taskdatabase/tests/test_views_filter_page.py
index 545c5448..18a81731 100644
--- a/atdb/taskdatabase/tests/test_views_filter_page.py
+++ b/atdb/taskdatabase/tests/test_views_filter_page.py
@@ -4,18 +4,6 @@ from django.urls import reverse
 from taskdatabase.models import Task, Workflow
 class FilterPageViewTest(TestCase):
 
-    @classmethod
-    def setUpTestData(cls):
-
-        # Set up non-modified objects used by all test methods
-        workflow = Workflow()
-        workflow.save()
-
-        # create a list of Tasks
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='stored', workflow = workflow)
-
     def test_url_exists_at_desired_location(self):
         response = self.client.get('/atdb/query/')
         self.assertEqual(response.status_code, 200)
diff --git a/atdb/taskdatabase/tests/test_views_finished_page.py b/atdb/taskdatabase/tests/test_views_finished_page.py
index 3de45fa2..df8ba413 100644
--- a/atdb/taskdatabase/tests/test_views_finished_page.py
+++ b/atdb/taskdatabase/tests/test_views_finished_page.py
@@ -4,17 +4,6 @@ from django.urls import reverse
 from taskdatabase.models import Task, Workflow
 class FinishedPageViewTest(TestCase):
 
-    @classmethod
-    def setUpTestData(cls):
-
-        # Set up non-modified objects used by all test methods
-        workflow = Workflow()
-        workflow.save()
-
-        # create a list of Tasks
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='finished', workflow = workflow)
-        Task.objects.get_or_create(sas_id=12345, status='stored', workflow = workflow)
 
     def test_url_exists_at_desired_location(self):
         response = self.client.get('/atdb/finished')
diff --git a/atdb/taskdatabase/tests/test_views_get_summary.py b/atdb/taskdatabase/tests/test_views_get_summary.py
new file mode 100644
index 00000000..ee64db5c
--- /dev/null
+++ b/atdb/taskdatabase/tests/test_views_get_summary.py
@@ -0,0 +1,104 @@
+from django.test import TestCase
+from django.urls import reverse
+from django.http import JsonResponse, HttpResponse
+
+from taskdatabase.models import Task, Workflow
+import taskdatabase.tests.test_calculated_qualities_outputs as outputs
+import json
+
+class GetSummaryTestCase(TestCase):
+    def setUp(self):
+        print("setUp: Run once for every test method to setup clean data.")
+
+        workflow_requantisation = Workflow(workflow_uri="psrfits_requantisation")
+        workflow_requantisation.save()
+
+        # rfi_percent=0
+        Task.objects.get_or_create(sas_id=54321, status='processed',
+                                   outputs=outputs.default_summary_flavour_with_rfi_percent_zero_1,
+                                   workflow=workflow_requantisation)
+
+        # rfi_percent 11,22,31,52
+        Task.objects.get_or_create(sas_id=54321, status='processed', outputs=outputs.default_summary_flavour_with_rfi_1,
+                                   workflow=workflow_requantisation)
+        Task.objects.get_or_create(sas_id=54321, status='processed', outputs=outputs.default_summary_flavour_with_rfi_2,
+                                   workflow=workflow_requantisation)
+        Task.objects.get_or_create(sas_id=54321, status='processed', outputs=outputs.default_summary_flavour_with_rfi_3,
+                                   workflow=workflow_requantisation)
+        Task.objects.get_or_create(sas_id=54321, status='processed', outputs=outputs.default_summary_flavour_with_rfi_4,
+                                   workflow=workflow_requantisation)
+
+    def test_summary_json_response(self):
+        # Mock request
+        response = self.client.get(reverse('get-summary', args=['54321', 'json']))
+
+        # Check if response is JsonResponse
+        self.assertIsInstance(response, JsonResponse)
+
+    def test_summary_json_contents(self):
+        response = self.client.get(reverse('get-summary', args=['54321', 'json']))
+
+        # Check if response is JsonResponse
+        self.assertIsInstance(response, JsonResponse)
+
+        # Add more assertions as needed
+        json_data = json.loads(response.content.decode('utf-8'))
+
+        # is this json generated for the expected SAS_ID?
+        expected = "Summary File for SAS_ID 54321"
+        actual = json_data['summary']['title']
+        self.assertEqual(expected, actual)
+
+        # are all the tasks in the json?
+        tasks = json_data['summary']['tasks']
+        actual = len(tasks)
+        expected = 5
+        self.assertEqual(expected, actual)
+
+        # check 1 task for correct contents
+        t = tasks[0]
+        self.assertEqual(t['file'], 'L526107_summaryIS.tar')
+        self.assertEqual(t['input_name'], 'L526107_summaryIS.tar')
+        self.assertEqual(t['input_size'], 495749120)
+        self.assertEqual(t['output_size'], 283791360)
+        self.assertEqual(t['size_ratio'], '0.572')
+
+
+    def test_summary_html_response(self):
+        # Mock request
+        response = self.client.get(reverse('get-summary', args=['your_sas_id', 'html']))
+
+        # Check if response is HttpResponse
+        self.assertIsInstance(response, HttpResponse)
+
+
+    def test_summary_html_contents(self):
+        response = self.client.get(reverse('get-summary', args=['54321', 'html']))
+
+        # Check if response is JsonResponse
+        self.assertIsInstance(response, HttpResponse)
+
+        # Add more assertions as needed
+        html_data = response.content.decode('utf-8')
+
+        # is this html generated for the expected SAS_ID?
+        title = "Summary File for SAS_ID 54321"
+        found = False
+        if title in html_data:
+            found = True
+        self.assertEqual(found, True)
+
+        # does this filename exist in the html?
+        input_name = "L526107_summaryIS.tar"
+        found = False
+        if input_name in html_data:
+            found = True
+        self.assertEqual(found, True)
+
+
+    def test_summary_pdf_response(self):
+        # Mock request
+        response = self.client.get(reverse('get-summary', args=['your_sas_id', 'pdf']))
+
+        # Check if response is HttpResponse
+        self.assertIsInstance(response, HttpResponse)
\ No newline at end of file
-- 
GitLab