@@ -3,7 +3,6 @@ package validator
33import (
44 "context"
55 "net/http"
6- "strings"
76 "testing"
87
98 gtfs "github.com/OneBusAway/go-gtfs"
@@ -12,29 +11,19 @@ import (
1211)
1312
1413func TestServiceAlertFoundInSituationIDs (t * testing.T ) {
15- client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) {
16- if strings .Contains (r .URL .Path , "arrivals-and-departures-for-stop" ) {
17- w .Header ().Set ("Content-Type" , "application/json" )
18- w .Write ([]byte (`{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1","situationIds":["1_ALERT1"]}]}}}` ))
19- return
20- }
21- t .Errorf ("unexpected path %s" , r .URL .Path )
22- })
23- src := & SourceContext {
24- Label : "ds0" ,
25- Config : config.DataSource {AgencyMapping : map [string ]string {"KCM" : "1" }},
26- PrepErrors : map [string ]error {},
27- Static : staticForVehicle (),
28- ServiceAlerts : & gtfs.Realtime {Alerts : []gtfs.Alert {{
29- ID : "ALERT1" ,
30- InformedEntities : []gtfs.AlertInformedEntity {{StopID : strp ("ST1" )}},
31- }}},
32- }
14+ client := arrivalsClient (t , `{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1","situationIds":["1_ALERT1"]}]}}}` )
3315 vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
34- results := serviceAlertCheck {}.Run (context .Background (), vc , src )
35- if len (results ) == 0 || results [0 ].Status != Pass {
36- t .Errorf ("want Pass, got %+v" , results )
37- }
16+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
17+ assertFirstStatus (t , results , Pass , "alert in situationIds" )
18+ }
19+
20+ // A `null` arrivals response (nil SDK response, nil error) must not be mistaken
21+ // for "stop has no situations" and Fail — it is an unconfirmed query, so Warn.
22+ func TestServiceAlertNullArrivalsResponseWarns (t * testing.T ) {
23+ client := arrivalsClient (t , `null` )
24+ vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
25+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
26+ assertFirstStatus (t , results , Warn , "null arrivals response" )
3827}
3928
4029func TestServiceAlertNoSamplableWarns (t * testing.T ) {
@@ -47,109 +36,36 @@ func TestServiceAlertNoSamplableWarns(t *testing.T) {
4736 }
4837 vc := & ValidationContext {Config : cfgForTest ("test" )}
4938 results := serviceAlertCheck {}.Run (context .Background (), vc , src )
50- if results [0 ].Status != Warn {
51- t .Errorf ("agency-only alert not stop-referenceable: want Warn got %v" , results [0 ].Status )
52- }
39+ assertFirstStatus (t , results , Warn , "agency-only alert not stop-referenceable" )
5340}
5441
5542func TestServiceAlertNoSituationsFails (t * testing.T ) {
56- client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) {
57- if strings .Contains (r .URL .Path , "arrivals-and-departures-for-stop" ) {
58- w .Header ().Set ("Content-Type" , "application/json" )
59- // Arrivals present but NO situations at all, though the feed says this stop is affected.
60- w .Write ([]byte (`{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1"}]}}}` ))
61- return
62- }
63- t .Errorf ("unexpected path %s" , r .URL .Path )
64- })
65- src := & SourceContext {
66- Label : "ds0" ,
67- Config : config.DataSource {AgencyMapping : map [string ]string {"KCM" : "1" }},
68- PrepErrors : map [string ]error {},
69- Static : staticForVehicle (),
70- ServiceAlerts : & gtfs.Realtime {Alerts : []gtfs.Alert {{
71- ID : "ALERT1" ,
72- InformedEntities : []gtfs.AlertInformedEntity {{StopID : strp ("ST1" )}},
73- }}},
74- }
43+ // Arrivals present but NO situations at all, though the feed says this stop is affected.
44+ client := arrivalsClient (t , `{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1"}]}}}` )
7545 vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
76- results := serviceAlertCheck {}.Run (context .Background (), vc , src )
77- if len (results ) == 0 || results [0 ].Status != Fail {
78- t .Errorf ("affected stop with no situations should Fail, got %+v" , results )
79- }
46+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
47+ assertFirstStatus (t , results , Fail , "affected stop with no situations" )
8048}
8149
8250func TestServiceAlertSituationsButNoMatchWarns (t * testing.T ) {
83- client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) {
84- if strings .Contains (r .URL .Path , "arrivals-and-departures-for-stop" ) {
85- w .Header ().Set ("Content-Type" , "application/json" )
86- // Situations exist but none match the feed alert id.
87- w .Write ([]byte (`{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1","situationIds":["1_DIFFERENT"]}]}}}` ))
88- return
89- }
90- t .Errorf ("unexpected path %s" , r .URL .Path )
91- })
92- src := & SourceContext {
93- Label : "ds0" ,
94- Config : config.DataSource {AgencyMapping : map [string ]string {"KCM" : "1" }},
95- PrepErrors : map [string ]error {},
96- Static : staticForVehicle (),
97- ServiceAlerts : & gtfs.Realtime {Alerts : []gtfs.Alert {{
98- ID : "ALERT1" ,
99- InformedEntities : []gtfs.AlertInformedEntity {{StopID : strp ("ST1" )}},
100- }}},
101- }
51+ // Situations exist but none match the feed alert id.
52+ client := arrivalsClient (t , `{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1","situationIds":["1_DIFFERENT"]}]}}}` )
10253 vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
103- results := serviceAlertCheck {}.Run (context .Background (), vc , src )
104- if len (results ) == 0 || results [0 ].Status != Warn {
105- t .Errorf ("situations present but no match should Warn, got %+v" , results )
106- }
54+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
55+ assertFirstStatus (t , results , Warn , "situations present but no match" )
10756}
10857
10958func TestServiceAlertFoundInGlobalReferences (t * testing.T ) {
110- client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) {
111- if strings .Contains (r .URL .Path , "arrivals-and-departures-for-stop" ) {
112- w .Header ().Set ("Content-Type" , "application/json" )
113- // situationIds empty on the arrival, but the alert IS in references.situations
114- w .Write ([]byte (`{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1"}]},"references":{"situations":[{"id":"1_ALERT1"}]}}}` ))
115- return
116- }
117- t .Errorf ("unexpected path %s" , r .URL .Path )
118- })
119- src := & SourceContext {
120- Label : "ds0" ,
121- Config : config.DataSource {AgencyMapping : map [string ]string {"KCM" : "1" }},
122- PrepErrors : map [string ]error {},
123- Static : staticForVehicle (),
124- ServiceAlerts : & gtfs.Realtime {Alerts : []gtfs.Alert {{
125- ID : "ALERT1" ,
126- InformedEntities : []gtfs.AlertInformedEntity {{StopID : strp ("ST1" )}},
127- }}},
128- }
59+ // situationIds empty on the arrival, but the alert IS in references.situations.
60+ client := arrivalsClient (t , `{"data":{"entry":{"arrivalsAndDepartures":[{"stopId":"1_ST1","tripId":"1_T1"}]},"references":{"situations":[{"id":"1_ALERT1"}]}}}` )
12961 vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
130- results := serviceAlertCheck {}.Run (context .Background (), vc , src )
131- if len (results ) == 0 || results [0 ].Status != Pass {
132- t .Errorf ("alert in global references.situations should Pass, got %+v" , results )
133- }
62+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
63+ assertFirstStatus (t , results , Pass , "alert in global references.situations" )
13464}
13565
13666func TestServiceAlert404StopWarns (t * testing.T ) {
137- client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) {
138- w .WriteHeader (http .StatusNotFound )
139- })
140- src := & SourceContext {
141- Label : "ds0" ,
142- Config : config.DataSource {AgencyMapping : map [string ]string {"KCM" : "1" }},
143- PrepErrors : map [string ]error {},
144- Static : staticForVehicle (),
145- ServiceAlerts : & gtfs.Realtime {Alerts : []gtfs.Alert {{
146- ID : "ALERT1" ,
147- InformedEntities : []gtfs.AlertInformedEntity {{StopID : strp ("ST1" )}},
148- }}},
149- }
67+ client := newTestClient (t , func (w http.ResponseWriter , r * http.Request ) { w .WriteHeader (http .StatusNotFound ) })
15068 vc := & ValidationContext {Config : cfgForTest ("test" ), Client : client }
151- results := serviceAlertCheck {}.Run (context .Background (), vc , src )
152- if len (results ) == 0 || results [0 ].Status != Warn {
153- t .Errorf ("404 on stop should Warn (not Fail), got %+v" , results )
154- }
69+ results := serviceAlertCheck {}.Run (context .Background (), vc , alertSrcForStop ())
70+ assertFirstStatus (t , results , Warn , "404 on stop should Warn not Fail" )
15571}
0 commit comments