def test_computeViewReturnsCorrectDataAccordingToTheDate(self): """ Check if the view returns the correct data according to the date """ tle = TLE.objects.findByCatalogEntryAndTime( CatalogEntry.objects.first(), format_inline_time('20170825200000')) sc = SatelliteComputation(tle=tle) sc.observer.date = '2017/8/25 20:00:00' expected_data_1 = sc.compute() sc.observer.date = '2017/8/25 20:00:01' expected_data_2 = sc.compute() response = self.client.get( '/api/v1/compute/25544/?time=20170825200000') content = response.content.decode('utf8') json_data_1 = json.loads(content) del json_data_1['tle'] response = self.client.get( '/api/v1/compute/25544/?time=20170825200001') content = response.content.decode('utf8') json_data_2 = json.loads(content) del json_data_2['tle'] self.assertEquals(json_data_1, expected_data_1) self.assertEquals(json_data_2, expected_data_2)
def test_calcOrbitalProducesCorrectOutput(self): expected_velocities = { 100: 7849.1108, 200: 7789.2220, 300: 7730.6834, 400: 7673.4451, 1000: 7354.8236, 2000: 6901.9526, } results = { 100: 0, 200: 0, 300: 0, 400: 0, 1000: 0, 2000: 0, } sc = SatelliteComputation(tle=TLE.objects.first()) for km in expected_velocities: results[km] = round(sc._calc_orbital_velocity(km * 1000), 4) self.assertEquals(expected_velocities, results)
def test_calcOrbitalVelocityTakesAParameter(self): sc = SatelliteComputation(tle=TLE.objects.first()) try: sc._calc_orbital_velocity(0) except TypeError: self.fail("method _calc_orbital_velocity must take a parameter")
def get(self, request, satellite_number, format=None): entry = get_object_or_404(CatalogEntry, norad_catalog_number=satellite_number) try: time = format_inline_time(request.GET.get('time', None)) except ValueError: return Response({'detail': 'The given time is not correct'}, status=status.HTTP_400_BAD_REQUEST) try: tle = TLE.objects.findByCatalogEntryAndTime(entry, time) except: return Response( {'detail': 'No TLE corresponding to the given date.'}, status=status.HTTP_400_BAD_REQUEST) sc = SatelliteComputation(tle=tle) sc.observer.date = time try: data = sc.compute() except ValueError as e: return Response({'detail': '{0}'.format(e)}, status=status.HTTP_400_BAD_REQUEST) return Response({ 'longitude': data['longitude'], 'latitude': data['latitude'], 'elevation': data['elevation'], 'velocity': data['velocity'], 'tle': tle.id, })
def test_calcOrbitalVelocityExists(self): sc = SatelliteComputation(tle=TLE.objects.first()) try: sc._calc_orbital_velocity() except AttributeError: self.fail("method _calc_orbital_velocity does not exist") except TypeError: pass
def test_methodComputeOutputsDataAsDict(self): sc = SatelliteComputation(tle=TLE.objects.first()) # Put the observer on a fixed date to avoid the test to fail while # running tests after the TLE expires sc.observer.date = '2017/8/25 20:00:00' data = sc.compute() self.assertTrue(isinstance(data, dict), "output is not a dict")
def test_calcOrbitalVelocityRaisesACorrectExceptionIfAltitudeIsNotANumber( self): sc = SatelliteComputation(tle=TLE.objects.first()) try: sc._calc_orbital_velocity(None) self.fail( "method _calc_orbital_velocity can't accept a non number") except TypeError as e: self.assertEquals("altitude must be a number", str(e))
def test_checkMethodComputeExists(self): sc = SatelliteComputation(tle=TLE.objects.first()) # Put the observer on a fixed date to avoid the test to fail while # running tests after the TLE expires sc.observer.date = '2017/8/25 20:00:00' try: sc.compute() except AttributeError: self.fail("compute() method does not exist")
def test_calcOrbitalVelocityRaisesACorrectExceptionIfAltitudeIsNegative( self): sc = SatelliteComputation(tle=TLE.objects.first()) try: sc._calc_orbital_velocity(-1) self.fail( "method _calc_orbital_velocity can't accept a negaitive number" ) except ValueError as e: self.assertEquals("altitude must be positive", str(e))
def test_methodComputeOutputsWellFormedDict(self): sc = SatelliteComputation(tle=TLE.objects.first()) # Put the observer on a fixed date to avoid the test to fail while # running tests after the TLE expires sc.observer.date = '2017/8/25 20:00:00' data = sc.compute() expected_keys = ['longitude', 'latitude', 'elevation', 'velocity'] for key in expected_keys: self.assertIn(key, data)
def test_observerPropertyExists(self): sc = SatelliteComputation(tle=TLE.objects.first()) try: sc.observer except AttributeError: self.fail("observer attribute does not exist")
def test_methodComputOutputsPyEphemDataAndCorrectVelocity(self): expected_data = { 'elevation': 406818.65625, 'latitude': 49.48508595186298, 'longitude': -156.969959505129, 'velocity': 7669.588322702025, } sc = SatelliteComputation(tle=TLE.objects.first()) # Put the observer on a fixed date to avoid the test to fail while # running tests after the TLE expires sc.observer.date = '2017/8/25 20:00:00' data = sc.compute() self.assertEquals(expected_data, data)
def test_computeViewReturnsSameDataAsSatelliteComputation(self): """ Check if the view returns the same data as SatelliteComputation tool """ tle = TLE.objects.findByCatalogEntryAndTime( CatalogEntry.objects.first(), format_inline_time('20170825200000')) sc = SatelliteComputation(tle=tle) # Put the observer on a fixed date to avoid the test to fail while # running after the TLE expires sc.observer.date = '2017/8/25 20:00:00' response = self.client.get( '/api/v1/compute/25544/?time=20170825200000') content = response.content.decode('utf8') json_data = json.loads(content) del json_data['tle'] expected_data = sc.compute() self.assertEquals(json_data, expected_data)
def test_emptyConstructorRaisesCorrectException(self): try: SatelliteComputation() self.fail("SatelliteComputation() can take no parameter") except TypeError as e: self.assertEquals("tle parameter is missing", str(e))
def test_constructorCanTakeAWellFormedTleParameter(self): try: SatelliteComputation(tle=TLE.objects.first()) except TypeError: self.fail("SatelliteComputation() can't take a 'tle' parameter")
def test_observerPropertyIsAPyEphemObserver(self): sc = SatelliteComputation(tle=TLE.objects.first()) self.assertTrue(isinstance(sc.observer, ephem.Observer), "observer is not of type ephem.Observer")
def test_constructorThrowsCorrectExceptionIfTheParamIsNotATle(self): try: SatelliteComputation(tle=None) self.fail("SatelliteComputation() can take a non TLE parameter") except TypeError as e: self.assertEquals("tle must be of type TLE", str(e))
def test_constructorThrowsCorrectExceptionIfTheTleIsInvalid(self): try: SatelliteComputation(tle=TLE()) self.fail("SatelliteComputation() can take an empty TLE") except ValueError as e: self.assertEquals("invalid TLE", str(e))