Skip to content

Commit 29157a3

Browse files
committed
updated tests
1 parent 4bc78ba commit 29157a3

3 files changed

Lines changed: 100 additions & 33 deletions

File tree

R/parameterized.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ tm_trip_metrics <- function(start_date, end_date, agg, line, base_url = tm_base_
5454

5555
#' Get scheduled service data
5656
#'
57-
#' Returns scheduled service counts aggregated over a date range. Optionally
58-
#' filtered to a single route.
57+
#' Returns scheduled service counts aggregated over a date range for a single
58+
#' MBTA route.
5959
#'
6060
#' @inheritParams tm_line_delays
6161
#' @param agg Aggregation level, e.g. `"daily"` or `"weekly"`.
62-
#' @param route_id Optional MBTA route ID to filter results.
62+
#' @param route_id MBTA route ID, e.g. `"Red"`, `"Orange"`, `"Green-B"`.
6363
#' @return A list of scheduled service records.
6464
#' @export
6565
#' @examples
6666
#' \dontrun{
67-
#' tm_scheduled_service("2024-01-01", "2024-01-31", agg = "daily")
6867
#' tm_scheduled_service("2024-01-01", "2024-01-31", agg = "daily",
6968
#' route_id = "Red")
7069
#' }
@@ -137,29 +136,33 @@ tm_speed_restrictions <- function(line_id, on_date, base_url = tm_base_url()) {
137136

138137
#' Get service hours data
139138
#'
140-
#' Returns total revenue service hours aggregated over a date range. Optionally
141-
#' filtered to a single route.
139+
#' Returns scheduled vs. delivered service hours aggregated over a date range
140+
#' for a single MBTA line.
142141
#'
143-
#' @inheritParams tm_line_delays
142+
#' @param start_date Start of the date range. A `Date` object or `"YYYY-MM-DD"`
143+
#' string.
144+
#' @param end_date End of the date range. A `Date` object or `"YYYY-MM-DD"`
145+
#' string.
144146
#' @param agg Aggregation level, e.g. `"daily"` or `"weekly"`.
145-
#' @param single_route_id Optional MBTA route ID to filter results.
147+
#' @param line_id MBTA line identifier, e.g. `"line-red"`.
148+
#' @param base_url Base URL of the TransitMatters API. Defaults to
149+
#' `getOption("tm_dashboard_base_url")` or the production host.
146150
#' @return A list of service hours records.
147151
#' @export
148152
#' @examples
149153
#' \dontrun{
150-
#' tm_service_hours("2024-01-01", "2024-01-31", agg = "daily")
151154
#' tm_service_hours("2024-01-01", "2024-01-31", agg = "daily",
152-
#' single_route_id = "Red")
155+
#' line_id = "line-red")
153156
#' }
154-
tm_service_hours <- function(start_date, end_date, agg, single_route_id = NULL,
157+
tm_service_hours <- function(start_date, end_date, agg, line_id = NULL,
155158
base_url = tm_base_url()) {
156159
tm_request(
157160
"api/service_hours",
158161
query = list(
159-
start_date = .tm_date(start_date),
160-
end_date = .tm_date(end_date),
161-
agg = agg,
162-
single_route_id = single_route_id
162+
start_date = .tm_date(start_date),
163+
end_date = .tm_date(end_date),
164+
agg = agg,
165+
line_id = if (!is.null(line_id)) tolower(.tm_gtfs_line_id(line_id))
163166
),
164167
base_url = base_url
165168
)

tests/testthat/test-dated.R

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
test_that("tm_headways returns headways field", {
1+
test_that("tm_headways returns a list", {
22
httr2::local_mocked_responses(
3-
function(req) mock_response('{"headways":{"Red":[]}}')
3+
function(req) mock_response("[]")
44
)
5-
result <- tm_headways("2024-01-15")
6-
expect_true("headways" %in% names(result))
5+
result <- tm_headways("2024-01-15", stop = "70061")
6+
expect_type(result, "list")
77
})
88

99
test_that("tm_headways accepts a Date object", {
1010
httr2::local_mocked_responses(
11-
function(req) mock_response('{"headways":{}}')
11+
function(req) mock_response("[]")
1212
)
13-
expect_no_error(tm_headways(as.Date("2024-01-15")))
13+
expect_no_error(tm_headways(as.Date("2024-01-15"), stop = "70061"))
1414
})
1515

16-
test_that("tm_dwells returns dwells field", {
16+
test_that("tm_dwells returns a list", {
1717
httr2::local_mocked_responses(
18-
function(req) mock_response('{"dwells":{"Red":[]}}')
18+
function(req) mock_response("[]")
1919
)
20-
result <- tm_dwells("2024-01-15")
21-
expect_true("dwells" %in% names(result))
20+
result <- tm_dwells("2024-01-15", stop = "70061")
21+
expect_type(result, "list")
2222
})
2323

24-
test_that("tm_travel_times returns travel_times field", {
24+
test_that("tm_travel_times returns a list", {
2525
httr2::local_mocked_responses(
26-
function(req) mock_response('{"travel_times":{"Red":[]}}')
26+
function(req) mock_response("[]")
2727
)
28-
result <- tm_travel_times("2024-01-15")
29-
expect_true("travel_times" %in% names(result))
28+
result <- tm_travel_times("2024-01-15",
29+
from_stop = "70076", to_stop = "70064")
30+
expect_type(result, "list")
3031
})
3132

3233
test_that("tm_alerts with no date hits undated endpoint", {
@@ -36,7 +37,7 @@ test_that("tm_alerts with no date hits undated endpoint", {
3637
mock_response('{"alerts":[]}')
3738
}
3839
)
39-
result <- tm_alerts()
40+
result <- tm_alerts(route = "Red")
4041
expect_true("alerts" %in% names(result))
4142
})
4243

@@ -47,6 +48,6 @@ test_that("tm_alerts with a date hits dated endpoint", {
4748
mock_response('{"alerts":[]}')
4849
}
4950
)
50-
result <- tm_alerts("2024-01-15")
51+
result <- tm_alerts("2024-01-15", route = "Red")
5152
expect_true("alerts" %in% names(result))
5253
})

vignettes/getting-started.Rmd

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ trips <- tm_trip_metrics(
132132
agg = "daily",
133133
line = "line-red"
134134
)
135+
136+
# How many trips were scheduled to run each day
137+
scheduled <- tm_scheduled_service(
138+
start_date = "2024-01-01",
139+
end_date = "2024-01-31",
140+
route_id = "Red",
141+
agg = "daily"
142+
)
143+
144+
# Total revenue service hours each day on the Red Line
145+
hours <- tm_service_hours(
146+
start_date = "2024-01-01",
147+
end_date = "2024-01-31",
148+
agg = "daily",
149+
line_id = "Red"
150+
)
135151
```
136152

137153
**Available line IDs:**
@@ -144,7 +160,7 @@ trips <- tm_trip_metrics(
144160
| Green Line | `"line-green"` |
145161
| Commuter | `"line-cr"` |
146162

147-
## Step 6: Turn the result into a data frame
163+
## Step 8: Turn the result into a data frame
148164

149165
The API gives back a list, but most R analysis tools (like `ggplot2`) prefer a
150166
**data frame**. Here's the easiest way to convert:
@@ -167,7 +183,7 @@ head(ridership_df)
167183
> `do.call(rbind, lapply(ridership_raw, as.data.frame))` — it's a bit more
168184
> verbose but handles some tricky list shapes.
169185
170-
## Step 7: Make a quick plot
186+
## Step 9: Make a quick plot
171187

172188
Once you have a data frame you can plot with `ggplot2`:
173189

@@ -190,11 +206,54 @@ ggplot(ridership_df, aes(x = as.Date(date), y = count)) +
190206
theme_minimal()
191207
```
192208

209+
## Step 7: Aggregate endpoints
210+
211+
The aggregate endpoints return long-run averages rather than individual trip
212+
events — useful for trend analysis over weeks or months.
213+
214+
```{r aggregate, eval = FALSE}
215+
# Average travel time from Park Street to Davis, by date
216+
travel_agg <- tm_aggregate_travel_times(
217+
from_stop = "70076",
218+
to_stop = "70064",
219+
start_date = "2024-01-01",
220+
end_date = "2024-01-31"
221+
)
222+
223+
# Same data via the v2 endpoint (returns by_date and by_time breakdowns)
224+
travel_agg2 <- tm_aggregate_travel_times2(
225+
from_stop = "70076",
226+
to_stop = "70064",
227+
start_date = "2024-01-01",
228+
end_date = "2024-01-31"
229+
)
230+
231+
# Average headways at a stop over a date range
232+
headways_agg <- tm_aggregate_headways(
233+
stop = "70061",
234+
start_date = "2024-01-01",
235+
end_date = "2024-01-31"
236+
)
237+
238+
# Average dwell times at a stop over a date range
239+
dwells_agg <- tm_aggregate_dwells(
240+
stop = "70061",
241+
start_date = "2024-01-01",
242+
end_date = "2024-01-31"
243+
)
244+
```
245+
246+
> **Tip:** The v2 travel times endpoint (`tm_aggregate_travel_times2`) is used
247+
> in the Red Line Analysis vignette for a worked plotting example.
248+
193249
## Recap: the functions at a glance
194250

195251
| Function | What it returns | Needs |
196252
|---|---|---|
197253
| `tm_healthcheck()` | API status | nothing |
254+
| `tm_git_id()` | Server git commit SHA | nothing |
255+
| `tm_time_predictions()` | Live time predictions | nothing |
256+
| `tm_service_ridership_dashboard()` | System-wide ridership summary | nothing |
198257
| `tm_facilities()` | All MBTA stations | nothing |
199258
| `tm_routes()` | All available routes by mode | nothing |
200259
| `tm_stops(route_id)` | Stop IDs for a route | route ID |
@@ -205,9 +264,13 @@ ggplot(ridership_df, aes(x = as.Date(date), y = count)) +
205264
| `tm_ridership(start, end)` | Ridership counts | date range |
206265
| `tm_trip_metrics(start, end, agg, line)` | Trip performance | date range + line |
207266
| `tm_line_delays(start, end, line)` | Delay summaries | date range + line |
267+
| `tm_scheduled_service(start, end, agg)` | Scheduled trip counts | date range + agg |
268+
| `tm_service_hours(start, end, agg, line_id)` | Revenue service hours | date range + agg + line |
208269
| `tm_speed_restrictions(line_id, date)` | Slow zones | line + date |
209270
| `tm_aggregate_travel_times(...)` | Long-run travel time trends | stop pair + range |
271+
| `tm_aggregate_travel_times2(...)` | Long-run travel times (v2, with by_date/by_time) | stop pair + range |
210272
| `tm_aggregate_headways(...)` | Long-run headway trends | stop + range |
273+
| `tm_aggregate_dwells(...)` | Long-run dwell time trends | stop + range |
211274

212275
Next, check out the **Red Line Analysis** vignette for a full worked example
213276
that goes from raw API data to a polished chart.

0 commit comments

Comments
 (0)