Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions gtfsdb/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions gtfsdb/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,13 @@ SELECT
FROM
trips;

-- name: ListTripsWithLimit :many
SELECT
*
FROM
trips
LIMIT ?;

-- name: GetArrivalsAndDeparturesForStop :many
SELECT
st.trip_id,
Expand Down
44 changes: 44 additions & 0 deletions gtfsdb/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/gtfs/gtfs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ func (manager *Manager) GetAgencies(ctx context.Context) ([]gtfsdb.Agency, error
return manager.GtfsDB.Queries.ListAgencies(ctx)
}

// IMPORTANT: Caller must hold manager.RLock() before calling this method.
func (manager *Manager) GetTrips() []gtfs.ScheduledTrip {
return manager.gtfsData.Trips
// GetTrips returns up to limit trips from the database.
func (manager *Manager) GetTrips(ctx context.Context, limit int64) ([]gtfsdb.Trip, error) {
return manager.GtfsDB.Queries.ListTripsWithLimit(ctx, limit)
}

// IMPORTANT: Caller must hold manager.RLock() before calling this method.
Expand Down
8 changes: 5 additions & 3 deletions internal/gtfs/gtfs_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func TestManager_GetTrips(t *testing.T) {
manager, _ := getSharedTestComponents(t)
assert.NotNil(t, manager)

trips := manager.GetTrips()
trips, err := manager.GetTrips(context.Background(), 100)
require.NoError(t, err)
assert.NotEmpty(t, trips)
assert.NotEmpty(t, trips[0].ID)
}
Expand Down Expand Up @@ -238,10 +239,11 @@ func TestManager_IsServiceActiveOnDate(t *testing.T) {
manager, _ := getSharedTestComponents(t)

// Get a trip to find a valid service ID
trips := manager.GetTrips()
trips, err := manager.GetTrips(context.Background(), 100)
require.NoError(t, err)
assert.NotEmpty(t, trips)

serviceID := trips[0].Service.Id
serviceID := trips[0].ServiceID

testCases := []struct {
name string
Expand Down
55 changes: 26 additions & 29 deletions internal/restapi/arrival_and_departure_for_stop_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ func TestArrivalAndDepartureForStopHandlerEndToEnd(t *testing.T) {

agency := mustGetAgencies(t, api)[0]
stops := mustGetStops(t, api)
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

if len(stops) == 0 {
t.Skip("No stops available for testing")
}

if len(trips) == 0 {
t.Skip("No trips available for testing")
}

stopID := utils.FormCombinedID(agency.ID, stops[0].ID)
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)
serviceDate := time.Now().Unix() * 1000

mux := http.NewServeMux()
Expand Down Expand Up @@ -118,9 +115,9 @@ func TestArrivalAndDepartureForStopHandlerWithInvalidStopID(t *testing.T) {
defer api.Shutdown()

agency := mustGetAgencies(t, api)[0]
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)
serviceDate := time.Now().Unix() * 1000

_, resp, model := serveAndRetrieveEndpoint(t,
Expand All @@ -139,18 +136,15 @@ func TestArrivalAndDepartureForStopHandlerWithTimeParameter(t *testing.T) {

agency := mustGetAgencies(t, api)[0]
stops := mustGetStops(t, api)
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

if len(stops) == 0 {
t.Skip("No stops available for testing")
}

if len(trips) == 0 {
t.Skip("No trips available for testing")
}

stopID := utils.FormCombinedID(agency.ID, stops[0].ID)
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)

// Use a specific time (1 hour from now)
specificTime := time.Now().Add(1 * time.Hour)
Expand Down Expand Up @@ -223,10 +217,10 @@ func TestArrivalAndDepartureForStopHandlerRequiresServiceDate(t *testing.T) {

agency := mustGetAgencies(t, api)[0]
stops := mustGetStops(t, api)
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

stopID := utils.FormCombinedID(agency.ID, stops[0].ID)
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)

mux := http.NewServeMux()
api.SetRoutes(mux)
Expand Down Expand Up @@ -260,18 +254,15 @@ func TestArrivalAndDepartureForStopHandlerWithStopSequence(t *testing.T) {

agency := mustGetAgencies(t, api)[0]
stops := mustGetStops(t, api)
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

if len(stops) == 0 {
t.Skip("No stops available for testing")
}

if len(trips) == 0 {
t.Skip("No trips available for testing")
}

stopID := utils.FormCombinedID(agency.ID, stops[0].ID)
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)
serviceDate := time.Now().Unix() * 1000
stopSequence := 1

Expand Down Expand Up @@ -299,18 +290,15 @@ func TestArrivalAndDepartureForStopHandlerWithMinutesParameters(t *testing.T) {

agency := mustGetAgencies(t, api)[0]
stops := mustGetStops(t, api)
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

if len(stops) == 0 {
t.Skip("No stops available for testing")
}

if len(trips) == 0 {
t.Skip("No trips available for testing")
}

stopID := utils.FormCombinedID(agency.ID, stops[0].ID)
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)
serviceDate := time.Now().Unix() * 1000

_, resp, model := serveAndRetrieveEndpoint(t,
Expand Down Expand Up @@ -369,10 +357,10 @@ func TestArrivalAndDepartureForStopHandlerWithMalformedStopID(t *testing.T) {
defer api.Shutdown()

agency := mustGetAgencies(t, api)[0]
trips := api.GtfsManager.GetTrips()
trip := mustGetTrip(t, api)

stopID := "malformedid" // No underscore, will fail extraction
tripID := utils.FormCombinedID(agency.ID, trips[0].ID)
tripID := utils.FormCombinedID(agency.ID, trip.ID)
serviceDate := time.Now().Unix() * 1000

_, resp, _ := serveAndRetrieveEndpoint(t,
Expand All @@ -390,7 +378,10 @@ func TestArrivalAndDepartureForStopHandlerWithValidTripStopCombination(t *testin
ctx := context.Background()

// Find a valid trip with stop times
trips := api.GtfsManager.GetTrips()
trips, err := api.GtfsManager.GetTrips(ctx, 100)
if err != nil {
t.Fatal(err)
}
if len(trips) == 0 {
t.Skip("No trips available for testing")
}
Expand Down Expand Up @@ -476,7 +467,10 @@ func TestArrivalAndDepartureForStopHandlerWithValidTripAndStopSequence(t *testin
ctx := context.Background()

// Find a valid trip with multiple stops
trips := api.GtfsManager.GetTrips()
trips, err := api.GtfsManager.GetTrips(ctx, 100)
if err != nil {
t.Fatal(err)
}
if len(trips) == 0 {
t.Skip("No trips available for testing")
}
Expand Down Expand Up @@ -1106,7 +1100,10 @@ func TestArrivalAndDepartureForStop_VehicleWithNilID(t *testing.T) {

ctx := context.Background()

trips := api.GtfsManager.GetTrips()
trips, err := api.GtfsManager.GetTrips(ctx, 100)
if err != nil {
t.Fatal(err)
}
require.NotEmpty(t, trips)

var validTripID, validStopID string
Expand Down
8 changes: 4 additions & 4 deletions internal/restapi/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func BenchmarkTripDetails(b *testing.B) {
defer cleanup()

agencies := mustGetAgencies(b, api)
trips := api.GtfsManager.GetTrips()
if len(agencies) == 0 || len(trips) == 0 {
b.Fatal("no agencies or trips")
if len(agencies) == 0 {
b.Fatal("no agencies")
}
tripID := utils.FormCombinedID(agencies[0].ID, trips[0].ID)
trip := mustGetTrip(b, api)
tripID := utils.FormCombinedID(agencies[0].ID, trip.ID)

mux := http.NewServeMux()
api.SetRoutes(mux)
Expand Down
3 changes: 2 additions & 1 deletion internal/restapi/calculate_block_trip_sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func TestCalculateBlockTripSequence(t *testing.T) {
api.GtfsManager.RLock()
defer api.GtfsManager.RUnlock()

trips := api.GtfsManager.GetTrips()
trips, err := api.GtfsManager.GetTrips(ctx, 100)
require.NoError(t, err)
require.NotEmpty(t, trips, "Should have test trips")

// Monday within the RABA dataset's active service period (calendar range covers this date)
Expand Down
9 changes: 9 additions & 0 deletions internal/restapi/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ func mustGetAgencies(t testing.TB, api *RestAPI) []gtfsdb.Agency {
return agencies
}

// mustGetTrip fetches a single trip from the DB for use in tests.
func mustGetTrip(t testing.TB, api *RestAPI) gtfsdb.Trip {
t.Helper()
trips, err := api.GtfsManager.GetTrips(context.Background(), 1)
require.NoError(t, err)
require.NotEmpty(t, trips, "test data should contain at least one trip")
return trips[0]
}

// serveAndRetrieveEndpoint sets up a test server, makes a request to the specified endpoint, and returns the response
// and decoded model.
// Accepts testing.TB to support both *testing.T and *testing.B
Expand Down
Loading
Loading