summaryrefslogtreecommitdiff
path: root/rides
diff options
context:
space:
mode:
Diffstat (limited to 'rides')
-rw-r--r--rides/rides.go17
-rw-r--r--rides/rides_test.go17
2 files changed, 28 insertions, 6 deletions
diff --git a/rides/rides.go b/rides/rides.go
index 8112c63..f68952c 100644
--- a/rides/rides.go
+++ b/rides/rides.go
@@ -43,12 +43,17 @@ const (
const columns = "id, external_id, gpx_path, name, type, start_date, distance_m, moving_time_s, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version, created_at, updated_at"
func Save(db *sql.DB, r Ride) error {
- parsed, err := ride.ParseFile(ride.GPXRideParser{}, r.GPXPath)
- if err != nil {
- return fmt.Errorf("compute Cotacol for ride %q: %w", r.ExternalID, err)
+ var cotacolScore any
+ var cotacolAlgorithmVersion any
+ if r.GPXPath != "" {
+ parsed, err := ride.ParseFile(ride.GPXRideParser{}, r.GPXPath)
+ if err != nil {
+ return fmt.Errorf("compute Cotacol for ride %q: %w", r.ExternalID, err)
+ }
+ cotacolScore = ride.Cotacol(parsed)
+ cotacolAlgorithmVersion = ride.CotacolAlgorithmVersion
}
- cotacolScore := ride.Cotacol(parsed)
- _, err = db.Exec(`
+ _, err := db.Exec(`
INSERT INTO rides (external_id, gpx_path, name, type, start_date, distance_m, moving_time_s, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(external_id) DO UPDATE SET
@@ -64,7 +69,7 @@ func Save(db *sql.DB, r Ride) error {
cotacol_score = excluded.cotacol_score,
cotacol_algo_version = excluded.cotacol_algo_version,
updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
- `, r.ExternalID, r.GPXPath, r.Name, r.Type, r.StartDate.UTC().Format(time.RFC3339), r.DistanceM, r.MovingTimeS, r.ElapsedTimeS, r.TotalElevationGainM, r.AverageSpeedMps, cotacolScore, ride.CotacolAlgorithmVersion)
+ `, r.ExternalID, r.GPXPath, r.Name, r.Type, r.StartDate.UTC().Format(time.RFC3339), r.DistanceM, r.MovingTimeS, r.ElapsedTimeS, r.TotalElevationGainM, r.AverageSpeedMps, cotacolScore, cotacolAlgorithmVersion)
return err
}
diff --git a/rides/rides_test.go b/rides/rides_test.go
index 1c382b0..eb705fa 100644
--- a/rides/rides_test.go
+++ b/rides/rides_test.go
@@ -117,6 +117,23 @@ func TestSaveComputesCotacol(t *testing.T) {
assert.Equal(t, ride.CotacolAlgorithmVersion, got.CotacolAlgorithmVersion())
}
+func TestSaveAllowsRideWithoutGPX(t *testing.T) {
+ db := newTestDB(t)
+ indoor := sampleRide(t)
+ indoor.ExternalID = "strava:14701658670"
+ indoor.GPXPath = ""
+
+ require.NoError(t, Save(db, indoor))
+
+ got, found, err := GetByExternalID(db, indoor.ExternalID)
+ require.NoError(t, err)
+ require.True(t, found)
+ assert.Empty(t, got.GPXPath)
+ _, ready := got.CotacolScore()
+ assert.False(t, ready)
+ assert.Empty(t, got.CotacolAlgorithmVersion())
+}
+
func TestBackfill(t *testing.T) {
db := newTestDB(t)
require.NoError(t, Save(db, sampleRide(t)))