Files
aprsd/tests/packets/test_beacon_packet.py
hemna f30f7640ac refactor: compose Position value object into GPSPacket
Replace the flat GPS fields on GPSPacket and its subclasses with a
composed Position value object (has-a instead of is-a), collapsing the
Packet -> GPSPacket -> {Beacon, MicE, Telemetry, Object, Weather}
data spine.  Position owns the coordinates, symbol, movement/PHG data
and the conversion/formatting logic (zulu time, DDM, human_info).

- GPSPacket holds position: Optional[Position]; subclass defaults
  preserved via _position_default() (ObjectPacket 'r', WeatherPacket '_')
- to_dict()/from_dict() flatten/unflatten so the on-disk JSON format
  is unchanged; verified via objectstore save/load round-trip
- GPSPacket inherits DataClassJsonMixin directly because the
  @dataclass_json decorator overwrites to_dict()/from_dict() and would
  clobber the flattening overrides
- constructor is breaking: position=Position(...) instead of
  latitude=.../longitude=...; updated tx.py, log.py and all tests

Full test suite (775) passes; ruff format + lint clean.
2026-09-24 10:19:35 -04:00

105 lines
4.4 KiB
Python

import json
import unittest
import aprslib
from aprsd import packets
from tests import fake
class TestBeaconPacket(unittest.TestCase):
"""Test BeaconPacket JSON serialization."""
def test_beacon_packet_to_json(self):
"""Test BeaconPacket.to_json() method."""
packet = packets.BeaconPacket(
from_call=fake.FAKE_FROM_CALLSIGN,
to_call=fake.FAKE_TO_CALLSIGN,
position=packets.Position(
latitude=37.7749,
longitude=-122.4194,
symbol='>',
symbol_table='/',
comment='Test beacon comment',
),
)
json_str = packet.to_json()
self.assertIsInstance(json_str, str)
json_dict = json.loads(json_str)
self.assertEqual(json_dict['_type'], 'BeaconPacket')
self.assertEqual(json_dict['from_call'], fake.FAKE_FROM_CALLSIGN)
self.assertEqual(json_dict['to_call'], fake.FAKE_TO_CALLSIGN)
self.assertEqual(json_dict['latitude'], 37.7749)
self.assertEqual(json_dict['longitude'], -122.4194)
self.assertEqual(json_dict['symbol'], '>')
self.assertEqual(json_dict['symbol_table'], '/')
self.assertEqual(json_dict['comment'], 'Test beacon comment')
def test_beacon_packet_from_dict(self):
"""Test BeaconPacket.from_dict() method."""
packet_dict = {
'_type': 'BeaconPacket',
'from_call': fake.FAKE_FROM_CALLSIGN,
'to_call': fake.FAKE_TO_CALLSIGN,
'latitude': 37.7749,
'longitude': -122.4194,
'symbol': '>',
'symbol_table': '/',
'comment': 'Test beacon comment',
}
packet = packets.BeaconPacket.from_dict(packet_dict)
self.assertIsInstance(packet, packets.BeaconPacket)
self.assertEqual(packet.from_call, fake.FAKE_FROM_CALLSIGN)
self.assertEqual(packet.to_call, fake.FAKE_TO_CALLSIGN)
self.assertEqual(packet.position.latitude, 37.7749)
self.assertEqual(packet.position.longitude, -122.4194)
self.assertEqual(packet.position.symbol, '>')
self.assertEqual(packet.position.symbol_table, '/')
self.assertEqual(packet.position.comment, 'Test beacon comment')
def test_beacon_packet_round_trip(self):
"""Test BeaconPacket round-trip: to_json -> from_dict."""
original = packets.BeaconPacket(
from_call=fake.FAKE_FROM_CALLSIGN,
to_call=fake.FAKE_TO_CALLSIGN,
position=packets.Position(
latitude=37.7749,
longitude=-122.4194,
symbol='>',
symbol_table='/',
comment='Test beacon comment',
),
)
json_str = original.to_json()
packet_dict = json.loads(json_str)
restored = packets.BeaconPacket.from_dict(packet_dict)
self.assertEqual(restored.from_call, original.from_call)
self.assertEqual(restored.to_call, original.to_call)
self.assertEqual(restored.position.latitude, original.position.latitude)
self.assertEqual(restored.position.longitude, original.position.longitude)
self.assertEqual(restored.position.symbol, original.position.symbol)
self.assertEqual(restored.position.symbol_table, original.position.symbol_table)
self.assertEqual(restored.position.comment, original.position.comment)
self.assertEqual(restored._type, original._type)
def test_beacon_packet_from_raw_string(self):
"""Test BeaconPacket creation from raw APRS string."""
# Use a format that aprslib can parse correctly
packet_raw = 'kd8mey-10>APRS,TCPIP*,qAC,T2SYDNEY:=4247.80N/08539.00WrPHG1210/Making 220 Great Again Allstar# 552191'
packet_dict = aprslib.parse(packet_raw)
packet = packets.factory(packet_dict)
self.assertIsInstance(packet, packets.BeaconPacket)
# Test to_json
json_str = packet.to_json()
self.assertIsInstance(json_str, str)
json_dict = json.loads(json_str)
self.assertEqual(json_dict['_type'], 'BeaconPacket')
# Test from_dict round trip
restored = packets.factory(json_dict)
self.assertIsInstance(restored, packets.BeaconPacket)
self.assertEqual(restored.from_call, packet.from_call)
self.assertEqual(restored.position.latitude, packet.position.latitude)
self.assertEqual(restored.position.longitude, packet.position.longitude)