Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / gtfs_ingest / src / bin / transit_stops_extra.rs

transit_stops_extra.rs164 lines · 6.0 KiBRaw
1//! Tests for `transit_stops`, moved wholesale out of `transit_stops.rs` to keep
2//! the bin under the file-size limit. Behaviour-identical: same helpers, same
3//! assertions, only the module path changed.
4
5use super::{fold_stop_mode, json_escape, normalize_route_type, run, trip_modes};
6use gtfs_ingest::gtfs::{Csv, parse_csv};
7use gtfs_ingest::manifest::FeedSpec;
8use gtfs_ingest::par;
9use std::collections::HashMap;
10use std::path::PathBuf;
11
12/// The mode fold driven from a parsed `stop_times` table.
13///
14/// The production path streams the file instead, but both go through
15/// [`trip_modes`] and [`fold_stop_mode`], so this exercises the same decisions
16/// without a 5.8 GB fixture on disk.
17fn derive_from_csv(routes: &Csv, trips: &Csv, stop_times: &Csv) -> HashMap<String, u32> {
18 let trip_type = trip_modes(routes, trips);
19 let mut out = HashMap::new();
20 for row in &stop_times.rows {
21 fold_stop_mode(
22 &mut out,
23 &trip_type,
24 stop_times.get(row, "trip_id"),
25 stop_times.get(row, "stop_id"),
26 );
27 }
28 out
29}
30
31#[test]
32fn extended_route_types_fold_onto_the_basic_set() {
33 assert_eq!(normalize_route_type(3), 3, "plain bus is unchanged");
34 assert_eq!(normalize_route_type(109), 2, "suburban railway -> rail");
35 assert_eq!(normalize_route_type(401), 1, "metro -> subway");
36 assert_eq!(normalize_route_type(717), 3, "share taxi -> bus");
37 assert_eq!(normalize_route_type(900), 0, "tram service -> tram");
38 assert_eq!(normalize_route_type(1501), 3, "unknown -> bus");
39}
40
41#[test]
42fn rail_outranks_bus_at_a_shared_stop() {
43 let routes = parse_csv(
44 "route_id,route_type\n\
45 BUS,3\n\
46 SUB,1\n",
47 );
48 let trips = parse_csv(
49 "route_id,trip_id\n\
50 BUS,TB\n\
51 SUB,TS\n",
52 );
53 // The bus trip is listed first, so a naive first-wins would pick bus.
54 let stop_times = parse_csv(
55 "trip_id,stop_id,stop_sequence\n\
56 TB,SHARED,1\n\
57 TS,SHARED,1\n\
58 TB,BUSONLY,2\n",
59 );
60 let modes = derive_from_csv(&routes, &trips, &stop_times);
61 assert_eq!(modes.get("SHARED"), Some(&1), "subway wins over bus");
62 assert_eq!(modes.get("BUSONLY"), Some(&3));
63}
64
65#[test]
66fn an_unserved_stop_gets_no_mode() {
67 let routes = parse_csv("route_id,route_type\nR,3\n");
68 let trips = parse_csv("route_id,trip_id\nR,T\n");
69 let stop_times = parse_csv("trip_id,stop_id,stop_sequence\nT,A,1\n");
70 let modes = derive_from_csv(&routes, &trips, &stop_times);
71 // A parent station or orphan row no trip calls at is absent, which is what
72 // keeps it out of the layer.
73 assert_eq!(modes.get("STATION"), None);
74 assert_eq!(modes.get("A"), Some(&3));
75}
76
77/// Sharding feeds across threads must change nothing.
78///
79/// The fixture is built so the merge is genuinely order-sensitive: two feeds carry
80/// the SAME physical stop and BOTH have a MOTIS prefix, so the first-wins rule
81/// decides between them and only spec order can break the tie. With one prefixed
82/// feed the rule converges regardless of order, and the test would pass even if
83/// the merge were replayed in completion order — proving nothing.
84#[test]
85fn sharding_feeds_across_threads_changes_no_bytes() {
86 let root = std::env::temp_dir().join(format!("gtfs_shard_{}", std::process::id()));
87 let _ = std::fs::remove_dir_all(&root);
88
89 // Every feed has one shared platform — same name AND same position, so the
90 // dedup key really collides — plus one stop of its own.
91 let write_feed = |tag: &str| -> PathBuf {
92 let dir = root.join(tag);
93 std::fs::create_dir_all(&dir).unwrap();
94 let w = |file: &str, body: String| std::fs::write(dir.join(file), body).unwrap();
95 w("routes.txt", "route_id,route_type\nR1,3\nR2,1\n".to_string());
96 w("trips.txt", "route_id,trip_id\nR1,T1\nR2,T2\n".to_string());
97 w(
98 "stops.txt",
99 format!(
100 "stop_id,stop_name,stop_lat,stop_lon\n\
101 SHARED,Shared Platform,37.7749000,-122.4194000\n\
102 OWN{tag},Own {tag},37.8{},-122.3{}\n",
103 tag.len(),
104 tag.len()
105 ),
106 );
107 w(
108 "stop_times.txt",
109 format!(
110 "trip_id,stop_id,stop_sequence,arrival_time,departure_time\n\
111 T1,SHARED,1,08:00:00,08:00:00\n\
112 T2,OWN{tag},1,09:00:00,09:00:00\n"
113 ),
114 );
115 dir
116 };
117
118 // Seven feeds, so 2- and 3-thread batches both straddle a boundary. Feeds a
119 // and b both carry a prefix, so which of them wins SHARED depends on order.
120 let specs: Vec<FeedSpec> = ["a", "b", "c", "d", "e", "f", "g"]
121 .iter()
122 .map(|t| (t.to_string(), write_feed(t), format!("p{t}")))
123 .collect();
124
125 let run_at = |n: usize| -> Vec<u8> {
126 par::set_threads(n);
127 let out = root.join(format!("stops.t{n}.geojsonseq"));
128 run(&out, &specs).unwrap();
129 std::fs::read(&out).unwrap()
130 };
131
132 let base = run_at(1);
133 assert!(!base.is_empty(), "the fixture produced no stops");
134 // The shared platform survives exactly once, and it is the FIRST feed's.
135 let text = String::from_utf8(base.clone()).unwrap();
136 assert_eq!(
137 text.matches("Shared Platform").count(),
138 1,
139 "the shared stop must dedup to one feature: {text}"
140 );
141 assert!(
142 text.contains("\"pa_SHARED\""),
143 "feed 'a' comes first in spec order, so its id must survive: {text}"
144 );
145 for n in [2, 3, 7, 32] {
146 assert!(
147 run_at(n) == base,
148 "{n} threads perturbed the layer"
149 );
150 }
151 par::clear_threads();
152 let _ = std::fs::remove_dir_all(&root);
153}
154
155#[test]
156fn json_escaping_covers_quotes_and_controls() {
157 let mut out = Vec::new();
158 json_escape("A\"B\\C\tD".as_bytes(), &mut out);
159 assert_eq!(String::from_utf8(out).unwrap(), "A\\\"B\\\\C\\tD");
160 // Non-ASCII passes through as UTF-8 rather than being \u-escaped.
161 let mut utf8 = Vec::new();
162 json_escape("Béziers".as_bytes(), &mut utf8);
163 assert_eq!(String::from_utf8(utf8).unwrap(), "Béziers");
164}