# Path: ogn/parser/parse.py
# def parse(aprs_message, reference_timestamp=None, calculate_relations=False, use_server_timestamp=True):
#     global positions
#     global server_timestamp
# 
#     if use_server_timestamp is True:
#         reference_timestamp = server_timestamp or datetime.utcnow()
#     elif reference_timestamp is None:
#         reference_timestamp = datetime.utcnow()
# 
#     message = parse_aprs(aprs_message, reference_timestamp=reference_timestamp)
#     if message['aprs_type'] == 'position' or message['aprs_type'] == 'status':
#         message.update(parse_comment(message['comment'],
#                                      dstcall=message['dstcall'],
#                                      aprs_type=message['aprs_type']))
# 
#     if message['aprs_type'].startswith('position') and calculate_relations is True:
#         positions[message['name']] = (message['longitude'], message['latitude'])
#         if message['receiver_name'] in positions:
#             cheap_ruler = CheapRuler((message['latitude'] + positions[message['receiver_name']][1]) / 2.0)
#             message['distance'] = cheap_ruler.distance((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['bearing'] = cheap_ruler.bearing((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['normalized_quality'] = normalized_quality(message['distance'], message['signal_quality']) if 'signal_quality' in message else None
# 
#     if message['aprs_type'] == 'server':
#         server_timestamp = message['timestamp']
# 
#     return message
# 
# Path: ogn/parser/exceptions.py
# class AprsParseError(ParseError):
#     """Parse error while parsing an aprs packet."""
#     def __init__(self, aprs_string):
#         self.aprs_string = aprs_string
# 
#         self.message = "This is not a valid APRS packet: {}".format(aprs_string)
#         super(AprsParseError, self).__init__(self.message)


import unittest
import unittest.mock as mock
import os
from datetime import datetime
from time import sleep
from ogn.parser.parse import parse
from ogn.parser.exceptions import AprsParseError

    def test_naviter_beacons(self):
        self.parse_valid_beacon_data_file(filename='naviter.txt', beacon_type='naviter')

    def test_pilot_aware_beacons(self):
        self.parse_valid_beacon_data_file(filename='pilot_aware.txt', beacon_type='pilot_aware')

    def test_skylines_beacons(self):
        self.parse_valid_beacon_data_file(filename='skylines.txt', beacon_type='skylines')

    def test_spider_beacons(self):
        self.parse_valid_beacon_data_file(filename='spider.txt', beacon_type='spider')

    def test_spot_beacons(self):
        self.parse_valid_beacon_data_file(filename='spot.txt', beacon_type='spot')

    def test_generic_beacons(self):
        message = parse("EPZR>WTFDSTCALL,TCPIP*,qAC,GLIDERN1:>093456h this is a comment")
        self.assertEqual(message['beacon_type'], 'unknown')
        self.assertEqual(message['comment'], "this is a comment")

    def test_fail_parse_aprs_none(self):
        with self.assertRaises(TypeError):
            parse(None)

    def test_fail_empty(self):
        with self.assertRaises(AprsParseError):
            parse("")

    def test_fail_bad_string(self):

# Path: ogn/parser/parse.py
# def parse(aprs_message, reference_timestamp=None, calculate_relations=False, use_server_timestamp=True):
#     global positions
#     global server_timestamp
# 
#     if use_server_timestamp is True:
#         reference_timestamp = server_timestamp or datetime.utcnow()
#     elif reference_timestamp is None:
#         reference_timestamp = datetime.utcnow()
# 
#     message = parse_aprs(aprs_message, reference_timestamp=reference_timestamp)
#     if message['aprs_type'] == 'position' or message['aprs_type'] == 'status':
#         message.update(parse_comment(message['comment'],
#                                      dstcall=message['dstcall'],
#                                      aprs_type=message['aprs_type']))
# 
#     if message['aprs_type'].startswith('position') and calculate_relations is True:
#         positions[message['name']] = (message['longitude'], message['latitude'])
#         if message['receiver_name'] in positions:
#             cheap_ruler = CheapRuler((message['latitude'] + positions[message['receiver_name']][1]) / 2.0)
#             message['distance'] = cheap_ruler.distance((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['bearing'] = cheap_ruler.bearing((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['normalized_quality'] = normalized_quality(message['distance'], message['signal_quality']) if 'signal_quality' in message else None
# 
#     if message['aprs_type'] == 'server':
#         server_timestamp = message['timestamp']
# 
#     return message
# 
# Path: ogn/parser/exceptions.py
# class AprsParseError(ParseError):
#     """Parse error while parsing an aprs packet."""
#     def __init__(self, aprs_string):
#         self.aprs_string = aprs_string
# 
#         self.message = "This is not a valid APRS packet: {}".format(aprs_string)
#         super(AprsParseError, self).__init__(self.message)


# Path: tests/parser/test_parse.py
import unittest
import unittest.mock as mock
import os
from datetime import datetime
from time import sleep
from ogn.parser.parse import parse
from ogn.parser.exceptions import AprsParseError


    def test_naviter_beacons(self):
        self.parse_valid_beacon_data_file(filename='naviter.txt', beacon_type='naviter')

    def test_pilot_aware_beacons(self):
        self.parse_valid_beacon_data_file(filename='pilot_aware.txt', beacon_type='pilot_aware')

    def test_skylines_beacons(self):
        self.parse_valid_beacon_data_file(filename='skylines.txt', beacon_type='skylines')

    def test_spider_beacons(self):
        self.parse_valid_beacon_data_file(filename='spider.txt', beacon_type='spider')

    def test_spot_beacons(self):
        self.parse_valid_beacon_data_file(filename='spot.txt', beacon_type='spot')

    def test_generic_beacons(self):
        message = parse("EPZR>WTFDSTCALL,TCPIP*,qAC,GLIDERN1:>093456h this is a comment")
        self.assertEqual(message['beacon_type'], 'unknown')
        self.assertEqual(message['comment'], "this is a comment")

    def test_fail_parse_aprs_none(self):
        with self.assertRaises(TypeError):
            parse(None)

    def test_fail_empty(self):
        with self.assertRaises(AprsParseError):
            parse("")

    def test_fail_bad_string(self):

        with self.assertRaises(AprsParseError):