forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
athletes_test.go
387 lines (300 loc) · 11.1 KB
/
athletes_test.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package strava
import (
"reflect"
"testing"
"time"
)
func TestAthletesGet(t *testing.T) {
client := newCassetteClient(testToken, "athlete_get")
athlete, err := NewAthletesService(client).Get(3545423).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
expected := &AthleteSummary{}
expected.Id = 3545423
expected.FirstName = "Strava"
expected.LastName = "Testing"
expected.Friend = "accepted"
expected.Follower = "accepted"
expected.Profile = "avatar/athlete/large.png"
expected.ProfileMedium = "avatar/athlete/medium.png"
expected.City = "Palo Alto"
expected.State = "CA"
expected.Country = "United States"
expected.Gender = "M"
expected.CreatedAt, _ = time.Parse(timeFormat, "2013-12-26T19:19:36Z")
expected.UpdatedAt, _ = time.Parse(timeFormat, "2014-01-12T00:20:58Z")
if !reflect.DeepEqual(athlete, expected) {
t.Errorf("should match\n%v\n%v", athlete, expected)
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// path
s.Get(111).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/111" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestCAthletesListStarredSegments(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_starred_segments")
segments, err := NewAthletesService(client).ListStarredSegments(3545423).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
expected := &PersonalSegmentSummary{}
expected.Id = 229781
expected.Name = "Hawk Hill"
expected.ActivityType = ActivityTypes.Ride
expected.Distance = 2684.82
expected.AverageGrade = 5.7
expected.MaximumGrade = 14.2
expected.ElevationHigh = 245.3
expected.ElevationLow = 92.4
expected.StartLocation = Location{37.8331119, -122.4834356}
expected.EndLocation = Location{37.8280722, -122.4981393}
expected.ClimbCategory = ClimbCategories.Category4
expected.City = "San Francisco"
expected.State = "CA"
expected.Country = "United States"
expected.Private = false
expected.Starred = true
expected.AthletePR.Id = 3439333050
expected.AthletePR.ElapsedTime = 550
expected.AthletePR.Distance = 2713.4
expected.AthletePR.StartDate, _ = time.Parse(timeFormat, "2013-01-21T19:05:07Z")
expected.AthletePR.StartDateLocal, _ = time.Parse(timeFormat, "2013-01-21T11:05:07Z")
expected.StarredDate, _ = time.Parse(timeFormat, "2014-07-24T23:23:24Z")
if !reflect.DeepEqual(segments[0], expected) {
t.Errorf("should match\n%v\n%v", segments[0], expected)
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// path
s.ListStarredSegments(123).Page(2).PerPage(3).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/segments/starred" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "page=2&per_page=3" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesListFriends(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_friends")
friends, err := NewAthletesService(client).ListFriends(3545423).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
if len(friends) == 0 {
t.Fatal("friends not parsed")
}
if friends[0].CreatedAt.IsZero() || friends[0].UpdatedAt.IsZero() {
t.Error("dates not parsed")
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// parameters
s.ListFriends(123).Page(2).PerPage(3).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/friends" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "page=2&per_page=3" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesListFollowers(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_followers")
followers, err := NewAthletesService(client).ListFollowers(3545423).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
if len(followers) == 0 {
t.Fatal("followers not parsed")
}
if followers[0].CreatedAt.IsZero() || followers[0].UpdatedAt.IsZero() {
t.Error("dates not parsed")
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// parameters
s.ListFollowers(123).Page(2).PerPage(3).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/followers" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "page=2&per_page=3" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesListBothFollowing(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_both_following")
followers, err := NewAthletesService(client).ListBothFollowing(3545423).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
if len(followers) == 0 {
t.Fatal("followers not parsed")
}
if followers[0].CreatedAt.IsZero() || followers[0].UpdatedAt.IsZero() {
t.Error("dates not parsed")
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// parameters
s.ListBothFollowing(123).PerPage(7).Page(8).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/both-following" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "page=8&per_page=7" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesStats(t *testing.T) {
client := newCassetteClient(testToken, "athlete_stats")
stats, err := NewAthletesService(client).Stats(123).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
expected := &AthleteStats{}
expected.BiggestRideDistance = 205481
expected.BiggestClimbElevationGain = 1224
expected.RecentRideTotals.Count = 29
expected.RecentRideTotals.Distance = 975679.00390625
expected.RecentRideTotals.MovingTime = 155868
expected.RecentRideTotals.ElapsedTime = 171286
expected.RecentRideTotals.ElevationGain = 12460.902572631836
expected.RecentRideTotals.AchievementCount = 336
expected.RecentRunTotals.Count = 7
expected.RecentRunTotals.Distance = 104608.0009765625
expected.RecentRunTotals.MovingTime = 31383
expected.RecentRunTotals.ElapsedTime = 32453
expected.RecentRunTotals.ElevationGain = 2107.405592918396
expected.RecentRunTotals.AchievementCount = 42
expected.YTDRideTotals.Count = 45
expected.YTDRideTotals.Distance = 1.58619e+06
expected.YTDRideTotals.MovingTime = 257281
expected.YTDRideTotals.ElapsedTime = 285315
expected.YTDRideTotals.ElevationGain = 22430
expected.YTDRunTotals.Count = 7
expected.YTDRunTotals.Distance = 104608
expected.YTDRunTotals.MovingTime = 31383
expected.YTDRunTotals.ElapsedTime = 32453
expected.YTDRunTotals.ElevationGain = 2107
expected.AllRideTotals.Count = 765
expected.AllRideTotals.Distance = 4.2918079e+07
expected.AllRideTotals.MovingTime = 6386345
expected.AllRideTotals.ElapsedTime = 7228437
expected.AllRideTotals.ElevationGain = 550886
expected.AllRunTotals.Count = 76
expected.AllRunTotals.Distance = 937914
expected.AllRunTotals.MovingTime = 256194
expected.AllRunTotals.ElapsedTime = 268123
expected.AllRunTotals.ElevationGain = 8062
if !reflect.DeepEqual(stats, expected) {
t.Errorf("should match\n%v\n%v", stats, expected)
}
}
func TestAthletesListKOMs(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_koms")
efforts, err := NewAthletesService(client).ListKOMs(3776).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
if len(efforts) == 0 {
t.Fatal("efforts not parsed")
}
if efforts[0].StartDate.IsZero() || efforts[0].StartDateLocal.IsZero() {
t.Error("dates not parsed")
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// parameters
s.ListKOMs(123).PerPage(9).Page(8).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/koms" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "page=8&per_page=9" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesListActivities(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_activies")
activities, err := NewAthletesService(client).ListActivities(14507).Do()
if err != nil {
t.Fatalf("service error: %v", err)
}
if len(activities) == 0 {
t.Fatal("efforts not parsed")
}
if activities[0].StartDate.IsZero() || activities[0].StartDateLocal.IsZero() {
t.Error("dates not parsed")
}
// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())
// path
s.ListActivities(123).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/activities" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
// parameters
s.ListActivities(123).PerPage(9).Page(8).Do()
transport = s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.RawQuery != "page=8&per_page=9" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
// parameters2
s.ListActivities(123).Before(1391020072).Do()
transport = s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.RawQuery != "before=1391020072" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
// parameters3
s.ListActivities(123).After(0).Do()
transport = s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.RawQuery != "after=0" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestAthletesBadJSON(t *testing.T) {
var err error
s := NewAthletesService(NewStubResponseClient("bad json"))
_, err = s.Get(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListStarredSegments(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListFriends(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListFollowers(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListBothFollowing(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListKOMs(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.ListActivities(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
}