Skip to content

Commit 74f5ac5

Browse files
committed
Add detection when no routes match for times
1 parent 253192c commit 74f5ac5

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/commands/times.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub async fn handle_times_request(
5151
};
5252

5353
let mut schedule_lines: Vec<(NaiveDateTime, String)> = Vec::new();
54+
let mut route_matched = false;
5455

5556
for route_schedule in &parsed_response.stop_schedule.route_schedules {
5657
let number_as_string;
@@ -72,6 +73,8 @@ pub async fn handle_times_request(
7273
continue;
7374
}
7475

76+
route_matched = true;
77+
7578
for scheduled_stop in &route_schedule.scheduled_stops {
7679
let time = NaiveDateTime::parse_from_str(
7780
&scheduled_stop.times.departure.estimated,
@@ -141,6 +144,13 @@ pub async fn handle_times_request(
141144
}
142145
}
143146

147+
if !route_matched {
148+
response_text.push_str(&format!(
149+
"No routes found matching {} at this stop",
150+
command.routes.join(" ")
151+
));
152+
}
153+
144154
Ok(response_text)
145155
}
146156

tests/times.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,46 @@ async fn stop_number_returns_single_route_ignoring_case(db: PgPool) {
241241
assert_that(body).contains(expected_body);
242242
}
243243

244+
#[sqlx::test(fixtures("numbers-approved"))]
245+
async fn stop_number_notes_no_matching_routes(db: PgPool) {
246+
let mock_winnipeg_transit_api = MockServer::start().await;
247+
let mock_stop_schedule_response = fs::read_to_string("tests/fixtures/times/stop_schedule.json")
248+
.expect("Failed to read stop schedule fixture");
249+
250+
Mock::given(method("GET"))
251+
.and(path_regex(r"^/v4/stops/.*/schedule.json$"))
252+
.respond_with(
253+
ResponseTemplate::new(200).set_body_string(mock_stop_schedule_response.clone()),
254+
)
255+
.expect(1)
256+
.mount(&mock_winnipeg_transit_api)
257+
.await;
258+
259+
let response = get(
260+
"/twilio?Body=10619 99&From=approved&To=textabus&MessageSid=SM1849",
261+
InjectableServices {
262+
db: db.clone(),
263+
twilio_address: None,
264+
winnipeg_transit_api_address: Some(mock_winnipeg_transit_api.uri()),
265+
},
266+
)
267+
.await
268+
.expect("Failed to execute request");
269+
270+
assert!(response.status().is_success());
271+
assert_eq!(response.headers()["content-type"], "text/xml");
272+
273+
let document = Document::from(response.text().await.unwrap().as_str());
274+
let body = &document.find(Name("body")).next().unwrap().text();
275+
276+
let expected_body = indoc! {"
277+
10619 WB Graham@Vaughan (The Bay)
278+
No routes found matching 99 at this stop
279+
"};
280+
281+
assert_that(body).contains(expected_body);
282+
}
283+
244284
#[sqlx::test(fixtures("numbers-approved"))]
245285
async fn incorrect_stop_number_returns_error(db: PgPool) {
246286
let mock_winnipeg_transit_api = MockServer::start().await;

0 commit comments

Comments
 (0)