summaryrefslogtreecommitdiff
path: root/web/server_test.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-08-08 22:29:49 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-08-08 22:29:49 +0200
commit31ce2a1d5ff5746249cd4de96762f5740aff907a (patch)
tree2a52c7c4b69f7b82836504bcac5344171581102e /web/server_test.go
parentd463633ecdc66cc4bedd6e5d1267f0eb69493f38 (diff)
perf: Load ride values from SQLite
Diffstat (limited to 'web/server_test.go')
-rw-r--r--web/server_test.go66
1 files changed, 52 insertions, 14 deletions
diff --git a/web/server_test.go b/web/server_test.go
index 98ea681..d2e14f0 100644
--- a/web/server_test.go
+++ b/web/server_test.go
@@ -180,22 +180,25 @@ func TestRideSortHeadersToggleDirection(t *testing.T) {
assert.Equal(t, "ascending", headers[2].AriaSort)
}
-func TestSortRideViewsByCotacolKeepsMissingLast(t *testing.T) {
- items := []RideView{
- {cotacolScore: 4, cotacolReady: true},
- {cotacolScore: 1, cotacolReady: true},
- {cotacolReady: false},
+func TestBuildRideViewsUsesStoredCotacol(t *testing.T) {
+ _, db := newWebTestServer(t)
+ ride := rides.Ride{
+ ExternalID: "strava:550e8400-e29b-41d4-a716-446655440000",
+ GPXPath: testGPXPath(t, "stored.gpx"),
+ Name: "Stored Ride",
+ Type: "Ride",
+ StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
+ DistanceM: 20_000,
}
+ require.NoError(t, rides.Save(db, ride))
+ _, err := db.Exec("UPDATE rides SET gpx_path = ? WHERE external_id = ?", "missing.gpx", ride.ExternalID)
+ require.NoError(t, err)
+ items, err := rides.List(db)
+ require.NoError(t, err)
- sortRideViews(items, RideSort{Column: rideSortCotacol})
- assert.Equal(t, 1.0, items[0].cotacolScore)
- assert.Equal(t, 4.0, items[1].cotacolScore)
- assert.False(t, items[2].cotacolReady)
-
- sortRideViews(items, RideSort{Column: rideSortCotacol, Descending: true})
- assert.Equal(t, 4.0, items[0].cotacolScore)
- assert.Equal(t, 1.0, items[1].cotacolScore)
- assert.False(t, items[2].cotacolReady)
+ views := buildRideViews(items)
+ require.Len(t, views, 1)
+ assert.NotEqual(t, "-", views[0].Cotacol)
}
func TestHandlerSortsRidesByDistance(t *testing.T) {
@@ -226,3 +229,38 @@ func TestHandlerSortsRidesByDistance(t *testing.T) {
assert.Less(t, strings.Index(body, "Near Ride"), strings.Index(body, "Far Ride"))
assert.Contains(t, body, "dir=desc&amp;sort=distance")
}
+
+func TestHandlerSortsRidesByCotacol(t *testing.T) {
+ server, db := newWebTestServer(t)
+ first := rides.Ride{
+ ExternalID: "strava:550e8400-e29b-41d4-a716-446655440000",
+ GPXPath: testGPXPath(t, "first.gpx"),
+ Name: "Low Cotacol",
+ Type: "Ride",
+ StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
+ DistanceM: 20_000,
+ }
+ second := rides.Ride{
+ ExternalID: "strava:6ba7b810-9dad-41d1-80b4-00c04fd430c8",
+ GPXPath: testGPXPath(t, "second.gpx"),
+ Name: "High Cotacol",
+ Type: "Ride",
+ StartDate: time.Date(2026, 8, 2, 7, 0, 0, 0, time.UTC),
+ DistanceM: 40_000,
+ }
+ require.NoError(t, rides.Save(db, first))
+ require.NoError(t, rides.Save(db, second))
+ _, err := db.Exec(`
+ UPDATE rides
+ SET cotacol_score = CASE external_id WHEN ? THEN 1 ELSE 4 END
+ `, first.ExternalID)
+ require.NoError(t, err)
+
+ req := httptest.NewRequest(http.MethodGet, "/?sort=cotacol&dir=asc", nil)
+ response := httptest.NewRecorder()
+ server.Handler().ServeHTTP(response, req)
+
+ body := response.Body.String()
+ assert.Equal(t, http.StatusOK, response.Code)
+ assert.Less(t, strings.Index(body, "Low Cotacol"), strings.Index(body, "High Cotacol"))
+}