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
|
package official_climb_test
import (
"testing"
"time"
"github.com/jftuga/geodist"
"github.com/martinlehoux/biking_home/internal/dbtest"
"github.com/martinlehoux/biking_home/official_climb"
"github.com/martinlehoux/biking_home/ride"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateListAndGetOfficialClimb(t *testing.T) {
db := dbtest.New(t)
created, err := official_climb.Create(db, official_climb.OfficialClimb{
Name: "Col de Test",
StartCoord: geodist.Coord{Lat: 43.1, Lon: 5.1},
EndCoord: geodist.Coord{Lat: 43.2, Lon: 5.2},
})
require.NoError(t, err)
assert.Positive(t, created.ID)
assert.Equal(t, "Col de Test", created.Name)
assert.Equal(t, geodist.Coord{Lat: 43.1, Lon: 5.1}, created.StartCoord)
listed, err := official_climb.List(db)
require.NoError(t, err)
require.Len(t, listed, 1)
assert.Equal(t, created.ID, listed[0].ID)
got, found, err := official_climb.GetByID(db, created.ID)
require.NoError(t, err)
require.True(t, found)
assert.Equal(t, created.EndCoord, got.EndCoord)
assert.False(t, got.CreatedAt.IsZero())
assert.False(t, got.UpdatedAt.IsZero())
}
func TestCreateRejectsInvalidOfficialClimb(t *testing.T) {
db := dbtest.New(t)
_, err := official_climb.Create(db, official_climb.OfficialClimb{
Name: " ",
StartCoord: geodist.Coord{Lat: 43.1, Lon: 5.1},
EndCoord: geodist.Coord{Lat: 43.2, Lon: 5.2},
})
assert.EqualError(t, err, "official climb name is required")
}
func TestMatchPolicyRejectsInvalidRadius(t *testing.T) {
assert.EqualError(t, (official_climb.MatchPolicy{}).Validate(), "official climb endpoint radius must be greater than zero")
}
func TestMatchClimbMatchesWithoutDetectedClimb(t *testing.T) {
parsed := ride.FromColumns(
[]float64{0, 1000, 2000, 3000},
[]float64{100, 150, 200, 100},
[]geodist.Coord{
{Lat: 43.10, Lon: 5.10},
{Lat: 43.11, Lon: 5.11},
{Lat: 43.12, Lon: 5.12},
{Lat: 43.13, Lon: 5.13},
},
[]time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0)},
)
official := official_climb.OfficialClimb{
Name: "On route",
StartCoord: parsed.Coord(1),
EndCoord: parsed.Coord(2),
}
matched, found := official_climb.MatchClimb(parsed.Coords, official, official_climb.DefaultMatchRadiusM)
require.True(t, found)
assert.Equal(t, 1, matched.StartIndex)
assert.Equal(t, 2, matched.EndIndex)
}
func TestMatchClimbMatchesEndpointInsideRadius(t *testing.T) {
parsed := ride.FromColumns(
[]float64{0, 1000, 2000, 3000},
[]float64{100, 150, 200, 100},
[]geodist.Coord{
{Lat: 43.10, Lon: 5.10},
{Lat: 43.11, Lon: 5.11},
{Lat: 43.12, Lon: 5.12},
{Lat: 43.13, Lon: 5.13},
},
[]time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0)},
)
official := official_climb.OfficialClimb{
Name: "Offset from route",
StartCoord: geodist.Coord{Lat: 43.1101, Lon: 5.1101},
EndCoord: geodist.Coord{Lat: 43.1201, Lon: 5.1201},
}
matched, found := official_climb.MatchClimb(parsed.Coords, official, official_climb.DefaultMatchRadiusM)
require.True(t, found)
assert.Equal(t, 1, matched.StartIndex)
assert.Equal(t, 2, matched.EndIndex)
}
func TestMatchClimbRejectsReversedEndpoints(t *testing.T) {
parsed := ride.FromColumns(
[]float64{0, 1000, 2000, 3000},
[]float64{100, 150, 200, 100},
[]geodist.Coord{
{Lat: 43.10, Lon: 5.10},
{Lat: 43.11, Lon: 5.11},
{Lat: 43.12, Lon: 5.12},
{Lat: 43.13, Lon: 5.13},
},
[]time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0)},
)
official := official_climb.OfficialClimb{
Name: "Reversed",
StartCoord: parsed.Coord(2),
EndCoord: parsed.Coord(1),
}
_, found := official_climb.MatchClimb(parsed.Coords, official, official_climb.DefaultMatchRadiusM)
assert.False(t, found)
}
func TestMatchClimbRejectsEndpointOutsideRadius(t *testing.T) {
parsed := ride.FromColumns(
[]float64{0, 1000, 2000, 3000},
[]float64{100, 150, 200, 100},
[]geodist.Coord{
{Lat: 43.10, Lon: 5.10},
{Lat: 43.11, Lon: 5.11},
{Lat: 43.12, Lon: 5.12},
{Lat: 43.13, Lon: 5.13},
},
[]time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0)},
)
official := official_climb.OfficialClimb{
Name: "Too far",
StartCoord: geodist.Coord{Lat: 43.09, Lon: 5.09},
EndCoord: parsed.Coord(2),
}
_, found := official_climb.MatchClimb(parsed.Coords, official, 10)
assert.False(t, found)
}
|