from collections import Counter import pytest from rowing_stats.parsing import ( extract_distances, extract_distances_by_method, extract_times, extract_times_by_method, find_distances, find_times, image_base_name, pick_best, ) @pytest.mark.parametrize("text, expected", [ ("30:06", ["30:06"]), ("30:06 29\n7283", ["30:06"]), ("30:06\n40:05", ["30:06", "40:05"]), ("30:06 30:06", ["30:06", "30:06"]), # each occurrence is a vote ("", []), ("no digits here", []), ("2:04", []), # a pace: one-digit minutes ("02:04", []), # under the 10-minute floor ("09:59", []), ("10:00", ["10:00"]), ("30:60", []), # seconds out of range ("99:00", ["99:00"]), ("130:06", ["30:06"]), # a stray leading digit is dropped ]) def test_find_times_mm_ss(text, expected): assert find_times(text) == expected @pytest.mark.parametrize("text, expected", [ ("1:00:08", ["60:08"]), # normalised to total minutes ("1:30:00", ["90:00"]), ("2:00:00", ["120:00"]), ("2:00:01", ["120:01"]), # bound is on minutes only ("2:01:00", []), # over two hours ("0:05:00", []), # under ten minutes ("0:30:06", ["30:06"]), ("1:00:08 30:06", ["60:08", "30:06"]), ]) def test_find_times_h_mm_ss(text, expected): assert find_times(text) == expected def test_find_times_does_not_reread_the_tail_of_an_hour_time(): # "1:00:08" must not also produce "00:08", nor "1:30:45" a second "30:45" assert find_times("1:30:45") == ["90:45"] # ...even when the hour-format time itself was rejected assert find_times("2:30:45") == [] @pytest.mark.parametrize("text, expected", [ ("7283", ["7283"]), ("13879 26911", ["13879", "26911"]), ("999", []), # too short ("1000 50000", ["1000", "50000"]), # bounds are inclusive ("50001", []), ("0999", []), # four digits, but under 1000 m ("07337", ["07337"]), # a leading zero is kept as read ("123456", []), # six digits is not a distance ("30:06", []), ("72.83", []), ("", []), ]) def test_find_distances(text, expected): assert find_distances(text) == expected LABELED = [ ("s2_otsu", "30:06 29\n7283"), ("s2_otsu_inv", "30:06\n7288"), ("s3_otsu", "38:06\n7283"), ("s3_otsu_inv", ""), ] def test_votes_are_counted_across_variants(): assert extract_times(LABELED) == Counter({"30:06": 2, "38:06": 1}) assert extract_distances(LABELED) == Counter({"7283": 2, "7288": 1}) def test_majority_wins(): assert pick_best(extract_times(LABELED)) == "30:06" assert pick_best(extract_distances(LABELED)) == "7283" def test_pick_best_empty_is_none(): assert pick_best(Counter()) is None def test_pick_best_tie_goes_to_first_seen(): assert pick_best(extract_times([("a", "30:06"), ("b", "31:06")])) == "30:06" assert pick_best(extract_times([("a", "31:06"), ("b", "30:06")])) == "31:06" def test_by_method_groups_and_shows_abstentions(): assert extract_times_by_method(LABELED) == { "s2_otsu": {"30:06"}, "s2_otsu_inv": {"30:06"}, "s3_otsu": {"38:06"}, "s3_otsu_inv": set(), } assert extract_distances_by_method(LABELED)["s3_otsu_inv"] == set() assert extract_distances_by_method(LABELED)["s2_otsu_inv"] == {"7288"} @pytest.mark.parametrize("filename, expected", [ ("IMG_5413_screen.jpg", "IMG_5413"), ("IMG_5413_screen (1).jpg", "IMG_5413"), ("IMG_5413_screen (12).jpg", "IMG_5413"), ("IMG_5413.JPEG", "IMG_5413"), ]) def test_image_base_name(filename, expected): assert image_base_name(filename) == expected