Files
openrun/tests/unit/test_garmin_export.py
2026-05-19 08:34:22 -04:00

62 lines
2.5 KiB
Python

"""Tests for `openrun.ingest.garmin_export`."""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
from openrun.ingest.garmin_export import _activity_id_from_filename
def test_activity_id_from_filename_connect_style() -> None:
# Connect export: <id>_<activity_name>.fit
assert _activity_id_from_filename("12345678_morning_run") == 12345678
def test_activity_id_from_filename_takeout_style() -> None:
# Takeout: <email>_<upload_id>.fit (email is the literal prefix Garmin uses)
assert _activity_id_from_filename("o@o00.io_132001424015") == 132001424015
def test_activity_id_from_filename_takeout_with_year_prefix() -> None:
# Some Takeout names embed a date prefix; we want the upload-id (longer chunk), not the year.
assert _activity_id_from_filename("2024_132001424015") == 132001424015
def test_activity_id_from_filename_none_when_no_digit_chunk() -> None:
assert _activity_id_from_filename("trainingPlan") is None
assert _activity_id_from_filename("just_words_here") is None
def test_activity_id_from_filename_short_digit_chunk_rejected() -> None:
# <8 digits is treated as not-an-activity-id (filters out year-like 2024, etc).
assert _activity_id_from_filename("2024_run") is None
def test_main_hint_printed_for_takeout_named_fits(tmp_path: Path) -> None:
"""End-to-end through main(): a FIT whose stem has no activity-id chunk
triggers the 'run openrun-link-fit' hint in the summary."""
export = tmp_path / "export"
export.mkdir()
# A Takeout-style filename — has a long digit chunk, but the chunk is
# the upload id (not the activity id), and we have no activities seeded.
# Even though the regex picks it up, the activity-not-found branch returns 0.
# To exercise the *no-id-in-filename* branch we use a clearly nameless FIT:
(export / "trainingPlan.fit").write_bytes(b"")
# Run via a child process so we don't touch the real DB. We point
# openrun at an isolated workspace via OPENRUN_CONFIG.
cfg = tmp_path / "openrun.toml"
cfg.write_text(f'[db]\npath = "{tmp_path / "test.db"}"\n')
proc = subprocess.run(
[sys.executable, "-m", "openrun.ingest.garmin_export", str(export)],
capture_output=True,
text=True,
env={"OPENRUN_CONFIG": str(cfg), "PATH": __import__("os").environ.get("PATH", "")},
)
assert proc.returncode == 0, proc.stderr
assert "FITs skipped" in proc.stdout
assert "openrun-link-fit" in proc.stdout