import pytest from conftest import REPO from rowing_stats.errors import RowingStatsError from rowing_stats.results import ( CSV_COLUMNS, WorkoutRow, calories, format_pace, format_time, load_ground_truth, make_row, pace_per_500m, parse_time, write_csv, ) @pytest.mark.parametrize("seconds, expected", [ (124.0, "2:04.0"), (123.14, "2:03.1"), (119.5, "1:59.5"), (119.97, "2:00.0"), # rounds up into the next minute, not "1:60.0" (60.0, "1:00.0"), ]) def test_format_pace(seconds, expected): assert format_pace(seconds) == expected def test_time_round_trip(): assert format_time(30, 6) == "30:06" assert format_time(60, 33) == "60:33" # minutes run past the hour assert parse_time("30:06") == 1806 assert parse_time("60:33") == 3633 def test_derived_metrics_match_the_committed_results(): # First row of rowing_results.csv: 30:06, 7283 m -> 2:04.0, 486 kcal assert format_pace(pace_per_500m(1806, 7283)) == "2:04.0" assert round(calories(7283)) == 486 def test_make_row_derives_pace_and_calories(): assert make_row("IMG_5413.JPEG", "30:06", 7283, "2025-11-05 12:08:00") == WorkoutRow( image="IMG_5413.JPEG", time="30:06", distance_m=7283, pace_per_500m="2:04.0", calories=486, date_taken="2025-11-05 12:08:00", ) def test_make_row_leaves_gaps_for_missing_fields(): assert make_row("a", "30:06", None) == WorkoutRow(image="a", time="30:06") assert make_row("a", None, 7283) == WorkoutRow(image="a", distance_m=7283, calories=486) assert make_row("a", None, None) == WorkoutRow(image="a") def test_csv_round_trip(tmp_path): path = tmp_path / "out.csv" write_csv([make_row("IMG_1.JPEG", "30:06", 7283), make_row("IMG_2.JPEG", "40:05", 9671)], path) header, first, _ = path.read_text().splitlines() assert header.split(",") == CSV_COLUMNS assert first == "IMG_1.JPEG,30:06,7283,2:04.0,486," assert load_ground_truth(path) == { "IMG_1": {"time": "30:06", "distance_m": 7283}, "IMG_2": {"time": "40:05", "distance_m": 9671}, } def test_load_ground_truth_reads_the_committed_reference(): truth = load_ground_truth(REPO / "rowing_results.csv") assert len(truth) == 34 assert truth["IMG_5413"] == {"time": "30:06", "distance_m": 7283} def test_load_ground_truth_missing_file(tmp_path): with pytest.raises(RowingStatsError, match="could not read"): load_ground_truth(tmp_path / "nope.csv") @pytest.mark.parametrize("content", [ "name,value\nx,1\n", # wrong columns "image,time,distance_m\nIMG_1.JPEG,30:06,far\n", # distance not a number "image,time,distance_m\nIMG_1.JPEG,30:06\n", # short row ]) def test_load_ground_truth_rejects_other_csvs(tmp_path, content): path = tmp_path / "bad.csv" path.write_text(content) with pytest.raises(RowingStatsError, match="not a results CSV"): load_ground_truth(path)