2016-02-28 11:11:16 +00:00
import unittest . mock as mock
2025-04-23 09:57:10 +00:00
import pytest
2017-09-27 18:23:43 +00:00
import os
2016-02-28 11:11:16 +00:00
from datetime import datetime
from time import sleep
2017-09-30 16:25:02 +00:00
from ogn . parser . parse import parse
2025-04-19 07:06:58 +00:00
from ogn . parser . exceptions import AprsParseError , OgnParseError
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def _parse_valid_beacon_data_file ( filename , beacon_type ) :
with open ( os . path . dirname ( __file__ ) + ' /valid_beacon_data/ ' + filename ) as f :
for line in f :
try :
message = parse ( line , datetime ( 2015 , 4 , 10 , 17 , 0 ) )
assert message is not None
if message [ ' aprs_type ' ] == ' position ' or message [ ' aprs_type ' ] == ' status ' :
assert message [ ' beacon_type ' ] == beacon_type
except NotImplementedError as e :
print ( e )
2016-02-28 11:11:16 +00:00
2017-09-28 06:43:50 +00:00
2025-04-23 09:57:10 +00:00
def test_aprs_aircraft_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' aprs_aircraft.txt ' , beacon_type = ' aprs_aircraft ' )
2017-09-27 20:43:04 +00:00
2018-04-10 16:54:30 +00:00
2025-04-23 09:57:10 +00:00
def test_aprs_receiver_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' aprs_receiver.txt ' , beacon_type = ' aprs_receiver ' )
2017-09-28 20:30:06 +00:00
2025-04-23 09:57:10 +00:00
def test_aprs_fanet_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' fanet.txt ' , beacon_type = ' fanet ' )
2017-09-28 20:30:06 +00:00
2019-06-08 12:48:42 +00:00
2025-04-23 09:57:10 +00:00
def test_flarm_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' flarm.txt ' , beacon_type = ' flarm ' )
2019-06-08 11:56:34 +00:00
2019-06-07 18:34:50 +00:00
2025-04-23 09:57:10 +00:00
def test_receiver_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' receiver.txt ' , beacon_type = ' receiver ' )
2017-09-27 20:43:04 +00:00
2025-04-23 09:57:10 +00:00
def test_tracker_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' tracker.txt ' , beacon_type = ' tracker ' )
2020-05-21 17:30:32 +00:00
2017-09-27 20:43:04 +00:00
2025-04-23 09:57:10 +00:00
def test_capturs_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' capturs.txt ' , beacon_type = ' capturs ' )
2017-09-27 20:43:04 +00:00
2025-04-23 09:57:10 +00:00
def test_flymaster_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' flymaster.txt ' , beacon_type = ' flymaster ' )
2019-06-03 20:17:55 +00:00
2017-05-16 12:36:38 +00:00
2025-04-23 09:57:10 +00:00
def test_inreach_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' inreach.txt ' , beacon_type = ' inreach ' )
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def test_lt24_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' lt24.txt ' , beacon_type = ' lt24 ' )
2017-10-01 20:22:57 +00:00
2025-04-23 09:57:10 +00:00
def test_naviter_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' naviter.txt ' , beacon_type = ' naviter ' )
2018-01-12 07:26:38 +00:00
2025-04-23 09:57:10 +00:00
def test_pilot_aware_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' pilot_aware.txt ' , beacon_type = ' pilot_aware ' )
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def test_skylines_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' skylines.txt ' , beacon_type = ' skylines ' )
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def test_spider_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' spider.txt ' , beacon_type = ' spider ' )
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def test_spot_beacons ( ) :
_parse_valid_beacon_data_file ( filename = ' spot.txt ' , beacon_type = ' spot ' )
2016-02-28 11:11:16 +00:00
2025-04-19 07:06:58 +00:00
2025-04-23 09:57:10 +00:00
def test_generic_beacons ( ) :
message = parse ( " EPZR>WTFDSTCALL,TCPIP*,qAC,GLIDERN1:>093456h this is a comment " )
assert message [ ' beacon_type ' ] == ' unknown '
assert message [ ' comment ' ] == " this is a comment "
2025-04-22 17:22:10 +00:00
2025-04-23 09:57:10 +00:00
def test_fail_parse_aprs_none ( ) :
with pytest . raises ( TypeError ) :
parse ( None )
2016-02-28 11:11:16 +00:00
2025-04-23 09:57:10 +00:00
def test_fail_empty ( ) :
with pytest . raises ( AprsParseError ) :
parse ( " " )
def test_fail_bad_string ( ) :
with pytest . raises ( AprsParseError ) :
parse ( " Lachens>APRS,TCPIwontbeavalidstring " )
def test_v026_chile ( ) :
# receiver beacons from chile have a APRS position message with a pure user comment
message = parse ( " VITACURA1>APRS,TCPIP*,qAC,GLIDERN4:/201146h3322.79SI07034.80W&/A=002329 Vitacura Municipal Aerodrome, Club de Planeadores Vitacura " )
assert message [ ' user_comment ' ] == " Vitacura Municipal Aerodrome, Club de Planeadores Vitacura "
message_with_id = parse ( " ALFALFAL>APRS,TCPIP*,qAC,GLIDERN4:/221830h3330.40SI07007.88W&/A=008659 Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs " )
assert message_with_id [ ' user_comment ' ] == " Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs "
@mock.patch ( ' ogn.parser.parse_module.createTimestamp ' )
def test_default_reference_date ( createTimestamp_mock ) :
valid_aprs_string = " Lachens>APRS,TCPIP*,qAC,GLIDERN2:/165334h4344.70NI00639.19E&/A=005435 v0.2.1 CPU:0.3 RAM:1764.4/2121.4MB NTP:2.8ms/+4.9ppm +47.0C RF:+0.70dB "
parse ( valid_aprs_string )
call_args_before = createTimestamp_mock . call_args
sleep ( 1 )
parse ( valid_aprs_string )
call_args_seconds_later = createTimestamp_mock . call_args
assert call_args_before != call_args_seconds_later
def test_copy_constructor ( ) :
valid_aprs_string = " FLRDDA5BA>APRS,qAS,LFMX:/160829h4415.41N/00600.03E ' 342/049/A=005524 id0ADDA5BA -454fpm -1.1rot 8.8dB 0e +51.2kHz gps4x5 "
message = parse ( valid_aprs_string )
assert message [ ' name ' ] == ' FLRDDA5BA '
assert message [ ' address ' ] == ' DDA5BA '
def test_bad_naviter_format ( ) :
with pytest . raises ( OgnParseError ) :
parse ( " FLRA51D93>OGNAVI,qAS,NAVITER2:/204507h4444.98N/09323.34W ' 000/000/A=000925 !W67! id06A51D93 +000fpm +0.0rot " )
def test_no_receiver ( ) :
result = parse ( " EDFW>OGNSDR:/102713h4949.02NI00953.88E&/A=000984 " )
assert result [ ' aprs_type ' ] == ' position '
assert result [ ' beacon_type ' ] == ' receiver '
assert result [ ' name ' ] == ' EDFW '
assert result [ ' dstcall ' ] == ' OGNSDR '
assert result [ ' receiver_name ' ] is None