import unittest
import src.services.tracker.satellite_tracker as sat_tracker


class TestSatelliteTracker(unittest.TestCase):
    RES = {'info': {'satname': 'LANDSAT 8',
                    'satid': 39084, 'transactionscount': 0},
           'positions': [{'satlatitude': -18.48666077, 'satlongitude': -154.10465247, 'sataltitude': 709.9,
                          'azimuth': 248.66, 'elevation': -43.99, 'ra': 136.94724981, 'dec': -41.05948297,
                          'timestamp': 1649752762, 'eclipsed': True}]}
    FAKE_URL = 'fake_url'

    def test_get_satellite_posiition_ok(self):
        r = sat_tracker.get_satellite_position()
        self.assertIsNotNone(r)

    def test_get_satellite_position_no_ok(self):
        with self.assertRaises(Exception):
            sat_tracker.get_satellite_position(url=self.FAKE_URL)

    def test_local_date_from_timestamp(self):
        sat_tracker.local_date_from_timestamp(self.RES)
        expected = '10:39:22'
        res = self.RES["positions"][0]["date"]
        self.assertEqual(res, expected)

    def test_utc_from_timestamp(self):
        sat_tracker.utc_from_timestamp(self.RES)
        expected = '08:39:22'
        res = self.RES["positions"][0]["utc"]
        self.assertEqual(res, expected)

    def test_km_to_miles(self):
        sat_tracker.km_to_miles(self.RES)
        expected = 441.1112729
        res = self.RES["positions"][0]["sataltitude_miles"]
        self.assertEqual(res, expected)

    def test_format_ra(self):
        sat_tracker.format_ra(self.RES)
        expected = '136:56.0:50.0'
        res = self.RES["positions"][0]["ra"]
        self.assertEqual(res, expected)
        self.RES["positions"][0]["ra"] = 136.94724981  # value changes when test executed, restoring

    def test_format_declination(self):
        sat_tracker.format_declination(self.RES)
        expected = '-2:15:45'
        res = self.RES["positions"][0]["ra"]
        self.assertEqual(res, expected)
        self.RES["positions"][0]["ra"] = 136.94724981  # value changes when test executed, restoring

    def test_update_speed(self):
        sat_tracker.update_speed(self.RES, 0, 0, 0, 0)
        expected = 4.3030690195019656e-07
        res = self.RES["positions"][0]["speed"]
        self.assertEqual(res, expected)


if __name__ == '__main__':
    unittest.main()
