/
test_rides.rb
203 lines (157 loc) · 6.92 KB
/
test_rides.rb
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
require 'helper'
require 'mocha'
require 'json'
class TestRides < Test::Unit::TestCase
def setup
@s = StravaApi::Base.new
end
def test_rides_index_no_arguments
expect_error(StravaApi::CommandError) { @s.rides }
end
def test_rides_index_invalid_argument
expect_error(StravaApi::CommandError) { @s.rides(:invalid => true) }
end
def test_rides_index_by_club
api_result = JSON.parse rides_index_by_club_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'clubId' => 23} }).returns(api_result)
result = @s.rides(:club_id => 23)
assert result.is_a?(Array)
result.each do |ride|
assert ride.is_a?(StravaApi::Ride)
end
end
def test_rides_index_by_athlete
#curl http://www.strava.com/api/v1/rides?athleteId=779
api_result = JSON.parse rides_index_by_athlete_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'athleteId' => 779} }).returns(api_result)
result = @s.rides(:athlete_id => 779)
assert result.is_a?(Array)
result.each do |ride|
assert ride.is_a?(StravaApi::Ride)
end
end
def test_rides_index_by_club_and_athlete
#curl "http://www.strava.com/api/v1/rides?clubId=23&athleteId=779"
api_result = JSON.parse rides_index_by_club_and_athlete_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'clubId' => 23, 'athleteId' => 779} }).returns(api_result)
result = @s.rides(:club_id => 23, :athlete_id => 779)
assert result.is_a?(Array)
result.each do |ride|
assert ride.is_a?(StravaApi::Ride)
end
end
def test_rides_index_with_mismatched_club_and_athlete
#curl "http://www.strava.com/api/v1/rides?clubId=24&athleteId=779"
api_result = JSON.parse '{"error":"Invalid clubId"}'
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'clubId' => 24, 'athleteId' => 779} }).returns(api_result)
expect_error(StravaApi::InvalidResponseError) { @s.rides(:club_id => 24, :athlete_id => 779) }
assert @s.errors.include?("Invalid clubId")
end
def test_rides_index_ignores_invalid_parameter
#curl http://www.strava.com/api/v1/rides?athleteId=779
api_result = JSON.parse rides_index_ignores_invalid_parameter_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'athleteId' => 779} }).returns(api_result)
result = @s.rides(:athlete_id => 779, :xclub_id => 24)
assert result.is_a?(Array)
result.each do |ride|
assert ride.is_a?(StravaApi::Ride)
end
end
def test_rides_after_start_date
#curl "http://www.strava.com/api/v1/rides?athleteId=779&startDate=2010-09-21"
api_result = JSON.parse rides_after_start_date_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'athleteId' => 779, 'startDate' => Date.civil(2010,9,21)} }).returns(api_result)
result = @s.rides(:athlete_id => 779, :start_date => Date.civil(2010,9,21))
assert result.is_a?(Array)
result.each do |ride|
assert ride.is_a?(StravaApi::Ride)
#TODO check that each ride is after 9/21/2010
end
end
def test_rides_using_offset
#curl "http://www.strava.com/api/v1/rides?athleteId=779&startDate=2010-07-1&endDate=2010_09_01"
api_result = JSON.parse rides_using_offset_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'startDate' => Date.civil(2010,7,1),
'endDate' => Date.civil(2010,7,5)} }).returns(api_result)
api_result2 = JSON.parse rides_using_offset_50_json
api_result2.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides', { :query => {'startDate' => Date.civil(2010,7,1),
'endDate' => Date.civil(2010,7,5), 'offset' => 50} }).returns(api_result2)
set_1 = @s.rides(:start_date => Date.civil(2010,7,1), :end_date => Date.civil(2010,7,5))
set_2 = @s.rides(:start_date => Date.civil(2010,7,1), :end_date => Date.civil(2010,7,5), :offset => set_1.size)
assert set_1.is_a?(Array)
assert set_2.is_a?(Array)
set_1.each {|ride| assert ride.is_a?(StravaApi::Ride)}
set_2.each {|ride| assert ride.is_a?(StravaApi::Ride)}
#but there shouldn't be any overlap
set_1_ids = set_1.collect(&:id)
set_2_ids = set_2.collect(&:id)
set_1_ids.each do |set_1_id|
assert !set_2_ids.include?(set_1_id), "Error: #{set_1_id} is included in both sets"
end
end
def test_ride_show
#curl "http://www.strava.com/api/v1/rides/77563"
api_result = JSON.parse ride_show_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides/77563', { :query => {} }).returns(api_result)
result = @s.ride_show(77563)
assert result.is_a?(StravaApi::Ride)
{
:time_zone_offset => -8.0,
:elevation_gain => 1441.02,
:location => "San Francisco, CA",
:elapsed_time => 14579,
:description => nil,
:name => "02/28/10 San Francisco, CA",
:moving_time => 12748,
:average_speed => 23260.8064010041,
:distance => 82369.1,
:start_date => "2010-02-28T16:31:35Z",
:average_watts => 175.112,
:start_date_local => "2010-02-28T08:31:35Z",
:id => 77563,
:maximum_speed => 64251.72
}.each do |property, value|
assert result[property] == value, "mismatch on #{property}: #{result[property]} != #{value}"
end
assert result[:athlete].is_a?(StravaApi::Member)
assert result[:athlete].username == "julianbill"
assert result[:bike].is_a?(StravaApi::Bike)
assert result[:bike].name == "Serotta Legend Ti"
end
def test_ride_efforts
#curl "http://www.strava.com/api/v1/rides/77563/efforts"
api_result = JSON.parse ride_efforts_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/rides/77563/efforts', { :query => {} }).returns(api_result)
result = @s.ride_efforts(77563)
assert result.is_a?(Array)
result.each do |effort|
assert effort.is_a?(StravaApi::Effort)
end
assert result.first.segment.is_a?(StravaApi::Segment)
assert result.first.segment.name == "Panhandle to GGP"
end
def test_ride_streams
#curl "http://www.strava.com/api/v1/streams/77563"
api_result = JSON.parse ride_streams_json
api_result.stubs(:parsed_response).returns("")
StravaApi::Base.stubs(:get).with('/streams/77563', { :query => {} }).returns(api_result)
result = @s.ride_streams(77563)
assert result.is_a?(StravaApi::Streams)
assert !result.altitude.empty?
assert !result.distance.empty?
assert !result.heartrate.empty?
assert !result.latlng.empty?
assert !result.time.empty?
assert !result.watts_calc.empty?
end
end