From f16a05975c790b29ecbaab7cf9c14f6e9713fdc7 Mon Sep 17 00:00:00 2001
From: jkuensem <jkuensem@physik.uni-bielefeld.de>
Date: Tue, 8 Sep 2020 17:26:34 +0200
Subject: [PATCH] TMSS-345: Add test coverage for sideral time conversion and
 /api/util/utc and /api/util/lst endpoints

---
 SAS/TMSS/src/tmss/urls.py       |   2 +-
 SAS/TMSS/test/CMakeLists.txt    |   1 +
 SAS/TMSS/test/t_conversions.py  | 133 ++++++++++++++++++++++++++++++++
 SAS/TMSS/test/t_conversions.run |   6 ++
 SAS/TMSS/test/t_conversions.sh  |   3 +
 5 files changed, 144 insertions(+), 1 deletion(-)
 create mode 100755 SAS/TMSS/test/t_conversions.py
 create mode 100755 SAS/TMSS/test/t_conversions.run
 create mode 100755 SAS/TMSS/test/t_conversions.sh

diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py
index 23bd6af6992..9b7895326ff 100644
--- a/SAS/TMSS/src/tmss/urls.py
+++ b/SAS/TMSS/src/tmss/urls.py
@@ -58,7 +58,7 @@ urlpatterns = [
     path('swagger/', swagger_schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
     path('redoc/', swagger_schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
     path(r'util/utc', views.utc, name="system-utc"),
-    path(r'util/lst/', views.lst, name="conversion-lst"),
+    path(r'util/lst', views.lst, name="conversion-lst"),
 ]
 
 
diff --git a/SAS/TMSS/test/CMakeLists.txt b/SAS/TMSS/test/CMakeLists.txt
index b19ddcd546e..769fce231ac 100644
--- a/SAS/TMSS/test/CMakeLists.txt
+++ b/SAS/TMSS/test/CMakeLists.txt
@@ -32,6 +32,7 @@ if(BUILD_TESTING)
     lofar_add_test(t_adapter)
     lofar_add_test(t_tasks)
     lofar_add_test(t_scheduling)
+    lofar_add_test(t_conversions)
 
     # To get ctest running
     file(COPY testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
diff --git a/SAS/TMSS/test/t_conversions.py b/SAS/TMSS/test/t_conversions.py
new file mode 100755
index 00000000000..ccd4025f6c4
--- /dev/null
+++ b/SAS/TMSS/test/t_conversions.py
@@ -0,0 +1,133 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2018    ASTRON (Netherlands Institute for Radio Astronomy)
+# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This file is part of the LOFAR software suite.
+# The LOFAR software suite is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# The LOFAR software suite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+
+# $Id:  $
+
+import os
+import unittest
+import datetime
+import logging
+import requests
+import dateutil.parser
+import astropy.coordinates
+
+logger = logging.getLogger(__name__)
+logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
+from lofar.sas.tmss.tmss.tmssapp.conversions import local_sidereal_time_for_utc_and_station, local_sidereal_time_for_utc_and_longitude
+
+# Do Mandatory setup step:
+# use setup/teardown magic for tmss test database, ldap server and django server
+# (ignore pycharm unused import statement, python unittests does use at RunTime the tmss_test_environment_unittest_setup module)
+from lofar.sas.tmss.test.tmss_test_environment_unittest_setup import *
+
+class SiderealTime(unittest.TestCase):
+
+    def test_local_sidereal_time_for_utc_and_longitude_returns_correct_result(self):
+        # test result against known correct value
+        lst = local_sidereal_time_for_utc_and_longitude(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0))
+        self.assertEqual(str(lst), '19h09m54.9567s')
+
+    def test_local_sidereal_time_for_utc_and_longitude_considers_timestamp(self):
+        # test that the results differ for different timestamps
+        lst1 = local_sidereal_time_for_utc_and_longitude(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0))
+        lst2 = local_sidereal_time_for_utc_and_longitude(timestamp=datetime.datetime(year=2020, month=1, day=2, hour=12, minute=0, second=0))
+        self.assertNotEqual(str(lst1), str(lst2))
+
+    def test_local_sidereal_time_for_utc_and_longitude_considers_longitude(self):
+        # test that the results differ for different longitudes
+        lst1 = local_sidereal_time_for_utc_and_longitude(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0), longitude=6.789)
+        lst2 = local_sidereal_time_for_utc_and_longitude(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0), longitude=6.123)
+        self.assertNotEqual(str(lst1), str(lst2))
+
+    def test_local_sidereal_time_for_utc_and_station_returns_correct_result(self):
+        # assert result against known correct value
+        lst = local_sidereal_time_for_utc_and_station(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0))
+        self.assertEqual(str(lst), '19h09m55.0856s')
+
+    def test_local_sidereal_time_for_utc_and_station_considers_timestamp(self):
+        # test that the results differ for different timestamps
+        lst1 = local_sidereal_time_for_utc_and_station(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0))
+        lst2 = local_sidereal_time_for_utc_and_station(timestamp=datetime.datetime(year=2020, month=1, day=2, hour=12, minute=0, second=0))
+        self.assertNotEqual(str(lst1), str(lst2))
+
+    def test_local_sidereal_time_for_utc_and_station_considers_station(self):
+        # test that the results differ for different stations
+        lst1 = local_sidereal_time_for_utc_and_station(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0), station="CS002")
+        lst2 = local_sidereal_time_for_utc_and_station(timestamp=datetime.datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0), station="DE602")
+        self.assertNotEqual(str(lst1), str(lst2))
+
+
+class UtilREST(unittest.TestCase):
+
+    def test_util_utc_returns_timestamp(self):
+
+        # assert local clock differs not too much from returned TMSS system clock
+        r = requests.get(BASE_URL + '/util/utc', auth=AUTH)
+        self.assertEqual(r.status_code, 200)
+        returned_datetime = dateutil.parser.parse(r.content.decode('utf8'))
+        current_datetime = datetime.datetime.utcnow()
+        delta = abs((returned_datetime - current_datetime).total_seconds())
+        self.assertTrue(delta < 60.0)
+
+    def test_util_lst_returns_longitude(self):
+
+        # assert returned value is a parseable hms value
+        for query in ['/util/lst',
+                      '/util/lst?timestamp=2020-01-01T12:00:00',
+                      '/util/lst?timestamp=2020-01-01T12:00:00&longitude=54.321',
+                      '/util/lst?timestamp=2020-01-01T12:00:00&station=DE609']:
+            r = requests.get(BASE_URL + query, auth=AUTH)
+            self.assertEqual(r.status_code, 200)
+            lon_str = r.content.decode('utf8')
+            lon_obj = astropy.coordinates.Longitude(lon_str)
+            self.assertEqual(str(lon_obj), lon_str)
+
+    def test_util_lst_considers_timestamp(self):
+
+        # assert returned value matches known result for given timestamp
+        r = requests.get(BASE_URL + '/util/lst?timestamp=2020-01-01T12:00:00', auth=AUTH)
+        self.assertEqual(r.status_code, 200)
+        lon_str = r.content.decode('utf8')
+        self.assertEqual('19h09m55.0856s', lon_str)
+
+    def test_util_lst_considers_station(self):
+
+        # assert returned value differs when a different station is given
+        r1 = requests.get(BASE_URL + '/util/lst', auth=AUTH)
+        r2 = requests.get(BASE_URL + '/util/lst?station=DE602', auth=AUTH)
+        self.assertEqual(r1.status_code, 200)
+        self.assertEqual(r2.status_code, 200)
+        lon_str1 = r1.content.decode('utf8')
+        lon_str2 = r2.content.decode('utf8')
+        self.assertNotEqual(lon_str1, lon_str2)
+
+    def test_util_lst_considers_longitude(self):
+        # assert returned value differs when a different station is given
+        r1 = requests.get(BASE_URL + '/util/lst', auth=AUTH)
+        r2 = requests.get(BASE_URL + '/util/lst?longitude=12.345', auth=AUTH)
+        self.assertEqual(r1.status_code, 200)
+        self.assertEqual(r2.status_code, 200)
+        lon_str1 = r1.content.decode('utf8')
+        lon_str2 = r2.content.decode('utf8')
+        self.assertNotEqual(lon_str1, lon_str2)
+
+
+if __name__ == "__main__":
+    os.environ['TZ'] = 'UTC'
+    unittest.main()
diff --git a/SAS/TMSS/test/t_conversions.run b/SAS/TMSS/test/t_conversions.run
new file mode 100755
index 00000000000..d7c74389715
--- /dev/null
+++ b/SAS/TMSS/test/t_conversions.run
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+# Run the unit test
+source python-coverage.sh
+python_coverage_test "*tmss*" t_conversions.py
+
diff --git a/SAS/TMSS/test/t_conversions.sh b/SAS/TMSS/test/t_conversions.sh
new file mode 100755
index 00000000000..c95892264d5
--- /dev/null
+++ b/SAS/TMSS/test/t_conversions.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./runctest.sh t_conversions
\ No newline at end of file
-- 
GitLab