Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
46296531dThis file is pinned to the selected release. Download the original to open it in your design tool.
"""Tests for GPS manager classes in utils/gps_manager.py."""
import json
import os
import threading
import time
from unittest.mock import MagicMock, patch
import pytest
from utils.gps_manager import (
haversine,
GPSState,
SessionStats,
TrackRecorder,
GPSManager,
TCPNMEASource,
SerialNMEASource,
GpsdNMEASource,
)
# ---------------------------------------------------------------------------
# Haversine distance
# ---------------------------------------------------------------------------
class TestHaversine:
def test_same_point(self):
assert haversine(0, 0, 0, 0) == 0.0
def test_known_distance(self):
# Sydney to Melbourne is ~714 km
dist = haversine(-33.8688, 151.2093, -37.8136, 144.9631)
assert 700 < dist < 730
def test_short_distance(self):
# ~111 km per degree of latitude at equator
dist = haversine(0, 0, 1, 0)
assert 110 < dist < 112
def test_across_prime_meridian(self):
dist = haversine(51.5, -0.1, 51.5, 0.1)
assert dist > 0
assert dist < 15 # < 15 km
# ---------------------------------------------------------------------------
# GPSState
# ---------------------------------------------------------------------------
class TestGPSState:
def test_initial_state(self):
state = GPSState()
d = state.get_dict()
assert d['fix_valid'] is False
assert d['latitude'] is None
assert d['speed_kmh'] is None
def test_update_from_rmc_active(self):
state = GPSState()
state.update_from_rmc({
'status': 'A',
'lat': -33.8688,
'lon': 151.2093,
'speed_knots': 5.0,
'heading': 180.0,
})
d = state.get_dict()
assert d['fix_valid'] is True
assert d['latitude'] == pytest.approx(-33.8688)
assert d['speed_kmh'] == pytest.approx(9.3, abs=0.1) # 5 * 1.852
assert d['heading'] == pytest.approx(180.0)
def test_update_from_rmc_void(self):
state = GPSState()
state.update_from_rmc({'status': 'V', 'lat': None, 'lon': None,
'speed_knots': None, 'heading': None})
d = state.get_dict()
assert d['fix_valid'] is False
def test_update_from_gga(self):
state = GPSState()
state.update_from_gga({
'lat': -33.8688,
'lon': 151.2093,
'fix_quality': 1,
'satellites': 12,
'hdop': 0.8,
'altitude': 45.2,
})
d = state.get_dict()
assert d['satellites'] == 12
assert d['hdop'] == 0.8
assert d['altitude'] == 45.2
assert d['fix_valid'] is True
def test_stale_fix_marked_invalid(self):
state = GPSState()
state.update_from_rmc({
'status': 'A',
'lat': -33.8688,
'lon': 151.2093,
'speed_knots': 5.0,
'heading': 180.0,
})
# Manually set last_fix_time to 15 seconds ago
with state._lock:
state.last_fix_time = time.time() - 15
d = state.get_dict()
assert d['fix_valid'] is False
assert d['age_seconds'] > 10
# ---------------------------------------------------------------------------
# SessionStats
# ---------------------------------------------------------------------------
class TestSessionStats:
def test_initial_state(self):
s = SessionStats()
assert s.active is False
assert s.distance_km == 0.0
def test_start_stop(self):
s = SessionStats()
s.start()
assert s.active is True
s.stop()
assert s.active is False
def test_distance_accumulation(self):
s = SessionStats(boom_width_m=12.0)
s.start()
# Move ~111 m north (0.001 degree latitude at equator)
s.update(0.0, 0.0)
s.update(0.001, 0.0)
assert s.distance_km > 0.1 # Should be ~0.111 km
def test_no_accumulation_when_inactive(self):
s = SessionStats()
# Don't call start()
s.update(0.0, 0.0)
s.update(0.001, 0.0)
assert s.distance_km == 0.0
def test_area_calculation(self):
s = SessionStats(boom_width_m=12.0)
s.distance_km = 1.0
# 1 km * 12m / 10 = 1.2 ha
assert s.area_hectares == pytest.approx(1.2)
def test_to_dict(self):
s = SessionStats(boom_width_m=12.0)
d = s.to_dict()
assert 'active' in d
assert 'distance_km' in d
assert 'area_hectares' in d
assert 'boom_width_m' in d
# ---------------------------------------------------------------------------
# TrackRecorder
# ---------------------------------------------------------------------------
class TestTrackRecorder:
def test_start_creates_directory(self, tmp_path):
save_dir = str(tmp_path / 'tracks')
rec = TrackRecorder()
rec.start(save_dir)
assert rec.recording is True
assert os.path.isdir(save_dir)
def test_add_points_and_stop(self, tmp_path):
save_dir = str(tmp_path / 'tracks')
rec = TrackRecorder()
rec.start(save_dir)
# Add several points with sufficient distance and time gaps
points = [
(-33.8688, 151.2093),
(-33.8700, 151.2100),
(-33.8720, 151.2120),
]
for i, (lat, lon) in enumerate(points):
# Override the min interval check for testing
rec._last_record_time = 0
rec.add_point(lat, lon, speed=8.0 + i, heading=90.0 + i)
filepath = rec.stop()
assert rec.recording is False
assert filepath is not None
assert os.path.isfile(filepath)
# Validate GeoJSON structure
with open(filepath) as f:
data = json.load(f)
assert data['type'] == 'FeatureCollection'
assert len(data['features']) == 1
feature = data['features'][0]
assert feature['geometry']['type'] == 'LineString'
assert len(feature['geometry']['coordinates']) == 3
assert feature['properties']['point_count'] == 3
assert len(feature['properties']['speeds_kmh']) == 3
assert len(feature['properties']['timestamps']) == 3
def test_skip_too_close_points(self, tmp_path):
save_dir = str(tmp_path / 'tracks')
rec = TrackRecorder()
rec.start(save_dir)
# Add same point twice (should skip second due to min distance)
rec._last_record_time = 0
rec.add_point(-33.8688, 151.2093, speed=8.0, heading=90.0)
rec._last_record_time = 0 # Reset interval
rec.add_point(-33.8688, 151.2093, speed=8.0, heading=90.0)
rec.stop()
assert len(rec._coordinates) == 1 # Only first recorded
def test_stop_without_start(self):
rec = TrackRecorder()
result = rec.stop()
assert result is None
def test_coordinates_property_returns_copy(self, tmp_path):
save_dir = str(tmp_path / 'tracks')
rec = TrackRecorder()
rec.start(save_dir)
rec._last_record_time = 0
rec.add_point(-33.8688, 151.2093, speed=5.0, heading=90.0)
rec._last_record_time = 0
rec.add_point(-33.8700, 151.2110, speed=6.0, heading=95.0)
snap = rec.coordinates
assert isinstance(snap, list)
assert len(snap) == 2
# Mutating the snapshot must not affect the recorder's buffer
snap.clear()
assert len(rec.coordinates) == 2
def test_concurrent_add_and_read_safe(self, tmp_path):
"""Many concurrent add_point + coordinates reads should not raise."""
save_dir = str(tmp_path / 'tracks')
rec = TrackRecorder()
rec.start(save_dir)
stop_flag = threading.Event()
errors = []
def writer():
i = 0
while not stop_flag.is_set():
try:
rec._last_record_time = 0
rec.add_point(
-33.8688 + i * 0.0001, 151.2093 + i * 0.0001,
speed=5.0, heading=90.0,
)
i += 1
except Exception as e:
errors.append(e)
def reader():
while not stop_flag.is_set():
try:
_ = rec.coordinates
except Exception as e:
errors.append(e)
threads = [threading.Thread(target=writer), threading.Thread(target=reader)]
for t in threads:
t.start()
time.sleep(0.2)
stop_flag.set()
for t in threads:
t.join(timeout=2)
rec.stop()
assert errors == []
# ---------------------------------------------------------------------------
# NMEA source constructors — ensure they instantiate without side-effects
# ---------------------------------------------------------------------------
class TestNMEASources:
def test_tcp_source_defaults(self):
src = TCPNMEASource(on_line=lambda l: None, on_connect_change=lambda b: None, port=8500)
assert src.name == 'tcp'
assert src.port == 8500
assert src._running is False
def test_serial_source_defaults(self):
src = SerialNMEASource(
on_line=lambda l: None, on_connect_change=lambda b: None,
device='/dev/ttyACM0', baudrate=9600,
)
assert src.name == 'serial'
assert src.device == '/dev/ttyACM0'
assert src.baudrate == 9600
def test_gpsd_source_defaults(self):
src = GpsdNMEASource(on_line=lambda l: None, on_connect_change=lambda b: None)
assert src.name == 'gpsd'
assert src.host == 'localhost'
assert src.port == 2947
def test_serial_source_without_pyserial(self, monkeypatch):
"""If pyserial is missing, the source should log and exit cleanly, not crash."""
import utils.gps_manager as gm
monkeypatch.setattr(gm, 'serial', None)
called = {'line': False, 'connect': False}
src = SerialNMEASource(
on_line=lambda l: called.__setitem__('line', True),
on_connect_change=lambda b: called.__setitem__('connect', True),
device='/dev/nonexistent', baudrate=9600,
)
src.start()
# Give the background thread a moment; it should exit immediately.
time.sleep(0.1)
# Never produced a line or a connected event
assert called['line'] is False
# ---------------------------------------------------------------------------
# GPSManager — source selection
# ---------------------------------------------------------------------------
class TestGPSManagerSourceSelection:
def test_default_is_tcp(self):
mgr = GPSManager(source='tcp', port=18500)
assert mgr.source_name == 'tcp'
assert isinstance(mgr._source, TCPNMEASource)
state = mgr.get_state()
assert state['connection']['source'] == 'tcp'
assert 'gps_connected' in state['connection']
assert 'tcp_connected' not in state['connection']
def test_serial_source(self):
mgr = GPSManager(source='serial', serial_device='/dev/ttyACM0', baudrate=9600)
assert mgr.source_name == 'serial'
assert isinstance(mgr._source, SerialNMEASource)
assert mgr._source.device == '/dev/ttyACM0'
def test_gpsd_source(self):
mgr = GPSManager(source='gpsd')
assert mgr.source_name == 'gpsd'
assert isinstance(mgr._source, GpsdNMEASource)
def test_case_insensitive(self):
mgr = GPSManager(source='SERIAL')
assert mgr.source_name == 'serial'
def test_invalid_source_raises(self):
with pytest.raises(ValueError):
GPSManager(source='bogus')
def test_process_sentence_updates_state(self, tmp_path):
"""GPSManager routes parsed sentences through its state, regardless of source."""
mgr = GPSManager(source='tcp', port=18501, track_dir=str(tmp_path))
# Feed a valid NMEA sentence via the internal callback
mgr._on_line('$GPRMC,123519,A,3347.2000,S,15112.0000,E,5.0,180.0,230394,,,A*4C')
snap = mgr.state.get_dict()
# We don't care about the exact coord — just that a fix was derived.
assert snap['latitude'] is not None or snap['longitude'] is not None or snap['fix_valid'] is False
def test_on_connect_change_updates_state(self):
mgr = GPSManager(source='tcp')
assert mgr.state.connected is False
mgr._on_connect_change(True)
assert mgr.state.connected is True
mgr._on_connect_change(False)
assert mgr.state.connected is False