diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-24 08:37:33 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-24 08:37:33 +0200 |
| commit | 90614736f2a0c556f31eca5361b3c032f5f309f6 (patch) | |
| tree | 81fda954dc2bff8ae8b0023b502343c455995272 /official_climb/attempt_test.go | |
| parent | e3d0beee5be3dd2854b0f4fc10905a4fcdc08f47 (diff) | |
Match official climbs against their recorded sample
An official climb matched on endpoint coordinates alone. On a loop ridden
the other way the route meets the climb's top before its bottom, so pairing
each start with the first end inside its lap window credited a 5.9 km
passage as the 850 m Chemin de la Tour Pey Blanc, and a 49 km one on
another ride.
Climbs now keep the sample of the segment they were created from: the
recorded length and elevation gain of the selected route points, captured
by the ride detail and preview handlers and stored NOT NULL. Matching takes
the whole route and drops any candidate outside ±50% of that sample on
either axis, and attempts are refreshed by upsert and prune so re-analyzing
a ride keeps the recorded-at stamp of the passages that survive.
The backfill leaves the web package: backfill.Run recomputes ride values
then official climb attempts as a single `go run . backfill` command
(climbs-backfill is gone), rideanalysis owns the attempt snapshot, and the
mountain-pass loading margin is shared. Applied to the stored rides it
stamps 78 samples, drops the four bogus attempts and leaves ride 800 with
its three Pont Rout passages.
Diffstat (limited to 'official_climb/attempt_test.go')
| -rw-r--r-- | official_climb/attempt_test.go | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/official_climb/attempt_test.go b/official_climb/attempt_test.go index 147c80b..8913807 100644 --- a/official_climb/attempt_test.go +++ b/official_climb/attempt_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/jftuga/geodist" + "github.com/martinlehoux/biking_home/climb" "github.com/martinlehoux/biking_home/internal/dbtest" "github.com/martinlehoux/biking_home/official_climb" "github.com/stretchr/testify/assert" @@ -16,6 +17,11 @@ func coord(lat, lon float64) geodist.Coord { return geodist.Coord{Lat: lat, Lon: lon} } +// sampleFixture is a plausible sample for tests that do not exercise matching. +func sampleFixture() climb.SegmentSample { + return climb.SegmentSample{DistanceM: 3000, ElevationGainM: 200} +} + func insertRide(t *testing.T, db *sql.DB, opts ...func(*rideInsert)) int64 { t.Helper() insert := rideInsert{startDate: "2026-09-01T08:00:00Z"} @@ -57,7 +63,7 @@ func attemptFixture(rideID, climbID int64, occurrence int) official_climb.Attemp func TestReplaceRideAttemptsRoundTrip(t *testing.T) { db := dbtest.New(t) - created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42)}) + created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42), Sample: sampleFixture()}) require.NoError(t, err) rideID := insertRide(t, db) @@ -80,11 +86,31 @@ func TestReplaceRideAttemptsRoundTrip(t *testing.T) { require.Len(t, rideAttempts, 2) } +func TestReplaceRideAttemptsKeepsRecordedAt(t *testing.T) { + db := dbtest.New(t) + created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42), Sample: sampleFixture()}) + require.NoError(t, err) + rideID := insertRide(t, db) + attempts := []official_climb.Attempt{attemptFixture(rideID, created.ID, 0)} + require.NoError(t, official_climb.ReplaceRideAttempts(db, rideID, attempts)) + + recorded := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) + _, err = db.Exec("UPDATE ride_official_climb_attempts SET created_at = ? WHERE ride_id = ?", recorded.Format(time.RFC3339), rideID) + require.NoError(t, err) + + // Re-analyzing the same ride must not restamp the attempt it already has. + require.NoError(t, official_climb.ReplaceRideAttempts(db, rideID, attempts)) + stored, err := official_climb.AttemptsForRide(db, rideID) + require.NoError(t, err) + require.Len(t, stored, 1) + assert.Equal(t, recorded, stored[0].CreatedAt.UTC()) +} + func TestReplaceRideAttemptsDropsStaleRows(t *testing.T) { db := dbtest.New(t) - first, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col A", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42)}) + first, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col A", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42), Sample: sampleFixture()}) require.NoError(t, err) - second, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col B", StartCoord: coord(43.7, 5.5), EndCoord: coord(43.72, 5.52)}) + second, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col B", StartCoord: coord(43.7, 5.5), EndCoord: coord(43.72, 5.52), Sample: sampleFixture()}) require.NoError(t, err) rideID := insertRide(t, db) @@ -101,7 +127,7 @@ func TestReplaceRideAttemptsDropsStaleRows(t *testing.T) { func TestReplaceRideAttemptsRejectsForeignRide(t *testing.T) { db := dbtest.New(t) - created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42)}) + created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42), Sample: sampleFixture()}) require.NoError(t, err) rideID := insertRide(t, db) @@ -112,7 +138,7 @@ func TestReplaceRideAttemptsRejectsForeignRide(t *testing.T) { func TestAttemptsForClimbOrdersByRideDate(t *testing.T) { db := dbtest.New(t) - created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42)}) + created, err := official_climb.Create(db, official_climb.OfficialClimb{Name: "Col de Test", StartCoord: coord(43.6, 5.4), EndCoord: coord(43.62, 5.42), Sample: sampleFixture()}) require.NoError(t, err) newer := insertRide(t, db, withStartDate("2026-09-10T08:00:00Z")) older := insertRide(t, db, withStartDate("2026-09-02T08:00:00Z")) |
