-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfeed_validation_test.go
More file actions
532 lines (457 loc) · 18.5 KB
/
Copy pathfeed_validation_test.go
File metadata and controls
532 lines (457 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package main
import (
"fmt"
"strings"
"testing"
"time"
gtfsrt "github.com/OneBusAway/go-gtfs/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/OneBusAway/vehicle-positions/rider"
)
// validateFeedCompliance checks a FeedMessage against applicable
// MobilityData GTFS-RT validator rules for VehiclePosition feeds.
// Returns a slice of rule violation strings. Empty = compliant.
//
// Callers typically build a feed via buildFeed() and pass the result here.
func validateFeedCompliance(t *testing.T, feed *gtfsrt.FeedMessage) []string {
t.Helper()
var violations []string
// E038: valid GTFS-RT version
if v := feed.Header.GetGtfsRealtimeVersion(); v != "1.0" && v != "2.0" {
violations = append(violations, "E038: invalid gtfs_realtime_version: "+v)
}
// E048: header timestamp required for v2.0
if feed.Header.Timestamp == nil || *feed.Header.Timestamp == 0 {
violations = append(violations, "E048: header timestamp missing or zero")
}
// E049: incrementality required for v2.0
if feed.Header.Incrementality == nil {
violations = append(violations, "E049: incrementality missing")
}
headerTs := feed.Header.GetTimestamp()
now := uint64(time.Now().Unix())
seenIDs := make(map[string]struct{})
seenVehicleIDs := make(map[string]string)
for _, entity := range feed.Entity {
id := entity.GetId()
// Unique entity ids (GTFS-RT spec requirement; no numbered rule).
if _, exists := seenIDs[id]; exists {
violations = append(violations, fmt.Sprintf("duplicate entity id %q", id))
}
seenIDs[id] = struct{}{}
vp := entity.GetVehicle()
if vp == nil {
continue
}
// W002: vehicle.id populated
vehicleID := vp.GetVehicle().GetId()
if vehicleID == "" {
violations = append(violations, fmt.Sprintf("W002: vehicle.id empty for entity %q", id))
} else {
// E052: vehicle.id must be unique across the feed — two entities
// describing the same vehicle at once is the error MobilityData
// names. Driver ids cannot contain ":" (vehicleIDPattern), and
// rider ids carry the trip and the start date, so the two halves
// of the feed cannot collide.
if prev, exists := seenVehicleIDs[vehicleID]; exists {
violations = append(violations, fmt.Sprintf("E052: vehicle.id %q not unique (entities %q and %q)", vehicleID, prev, id))
}
seenVehicleIDs[vehicleID] = id
}
// W001: timestamp populated
entityTs := vp.GetTimestamp()
if entityTs == 0 {
violations = append(violations, fmt.Sprintf("W001: timestamp zero for entity %q", id))
}
// E001: POSIX seconds (not milliseconds)
if entityTs > 10_000_000_000 {
violations = append(violations, fmt.Sprintf("E001: timestamp looks like ms for entity %q", id))
}
// E012: header timestamp >= entity timestamp
if entityTs > headerTs {
violations = append(violations, fmt.Sprintf("E012: entity %q ts %d > header ts %d", id, entityTs, headerTs))
}
// E050: not >60s in future
if entityTs > now+60 {
violations = append(violations, fmt.Sprintf("E050: entity %q timestamp >60s in future", id))
}
pos := vp.GetPosition()
if pos != nil {
// E026: valid WGS84 coordinates
if lat := pos.GetLatitude(); lat < -90 || lat > 90 {
violations = append(violations, fmt.Sprintf("E026: latitude %f out of range for %q", lat, id))
}
if lon := pos.GetLongitude(); lon < -180 || lon > 180 {
violations = append(violations, fmt.Sprintf("E026: longitude %f out of range for %q", lon, id))
}
// E027: bearing 0..360 (inclusive per MobilityData E027 rule).
// Bearing is optional; buildFeed() leaves pos.Bearing nil when the
// source vehicle has no bearing, so the nil guard is required.
if pos.Bearing != nil {
if b := pos.GetBearing(); b < 0 || b > 360 {
violations = append(violations, fmt.Sprintf("E027: bearing %f out of range for %q", b, id))
}
}
// Speed non-negative (no formal validator rule number).
if pos.Speed != nil && pos.GetSpeed() < 0 {
violations = append(violations, fmt.Sprintf("speed: must be non-negative for entity %q", id))
}
}
// E039: no is_deleted in FULL_DATASET
if entity.IsDeleted != nil && *entity.IsDeleted {
violations = append(violations, fmt.Sprintf("E039: is_deleted set for %q", id))
}
}
// E050: header timestamp not >60s in future
if headerTs > now+60 {
violations = append(violations, "E050: header timestamp >60s in future")
}
return violations
}
// --- Individual rule tests ---
func TestFeedValidation_E001_TimestampsInPOSIXSeconds(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
entityTs := feed.Entity[0].Vehicle.GetTimestamp()
assert.Less(t, entityTs, uint64(10_000_000_000), "E001: timestamp should be in seconds, not ms")
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E012_HeaderTimestampGEEntityTimestamps(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now - 30},
{VehicleID: "bus-2", Latitude: 3, Longitude: 4, Timestamp: now - 10},
}
feed := buildFeed(vehicles, nil)
headerTs := feed.Header.GetTimestamp()
for _, e := range feed.Entity {
assert.GreaterOrEqual(t, headerTs, e.Vehicle.GetTimestamp(),
"E012: header must be >= entity %s", e.GetId())
}
}
func TestFeedValidation_E012_FutureEntityTimestamp(t *testing.T) {
// Use now+30 — within E050's 60s window — so the full compliance
// checker can run without triggering E050 on the header.
futureTs := time.Now().Unix() + 30
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: futureTs},
}
feed := buildFeed(vehicles, nil)
headerTs := feed.Header.GetTimestamp()
entityTs := feed.Entity[0].Vehicle.GetTimestamp()
assert.GreaterOrEqual(t, headerTs, entityTs,
"E012: header timestamp must be >= entity timestamp")
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E012_AllPastEntities(t *testing.T) {
// All vehicles have past timestamps.
// Header should be approximately time.Now(), not dragged down.
pastTs := time.Now().Unix() - 60
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: pastTs},
}
feed := buildFeed(vehicles, nil)
headerTs := feed.Header.GetTimestamp()
now := uint64(time.Now().Unix())
// Header should be close to now, not the past entity timestamp.
// Delta of 5 seconds accounts for CI environments under load.
assert.InDelta(t, float64(now), float64(headerTs), 5, "header should be close to now for past entities")
assert.GreaterOrEqual(t, headerTs, uint64(pastTs), "E012: header >= entity")
}
func TestFeedValidation_E026_ValidCoordinates(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "nairobi", Latitude: -1.2921, Longitude: 36.8219, Timestamp: now},
{VehicleID: "nyc", Latitude: 40.7128, Longitude: -74.0060, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E026_BoundaryCoordinates(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "north-pole", Latitude: 90, Longitude: 0.001, Timestamp: now},
{VehicleID: "south-pole", Latitude: -90, Longitude: 0.001, Timestamp: now},
{VehicleID: "dateline-east", Latitude: 0.001, Longitude: 180, Timestamp: now},
{VehicleID: "dateline-west", Latitude: 0.001, Longitude: -180, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "boundary coordinates should be valid")
}
func TestFeedValidation_E027_ValidBearing(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Bearing: float64ptr(180), Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E027_BearingBoundaries(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "north", Latitude: 1, Longitude: 2, Bearing: float64ptr(0), Timestamp: now},
{VehicleID: "east", Latitude: 3, Longitude: 4, Bearing: float64ptr(90), Timestamp: now},
{VehicleID: "south", Latitude: 5, Longitude: 6, Bearing: float64ptr(180), Timestamp: now},
{VehicleID: "wrap", Latitude: 7, Longitude: 8, Bearing: float64ptr(360), Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "all bearings 0-360 should be valid")
}
func TestFeedValidation_E038_ValidVersion(t *testing.T) {
feed := buildFeed(nil, nil)
assert.Equal(t, "2.0", feed.Header.GetGtfsRealtimeVersion())
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E039_NoIsDeletedInFullDataset(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
for _, e := range feed.Entity {
assert.Nil(t, e.IsDeleted, "E039: is_deleted must not be set in FULL_DATASET")
}
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_E048_HeaderTimestampPopulated(t *testing.T) {
feed := buildFeed(nil, nil)
assert.NotNil(t, feed.Header.Timestamp, "E048: header timestamp required for v2.0")
assert.NotZero(t, feed.Header.GetTimestamp())
}
func TestFeedValidation_E049_IncrementalityPopulated(t *testing.T) {
feed := buildFeed(nil, nil)
assert.NotNil(t, feed.Header.Incrementality, "E049: incrementality required for v2.0")
assert.Equal(t, gtfsrt.FeedHeader_FULL_DATASET, feed.Header.GetIncrementality())
}
func TestFeedValidation_E050_TimestampsNotFarFuture(t *testing.T) {
// Use current timestamps — these should never trigger E050.
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
{VehicleID: "bus-2", Latitude: 3, Longitude: 4, Timestamp: now - 30},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "current/past timestamps should not trigger E050")
}
func TestFeedValidation_E050_RejectsEntityFarInFuture(t *testing.T) {
now := time.Now().Unix()
feed := buildFeed([]*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now + 120},
}, nil)
violations := validateFeedCompliance(t, feed)
require.NotEmpty(t, violations)
joined := strings.Join(violations, "|")
assert.Contains(t, joined, "E050:")
}
func TestFeedValidation_E050_IngestWindowGap(t *testing.T) {
// Ingest allows now+300s but E050 rejects >now+60s.
// A vehicle at now+120 passes ingest but fails E050.
// Placeholder documenting the gap; skipped pending follow-up — no assertion is made.
t.Skip("known limitation: ingest accepts now+300s but E050 rejects >now+60s — tracked for follow-up")
}
func TestFeedValidation_E052_UniqueVehicleIDs(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
{VehicleID: "bus-2", Latitude: 3, Longitude: 4, Timestamp: now},
{VehicleID: "bus-3", Latitude: 5, Longitude: 6, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "unique IDs should not trigger E052")
}
func TestFeedValidation_E052_RejectsDuplicateIDs(t *testing.T) {
now := time.Now().Unix()
feed := buildFeed([]*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}, nil)
// The Tracker dedups by VehicleID upstream (tracker.go), so buildFeed
// never emits duplicate IDs in practice — inject a raw entity to exercise E052.
dup := >fsrt.FeedEntity{
Id: proto.String("bus-1"),
Vehicle: >fsrt.VehiclePosition{
Vehicle: >fsrt.VehicleDescriptor{Id: proto.String("bus-1")},
Position: >fsrt.Position{Latitude: proto.Float32(1), Longitude: proto.Float32(2)},
Timestamp: proto.Uint64(uint64(now)),
},
}
feed.Entity = append(feed.Entity, dup)
violations := validateFeedCompliance(t, feed)
require.NotEmpty(t, violations)
joined := strings.Join(violations, "|")
assert.Contains(t, joined, "E052:")
assert.Contains(t, joined, "bus-1")
}
func TestFeedValidation_E052_RiderVehicleIDsUniqueAcrossStartDates(t *testing.T) {
// The same trip running on two service dates is two vehicles. When
// vehicle.id was just "rider:<trip_id>" both entities claimed one vehicle.
yesterday := sampleEstimate()
yesterday.Key.StartDate = "20260901"
feed := buildFeed(nil, []rider.TripEstimate{sampleEstimate(), yesterday})
require.Len(t, feed.Entity, 2)
assert.Equal(t, "rider:T1:20260902", feed.Entity[0].GetVehicle().GetVehicle().GetId())
assert.Equal(t, "rider:T1:20260901", feed.Entity[1].GetVehicle().GetVehicle().GetId())
assert.Empty(t, validateFeedCompliance(t, feed))
}
func TestFeedValidation_E052_RejectsDuplicateVehicleIDs(t *testing.T) {
now := time.Now().Unix()
feed := buildFeed([]*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}, nil)
// Distinct entity id, same vehicle.id: exactly what E052 forbids.
feed.Entity = append(feed.Entity, >fsrt.FeedEntity{
Id: proto.String("bus-1-second-entity"),
Vehicle: >fsrt.VehiclePosition{
Vehicle: >fsrt.VehicleDescriptor{Id: proto.String("bus-1")},
Position: >fsrt.Position{Latitude: proto.Float32(1), Longitude: proto.Float32(2)},
Timestamp: proto.Uint64(uint64(now)),
},
})
violations := validateFeedCompliance(t, feed)
require.NotEmpty(t, violations)
assert.Contains(t, strings.Join(violations, "|"), `E052: vehicle.id "bus-1" not unique`)
}
func TestFeedValidation_W001_TimestampsPopulated(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
for _, e := range feed.Entity {
assert.NotZero(t, e.Vehicle.GetTimestamp(), "W001: entity timestamp must be populated")
}
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_W002_VehicleIDPopulated(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", Latitude: 1, Longitude: 2, Timestamp: now},
}
feed := buildFeed(vehicles, nil)
for _, e := range feed.Entity {
assert.NotEmpty(t, e.Vehicle.GetVehicle().GetId(), "W002: vehicle.id must be populated")
}
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_SpeedNonNegative(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "stationary", Latitude: 1, Longitude: 2, Speed: float64ptr(0), Timestamp: now},
{VehicleID: "moving", Latitude: 3, Longitude: 4, Speed: float64ptr(15.5), Timestamp: now},
}
feed := buildFeed(vehicles, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "zero and positive speeds should be valid")
}
// --- Composite tests ---
func TestFeedValidation_EmptyFeedValid(t *testing.T) {
feed := buildFeed(nil, nil)
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "empty feed should be valid: %v", violations)
assert.Equal(t, "2.0", feed.Header.GetGtfsRealtimeVersion())
assert.NotNil(t, feed.Header.Timestamp)
assert.NotZero(t, feed.Header.GetTimestamp())
assert.Empty(t, feed.Entity)
}
func TestFeedValidation_FullFeedCompliance(t *testing.T) {
// 3 realistic vehicles — mix of with/without trips, varying fields.
now := time.Now().Unix()
vehicles := []*VehicleState{
{
VehicleID: "vehicle-042",
TripID: "route_5_0830",
Latitude: -1.2921,
Longitude: 36.8219,
Bearing: float64ptr(180.0),
Speed: float64ptr(8.5),
Timestamp: now,
},
{
VehicleID: "vehicle-100",
Latitude: -1.3000,
Longitude: 36.8300,
Bearing: float64ptr(0.0), // heading north — valid
Speed: float64ptr(0.0), // stationary — valid
Timestamp: now - 30,
},
{
VehicleID: "vehicle-200",
TripID: "route_10_0900",
Latitude: 40.7128,
Longitude: -74.0060,
Bearing: float64ptr(270.0),
Speed: float64ptr(12.5),
Timestamp: now - 5,
},
}
feed := buildFeed(vehicles, nil)
// Run full compliance check
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations, "feed should pass all validator rules: %v", violations)
// Verify structure
require.Len(t, feed.Entity, 3)
assert.Equal(t, "2.0", feed.Header.GetGtfsRealtimeVersion())
assert.Equal(t, gtfsrt.FeedHeader_FULL_DATASET, feed.Header.GetIncrementality())
// Verify trip assignment
var withTrip, withoutTrip int
for _, e := range feed.Entity {
if e.Vehicle.Trip != nil {
withTrip++
} else {
withoutTrip++
}
}
assert.Equal(t, 2, withTrip, "two vehicles should have trips")
assert.Equal(t, 1, withoutTrip, "one vehicle should have no trip")
}
func TestFeedValidation_ZeroTimestampVehicleSkipped(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "valid", Latitude: 1, Longitude: 2, Timestamp: now},
{VehicleID: "zero-ts", Latitude: 3, Longitude: 4, Timestamp: 0},
{VehicleID: "negative-ts", Latitude: 5, Longitude: 6, Timestamp: -100},
}
feed := buildFeed(vehicles, nil)
// Only the valid vehicle should appear in the feed
require.Len(t, feed.Entity, 1)
assert.Equal(t, "valid", feed.Entity[0].GetId())
violations := validateFeedCompliance(t, feed)
assert.Empty(t, violations)
}
func TestFeedValidation_FeedSerializesCleanly(t *testing.T) {
now := time.Now().Unix()
vehicles := []*VehicleState{
{VehicleID: "bus-1", TripID: "trip-1", Latitude: -1.29, Longitude: 36.82,
Bearing: float64ptr(180), Speed: float64ptr(8.5), Timestamp: now},
}
feed := buildFeed(vehicles, nil)
// Verify protobuf round-trip: marshal then unmarshal
data, err := proto.Marshal(feed)
require.NoError(t, err, "feed must serialize to protobuf")
var decoded gtfsrt.FeedMessage
err = proto.Unmarshal(data, &decoded)
require.NoError(t, err, "feed must deserialize from protobuf")
require.Len(t, decoded.Entity, 1)
assert.Equal(t, "bus-1", decoded.Entity[0].GetId())
assert.Equal(t, float32(-1.29), decoded.Entity[0].Vehicle.Position.GetLatitude())
assert.Equal(t, "trip-1", decoded.Entity[0].Vehicle.Trip.GetTripId())
// Verify the deserialized feed also passes compliance
violations := validateFeedCompliance(t, &decoded)
assert.Empty(t, violations, "round-tripped feed should be compliant: %v", violations)
}