@@ -8,9 +8,21 @@ import (
88 "github.com/stretchr/testify/assert"
99 "github.com/stretchr/testify/require"
1010 "maglev.onebusaway.org/internal/clock"
11+ "maglev.onebusaway.org/internal/models"
1112 "maglev.onebusaway.org/internal/utils"
1213)
1314
15+ type scheduleForRouteResponse struct {
16+ Code int `json:"code"`
17+ Data scheduleForRouteData `json:"data"`
18+ Text string `json:"text"`
19+ }
20+
21+ type scheduleForRouteData struct {
22+ Entry models.ScheduleForRouteEntry `json:"entry"`
23+ References models.ReferencesModel `json:"references"`
24+ }
25+
1426func TestScheduleForRouteHandler (t * testing.T ) {
1527
1628 clk := clock .NewMockClock (time .Date (2025 , 12 , 26 , 12 , 0 , 0 , 0 , time .UTC ))
@@ -210,48 +222,25 @@ func TestScheduleForRouteHandler_DirectionIDMatchesCSV(t *testing.T) {
210222
211223 routeID := utils .FormCombinedID ("25" , "1885" )
212224 endpoint := "/api/where/schedule-for-route/" + routeID + ".json?key=TEST&date=2025-06-12"
213- resp , model := serveApiAndRetrieveEndpoint (t , api , endpoint )
225+ resp , model := callAPIHandler [ scheduleForRouteResponse ] (t , api , endpoint )
214226 require .Equal (t , http .StatusOK , resp .StatusCode )
215227
216- data , ok := model .Data .(map [string ]interface {})
217- require .True (t , ok , "model.Data should be a map" )
218- entry , ok := data ["entry" ].(map [string ]interface {})
219- require .True (t , ok , "entry should be a map" )
220- groupings , ok := entry ["stopTripGroupings" ].([]interface {})
221- require .True (t , ok , "stopTripGroupings should be a slice" )
222- require .Len (t , groupings , 2 )
223-
224- refs , ok := data ["references" ].(map [string ]interface {})
225- require .True (t , ok , "references should be a map" )
226- tripRefs , ok := refs ["trips" ].([]interface {})
227- require .True (t , ok , "trips should be a slice" )
228-
229- tripDirByID := make (map [string ]string , len (tripRefs ))
230- for _ , tr := range tripRefs {
231- trMap , ok := tr .(map [string ]interface {})
232- require .True (t , ok , "trip ref should be a map" )
233- tid , _ := trMap ["id" ].(string )
234- dir , _ := trMap ["directionId" ].(string )
235- tripDirByID [tid ] = dir
228+ groupings := model .Data .Entry .StopTripGroupings
229+ require .Len (t , groupings , 2 , "route 25_1885 has trips in both directions" )
230+
231+ tripDirByID := make (map [string ]string , len (model .Data .References .Trips ))
232+ for _ , tr := range model .Data .References .Trips {
233+ tripDirByID [tr .ID ] = tr .DirectionID
236234 }
237235
238- first , ok := groupings [0 ].(map [string ]interface {})
239- require .True (t , ok , "first grouping should be a map" )
240- second , ok := groupings [1 ].(map [string ]interface {})
241- require .True (t , ok , "second grouping should be a map" )
242- assert .Equal (t , "0" , first ["directionId" ])
243- assert .Equal (t , "1" , second ["directionId" ])
236+ assert .Equal (t , "0" , groupings [0 ].DirectionID )
237+ assert .Equal (t , "1" , groupings [1 ].DirectionID )
244238
245239 for _ , g := range groupings {
246- gMap , ok := g .(map [string ]interface {})
247- require .True (t , ok , "grouping should be a map" )
248- gid , _ := gMap ["directionId" ].(string )
249- tripIDs , _ := gMap ["tripIds" ].([]interface {})
250- require .NotEmpty (t , tripIDs )
251- for _ , tid := range tripIDs {
252- ts , _ := tid .(string )
253- assert .Equal (t , gid , tripDirByID [ts ],
254- "group %q trip %q should have CSV direction_id %q, got %q" , gid , ts , gid , tripDirByID [ts ])
240+ require .NotEmpty (t , g .TripIDs )
241+ for _ , tid := range g .TripIDs {
242+ assert .Equal (t , g .DirectionID , tripDirByID [tid ],
243+ "group %q trip %q should have CSV direction_id %q, got %q" , g .DirectionID , tid , g .DirectionID , tripDirByID [tid ])
255244 }
256245 }
257246}
0 commit comments