fix: update tests to match current algorithm — thresholds, _best_climb tuples, ComputedMetrics fields
CI / Python tests (push) Waiting to run
CI / Frontend build (push) Waiting to run

This commit is contained in:
Davide Scaini
2026-06-03 22:18:17 +02:00
parent f167c6eed7
commit 060bdf5114
4 changed files with 36 additions and 29 deletions
+25 -21
View File
@@ -29,13 +29,14 @@ def _pt(offset_s: int, **kw) -> DataPoint:
return DataPoint(timestamp=_ts(offset_s), **kw)
def _activity(points: list[DataPoint], sport: str = "cycling") -> ParsedActivity:
def _activity(points: list[DataPoint], sport: str = "cycling", altitude_source: str = "unknown") -> ParsedActivity:
return ParsedActivity(
points=points,
sport=sport,
started_at=_ts(0),
source_file="test.fit",
source_hash="sha256:abc",
altitude_source=altitude_source,
)
@@ -110,12 +111,13 @@ def test_compute_moving_time_excludes_stops():
def test_compute_elevation_gain():
# Barometric source: no MA smoothing, so even 3 points produce correct gain.
pts = [
_pt(0, lat=48.0, lon=11.0, elevation_m=100.0),
_pt(10, lat=48.001, lon=11.0, elevation_m=150.0),
_pt(20, lat=48.002, lon=11.0, elevation_m=120.0),
]
m = compute(_activity(pts))
m = compute(_activity(pts, altitude_source="barometric"))
assert m.elevation_gain_m == 50.0
assert m.elevation_loss_m == 30.0
@@ -134,8 +136,8 @@ def _ele_pts(elevations: list[float]) -> list[DataPoint]:
def test_elevation_hysteresis_large_step_always_counted():
# A single 50m step is way above any threshold — both sources should count it.
pts = _ele_pts([100.0, 150.0])
# A 50m step with 5 points per level so the GPS moving average doesn't flatten it.
pts = _ele_pts([100.0] * 5 + [150.0] * 5)
gain_baro, _ = _elevation(pts, "barometric")
gain_gps, _ = _elevation(pts, "gps")
assert gain_baro == 50.0
@@ -143,26 +145,25 @@ def test_elevation_hysteresis_large_step_always_counted():
def test_elevation_hysteresis_flat_gps_noise_suppressed():
# Flat coastal route: 16m of GPS noise oscillating within ±8m.
# All steps are sub-1m — hysteresis should return ~0 gain.
import math
# GPS noise within ±0.5m — peak-to-peak 1.0m, well below the 2.0m GPS threshold.
n = 1000
elevations = [100.0 + 3.0 * math.sin(i * 0.1) for i in range(n)]
elevations = [100.0 + 0.5 * math.sin(i * 0.1) for i in range(n)]
pts = _ele_pts(elevations)
gain, loss = _elevation(pts, "gps")
# With threshold=10m no oscillation within ±3m should ever commit.
assert gain == 0.0
assert loss == 0.0
def test_elevation_hysteresis_barometric_threshold_lower():
# Steps of exactly 7m — above barometric (5m) but below GPS (10m) threshold.
elevations = [0.0, 7.0, 0.0, 7.0]
# 1.7m steps at 100m baseline (avoids sensor-dropout suppression which
# skips values near 0): above the 1.5m barometric threshold but, after GPS
# MA smoothing, the effective diff stays below the 2.0m GPS threshold.
elevations = [100.0, 101.7, 100.0, 101.7]
pts = _ele_pts(elevations)
gain_baro, _ = _elevation(pts, "barometric")
gain_gps, _ = _elevation(pts, "gps")
assert gain_baro == 14.0 # both 7m steps committed
assert gain_gps == 0.0 # 7m < 10m threshold suppressed
assert gain_baro == pytest.approx(3.4) # both 1.7m steps committed
assert gain_gps == 0.0 # MA + 2.0m threshold suppresses
def test_elevation_hysteresis_real_climb_approximated():
@@ -350,33 +351,36 @@ def test_best_efforts_no_targets_for_sport():
# ── best climb ────────────────────────────────────────────────────────────────
def test_best_climb_simple_ascent():
# 0 → 100 m with no gaps
ele = [float(i) for i in range(101)]
# 0 → 100 m with no gaps; x is cumulative distance (m)
ele = [(float(i), float(i)) for i in range(101)]
result = _best_climb(ele)
assert result == 100.0
def test_best_climb_with_descent():
# Up 50, down 20, up 80 → best contiguous window = 80
ele = list(range(0, 51)) + list(range(50, 30, -1)) + list(range(30, 111))
vals = list(range(0, 51)) + list(range(50, 30, -1)) + list(range(30, 111))
ele = [(float(i), float(v)) for i, v in enumerate(vals)]
result = _best_climb(ele)
assert result is not None
assert result >= 80.0
def test_best_climb_none_gap_resets_window():
# 50 m up, then a GPS gap, then 30 m up — windows don't bridge the gap
ele: list = list(range(0, 51)) + [None] + list(range(0, 31))
result = _best_climb(ele)
# 50 m up, then a GPS gap (skipped), then 30 m up — windows don't bridge the gap.
# None elevations are excluded when building dist_ele, so the climb restarts at 0.
ele_up1 = [(float(i), float(i)) for i in range(51)]
ele_up2 = [(float(51 + i), float(i)) for i in range(31)]
result = _best_climb(ele_up1 + ele_up2)
assert result == 50.0
def test_best_climb_only_descent():
ele = [100.0, 80.0, 60.0, 40.0]
ele = [(float(i), v) for i, v in enumerate([100.0, 80.0, 60.0, 40.0])]
result = _best_climb(ele)
assert result is None
def test_best_climb_too_few_samples():
assert _best_climb([]) is None
assert _best_climb([100.0]) is None
assert _best_climb([(0.0, 100.0)]) is None