1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package rides
import (
"database/sql"
"os"
"path/filepath"
"testing"
"time"
"github.com/martinlehoux/biking_home/ride"
"github.com/martinlehoux/kagamigo/kcore"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestDB(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
t.Cleanup(func() { db.Close() })
_, err = db.Exec(`
create table rides (
id integer primary key,
external_id text unique not null,
gpx_path text not null,
name text not null,
type text not null,
start_date text not null,
distance_m real not null,
moving_time_s integer not null,
elapsed_time_s integer not null,
total_elevation_gain_m real not null,
average_speed_mps real not null,
cotacol_score real,
cotacol_algo_version text,
created_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
updated_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
)
`)
require.NoError(t, err)
return db
}
func sampleRide(t *testing.T) Ride {
t.Helper()
gpxPath := filepath.Join(t.TempDir(), "ride.gpx")
require.NoError(t, os.WriteFile(gpxPath, []byte(testGPX), 0o600))
return Ride{
ExternalID: "strava:1234",
GPXPath: gpxPath,
Name: "Morning Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 1, 7, 30, 0, 0, time.UTC),
DistanceM: 42_195,
MovingTimeS: 7_200,
ElapsedTimeS: 7_800,
TotalElevationGainM: 850,
AverageSpeedMps: 5.86,
}
}
const testGPX = `<?xml version="1.0"?><gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1"><trk><trkseg><trkpt lat="43.0" lon="5.0"><ele>100</ele></trkpt><trkpt lat="43.001" lon="5.001"><ele>200</ele></trkpt></trkseg></trk></gpx>`
func TestUpsertAndGet(t *testing.T) {
db := newTestDB(t)
sample := sampleRide(t)
err := Save(db, sample)
require.NoError(t, err)
got, ok, err := GetByExternalID(db, "strava:1234")
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, "Morning Ride", got.Name)
assert.Equal(t, sample.StartDate, got.StartDate)
assert.Equal(t, 42_195.0, got.DistanceM)
assert.Equal(t, sample.GPXPath, got.GPXPath)
score, found := got.CotacolScore()
require.True(t, found)
assert.Greater(t, score, 0.0)
assert.Equal(t, ride.CotacolAlgorithmVersion, got.CotacolAlgorithmVersion())
_, ok, err = GetByExternalID(db, "strava:9999")
require.NoError(t, err)
assert.False(t, ok)
}
func TestSaveComputesCotacol(t *testing.T) {
db := newTestDB(t)
require.NoError(t, Save(db, sampleRide(t)))
got, ok, err := GetByExternalID(db, "strava:1234")
require.NoError(t, err)
require.True(t, ok)
score, found := got.CotacolScore()
require.True(t, found)
assert.Greater(t, score, 0.0)
assert.Equal(t, ride.CotacolAlgorithmVersion, got.CotacolAlgorithmVersion())
}
func TestBackfill(t *testing.T) {
db := newTestDB(t)
require.NoError(t, Save(db, sampleRide(t)))
count, err := Backfill(db)
require.NoError(t, err)
assert.Equal(t, 1, count)
got, ok, err := GetByExternalID(db, "strava:1234")
require.NoError(t, err)
require.True(t, ok)
_, found := got.CotacolScore()
assert.True(t, found)
assert.Equal(t, ride.CotacolAlgorithmVersion, got.CotacolAlgorithmVersion())
count, err = Backfill(db)
require.NoError(t, err)
assert.Equal(t, 1, count)
}
func TestUpsertUpdatesExisting(t *testing.T) {
db := newTestDB(t)
ride := sampleRide(t)
require.NoError(t, Save(db, ride))
ride.Name = "Renamed Ride"
ride.DistanceM = 50_000
require.NoError(t, Save(db, ride))
got, ok, err := GetByExternalID(db, "strava:1234")
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, "Renamed Ride", got.Name)
assert.Equal(t, 50_000.0, got.DistanceM)
}
func TestList(t *testing.T) {
db := newTestDB(t)
first := sampleRide(t)
first.StartDate = time.Date(2026, 7, 1, 7, 0, 0, 0, time.UTC)
second := sampleRide(t)
second.ExternalID = "strava:5678"
second.Name = "Evening Ride"
require.NoError(t, Save(db, first))
require.NoError(t, Save(db, second))
rides, err := List(db)
require.NoError(t, err)
require.Len(t, rides, 2)
assert.Equal(t, "strava:5678", rides[0].ExternalID)
assert.Equal(t, "strava:1234", rides[1].ExternalID)
kcore.Assert(len(rides) == 2, "two rides")
}
|