Skip to content

Commit 4394e81

Browse files
committed
fix: Sort trips by ID for deterministic trip selection and consistent test results.
1 parent 0ad716e commit 4394e81

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

internal/restapi/stops_for_location_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (api *RestAPI) stopsForLocationHandler(w http.ResponseWriter, r *http.Reque
167167

168168
agencyId, _, err := utils.ExtractAgencyIDAndCodeID(routeIDStr)
169169
if err != nil {
170-
continue // Skip malformed route IDs
170+
continue // Skip malformed route IDs
171171
}
172172
stopRouteIDs[stopID] = append(stopRouteIDs[stopID], routeIDStr)
173173
agencyIDs[agencyId] = true

internal/restapi/stops_for_route_handler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func processTripGroups(
335335

336336
for _, key := range keys {
337337
tripsInGroup := tripGroups[key]
338+
339+
// Sort trips by ID to ensure we always pick the same representative trip
340+
sort.Slice(tripsInGroup, func(i, j int) bool {
341+
return tripsInGroup[i].ID < tripsInGroup[j].ID
342+
})
343+
338344
representativeTrip := tripsInGroup[0]
339345
stopsList, err := api.GtfsManager.GtfsDB.Queries.GetOrderedStopIDsForTrip(ctx, representativeTrip.ID)
340346
if err != nil {

internal/restapi/stops_for_route_handler_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ func TestStopsForRouteHandlerEndToEnd(t *testing.T) {
7575
inboundStopIds, ok := inboundGroup["stopIds"].([]interface{})
7676
require.True(t, ok)
7777

78-
// TODO: why is this varying between 21 and 22 depending on the test run?
79-
either21Or22 := len(inboundStopIds) == 21 || len(inboundStopIds) == 22
80-
assert.True(t, either21Or22, "Expected 21 or 22 stop IDs, got %d", len(inboundStopIds))
78+
// With deterministic sorting, checks should be consistent
79+
assert.Equal(t, 21, len(inboundStopIds))
8180

8281
inboundPolylines, ok := inboundGroup["polylines"].([]interface{})
8382
require.True(t, ok)
@@ -95,9 +94,8 @@ func TestStopsForRouteHandlerEndToEnd(t *testing.T) {
9594

9695
outboundStopIds, ok := outboundGroup["stopIds"].([]interface{})
9796
require.True(t, ok)
98-
// TODO: why is this varying between 21 and 22 depending on the test run?
99-
either21Or22 = len(outboundStopIds) == 21 || len(outboundStopIds) == 22
100-
assert.True(t, either21Or22, "Expected 21 or 22 stop IDs, got %d", len(outboundStopIds))
97+
// With deterministic sorting, checks should be consistent
98+
assert.Equal(t, 22, len(outboundStopIds))
10199

102100
// Verify references
103101
refs, ok := data["references"].(map[string]interface{})

internal/restapi/test_helper_test.go

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type mockTestingFatalf struct {
1313
failed bool
14-
err string
14+
err string
1515
}
1616

1717
func (m *mockTestingFatalf) Fatalf(format string, args ...any) {
@@ -22,11 +22,9 @@ func (m *mockTestingFatalf) Fatalf(format string, args ...any) {
2222

2323
func TestCollectAllNestedIdsFromObjects(t *testing.T) {
2424
data := []interface{}{
25-
map[string]interface{}{"routes":
26-
[]interface{}{"234", "235"},
25+
map[string]interface{}{"routes": []interface{}{"234", "235"},
2726
},
28-
map[string]interface{}{"routes":
29-
[]interface{}{"345"},
27+
map[string]interface{}{"routes": []interface{}{"345"},
3028
},
3129
}
3230
expected := []string{"234", "235", "345"}
@@ -42,33 +40,32 @@ func TestCollectAllNestedIdsFromObjectsFailures(t *testing.T) {
4240
expectedError string
4341
}{
4442
{
45-
name: "Invalid object type in the array",
46-
data: []interface{}{
47-
map[int]interface{}{1: "234"},
48-
},
43+
name: "Invalid object type in the array",
44+
data: []interface{}{
45+
map[int]interface{}{1: "234"},
46+
},
4947
expectedError: "item 0 is not a map[string]interface{}",
5048
},
5149
{
52-
name: "Missing key from the object",
53-
data: []interface{}{
54-
map[string]interface{}{"id": "234"},
55-
},
50+
name: "Missing key from the object",
51+
data: []interface{}{
52+
map[string]interface{}{"id": "234"},
53+
},
5654
expectedError: "item 0 missing key \"routes\"",
5755
},
5856
{
59-
name: "Invalid nested object",
60-
data: []interface{}{
61-
map[string]interface{}{"routes": "234"},
62-
},
57+
name: "Invalid nested object",
58+
data: []interface{}{
59+
map[string]interface{}{"routes": "234"},
60+
},
6361
expectedError: "item 0 key \"routes\" is not a []interface{}: string",
6462
},
6563
{
66-
name: "Invalid nested array type",
67-
data: []interface{}{
68-
map[string]interface{}{"routes":
69-
[]interface{}{234},
70-
},
71-
},
64+
name: "Invalid nested array type",
65+
data: []interface{}{
66+
map[string]interface{}{"routes": []interface{}{234},
67+
},
68+
},
7269
expectedError: "item 0 key \"routes\" index 0 is not a string: int",
7370
},
7471
}
@@ -109,24 +106,24 @@ func TestCollectAllIdsFromObjectsFailures(t *testing.T) {
109106
expectedError string
110107
}{
111108
{
112-
name: "Invalid object type in the array",
113-
data: []interface{}{
114-
map[int]interface{}{1: "234"},
115-
},
109+
name: "Invalid object type in the array",
110+
data: []interface{}{
111+
map[int]interface{}{1: "234"},
112+
},
116113
expectedError: "item 0 is not a map[string]interface{}",
117114
},
118115
{
119-
name: "Missing key from the object",
120-
data: []interface{}{
121-
map[string]interface{}{"name": "234"},
122-
},
116+
name: "Missing key from the object",
117+
data: []interface{}{
118+
map[string]interface{}{"name": "234"},
119+
},
123120
expectedError: "item 0 missing key \"id\"",
124121
},
125122
{
126-
name: "Invalid nested object",
127-
data: []interface{}{
128-
map[string]interface{}{"id": 234},
129-
},
123+
name: "Invalid nested object",
124+
data: []interface{}{
125+
map[string]interface{}{"id": 234},
126+
},
130127
expectedError: "item 0 key \"id\" is not a string: int",
131128
},
132129
}

0 commit comments

Comments
 (0)