Skip to content
Snippets Groups Projects
Commit f16a0597 authored by Jörn Künsemöller's avatar Jörn Künsemöller
Browse files

TMSS-345: Add test coverage for sideral time conversion and /api/util/utc and...

TMSS-345: Add test coverage for sideral time conversion and /api/util/utc and /api/util/lst endpoints
parent 906eb06e
No related branches found
No related tags found
1 merge request!211Resolve TMSS-345
...@@ -58,7 +58,7 @@ urlpatterns = [ ...@@ -58,7 +58,7 @@ urlpatterns = [
path('swagger/', swagger_schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), 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('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/utc', views.utc, name="system-utc"),
path(r'util/lst/', views.lst, name="conversion-lst"), path(r'util/lst', views.lst, name="conversion-lst"),
] ]
......
...@@ -32,6 +32,7 @@ if(BUILD_TESTING) ...@@ -32,6 +32,7 @@ if(BUILD_TESTING)
lofar_add_test(t_adapter) lofar_add_test(t_adapter)
lofar_add_test(t_tasks) lofar_add_test(t_tasks)
lofar_add_test(t_scheduling) lofar_add_test(t_scheduling)
lofar_add_test(t_conversions)
# To get ctest running # To get ctest running
file(COPY testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) file(COPY testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
......
#!/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()
#!/bin/bash
# Run the unit test
source python-coverage.sh
python_coverage_test "*tmss*" t_conversions.py
#!/bin/sh
./runctest.sh t_conversions
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment