From 88bbd085d6d30bd26cf21b4d561204e63a91f2d1 Mon Sep 17 00:00:00 2001
From: Mario Raciti <mario.raciti@inaf.it>
Date: Thu, 8 Apr 2021 15:29:40 +0200
Subject: [PATCH] TMSS-692: Add test for cancelled SUB; refactoring

---
 SAS/TMSS/backend/test/t_adapter.py | 56 ++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/SAS/TMSS/backend/test/t_adapter.py b/SAS/TMSS/backend/test/t_adapter.py
index 72596f5ceb9..9ac0b747fce 100755
--- a/SAS/TMSS/backend/test/t_adapter.py
+++ b/SAS/TMSS/backend/test/t_adapter.py
@@ -526,25 +526,47 @@ class ProjectReportTest(unittest.TestCase):
         self.test_data_creator = TMSSRESTTestDataCreator(BASE_URL, requests.auth.HTTPBasicAuth('test', 'test'))
         response = requests.get(self.test_data_creator.django_api_url + '/', auth=self.test_data_creator.auth)
 
-    def test_create_project_report(self):
+    def _get_SUB_with_subtask_and_set_status(self, status=None):
+        """
+        Help method to create SUB, TaskBlueprint, Subtask and (optionally) set the latter's status.
+        """
+        sub = models.SchedulingUnitBlueprint.objects.create(**SchedulingUnitBlueprint_test_data(draft=self.scheduling_unit_draft))
+        tb = models.TaskBlueprint.objects.create(**TaskBlueprint_test_data(task_draft=self.task_draft, scheduling_unit_blueprint=sub))
         # Create Subtask of type 'ingest'
         subtask_template = models.SubtaskTemplate.objects.create(**SubtaskTemplate_test_data(subtask_type_value='ingest'))
-        subtask = models.Subtask.objects.create(**Subtask_test_data(task_blueprint=self.task_blueprint, subtask_template=subtask_template))
+        subtask = models.Subtask.objects.create(**Subtask_test_data(task_blueprint=tb, subtask_template=subtask_template))
 
-        # Set Subtask status to 'finished'
-        with tmss_test_env.create_tmss_client() as client:
-            client.set_subtask_status(subtask.pk, 'finished')
-
-        # Refreshing Subtask from cache.
-        subtask = models.Subtask.objects.get(pk=subtask.pk)
-        while subtask.state.value != 'finished':
+        if status:  # Set Subtask status to 'cancelled'
+            with tmss_test_env.create_tmss_client() as client:
+                client.set_subtask_status(subtask.pk, status)
+            # Refreshing Subtask from cache
             subtask = models.Subtask.objects.get(pk=subtask.pk)
+            while subtask.state.value != status:
+                subtask = models.Subtask.objects.get(pk=subtask.pk)
 
-        # Create SubtaskOutput and Dataproducts
-        subtask_output = models.SubtaskOutput.objects.create(**SubtaskOutput_test_data(subtask=subtask))
+        return sub, tb, subtask
+
+    def test_create_project_report(self):
+        """
+        Test create project extra action.
+        """
+        # Create and set three SUBs and respectively set the following states: 'finished', 'cancelled', 'defined' (not cancelled)
+        succeeded_sub, _, succeeded_subtask = self._get_SUB_with_subtask_and_set_status('finished')
+        cancelled_sub, _, cancelled_subtask = self._get_SUB_with_subtask_and_set_status('cancelled')
+        not_cancelled_sub, _, not_cancelled_subtask = self._get_SUB_with_subtask_and_set_status('defined')
+
+        # Create SubtaskOutput and Dataproducts from subtask_output
+        subtask_output = models.SubtaskOutput.objects.create(**SubtaskOutput_test_data(subtask=succeeded_subtask))
         dataproduct1 = models.Dataproduct.objects.create(**Dataproduct_test_data(producer=subtask_output))
         dataproduct2 = models.Dataproduct.objects.create(**Dataproduct_test_data(producer=subtask_output))
 
+        # Calculate expected durations
+        total = succeeded_subtask.duration.total_seconds() + cancelled_subtask.duration.total_seconds() + \
+                not_cancelled_subtask.duration.total_seconds()
+        total_succeeded = succeeded_subtask.duration.total_seconds()
+        total_not_cancelled = succeeded_subtask.duration.total_seconds() + not_cancelled_subtask.duration.total_seconds()
+        total_failed = cancelled_subtask.duration.total_seconds()
+
         # Assert we get the expected object
         response = requests.get(BASE_URL + '/project/%s/report' % self.project.pk, auth=self.test_data_creator.auth)
         result = response.json()
@@ -554,19 +576,15 @@ class ProjectReportTest(unittest.TestCase):
         self.assertEqual(result['quota'][0]['id'], self.project_quota.pk)
 
         # Assert durations are well calculated
-        total = subtask.duration.total_seconds()
         self.assertEqual(result['durations']['total'], total)
-        total_succeeded = subtask.duration.total_seconds()
         self.assertEqual(result['durations']['total_succeeded'], total_succeeded)
-        total_not_cancelled = subtask.duration.total_seconds()
         self.assertEqual(result['durations']['total_not_cancelled'], total_not_cancelled)
-        total_failed = timedelta(seconds=0).total_seconds()
         self.assertEqual(result['durations']['total_failed'], total_failed)
 
-        # There is only this finished SUB
-        self.assertEqual(result['durations']['scheduling_unit_blueprints_finished'][0]['id'], self.scheduling_unit_blueprint.pk)
-        # TODO: Assert at least one failed SUB
-        self.assertEqual(result['durations']['scheduling_unit_blueprints_failed'], [])
+        # There is only one finished SUB
+        self.assertEqual(result['durations']['scheduling_unit_blueprints_finished'][0]['id'], succeeded_sub.pk)
+        # There is only one cancelled SUB
+        self.assertEqual(result['durations']['scheduling_unit_blueprints_failed'][0]['id'], cancelled_sub.pk)
 
         # There are just two dataproducts
         self.assertEqual(result['LTA dataproducts']['size__sum'], dataproduct1.size + dataproduct2.size)
-- 
GitLab