Skip to content

Commit 4d21abe

Browse files
committed
Start automated change-detector tests, but keep them disabled, since the
GJ output isn't deterministic right now
1 parent 661eace commit 4d21abe

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

backend/src/common/tags.rs

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ impl From<HashMap<String, String>> for Tags {
5858

5959
#[cfg(test)]
6060
mod tests {
61+
use super::*;
62+
6163
#[test]
6264
fn various() {
6365
let mut tags = Tags::empty();

backend/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod render_cells;
2828
mod route;
2929
mod scrape;
3030
mod shortcuts;
31+
#[cfg(test)]
32+
mod tests;
3133

3234
static START: Once = Once::new();
3335

backend/src/tests.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use anyhow::Result;
2+
use geo::Polygon;
3+
use geojson::FeatureCollection;
4+
5+
use crate::{MapModel, Neighbourhood};
6+
7+
#[test]
8+
fn test_bristol_west() {
9+
test_example("bristol", "bristol_west", "west");
10+
}
11+
12+
fn test_example(study_area_name: &str, savefile_name: &str, neighbourhood_name: &str) {
13+
let output_path = format!("../tests/output/{savefile_name}.geojson");
14+
let expected = std::fs::read_to_string(&output_path).unwrap_or_else(|_| String::new());
15+
let actual = get_gj(study_area_name, savefile_name, neighbourhood_name).unwrap();
16+
17+
if expected != actual {
18+
std::fs::write(output_path, actual).unwrap();
19+
panic!("{savefile_name} has changed. Manually compare before/after changes with the web UI, then commit the output file to git.");
20+
}
21+
}
22+
23+
// TODO Must run from 'backend'
24+
// TODO web/public/osm must be symlinked to local PBF copies
25+
fn get_gj(study_area_name: &str, savefile_name: &str, neighbourhood_name: &str) -> Result<String> {
26+
let input_bytes = std::fs::read(format!("../web/public/osm/{study_area_name}.pbf"))?;
27+
let mut map = MapModel::new(&input_bytes, Some(study_area_name.to_string()))?;
28+
29+
let savefile: FeatureCollection =
30+
std::fs::read_to_string(format!("../tests/{savefile_name}.geojson"))?.parse()?;
31+
map.load_savefile(savefile)?;
32+
33+
// set_current_neighbourhood equivalent
34+
let boundary_gj = map.boundaries.get(neighbourhood_name).cloned().unwrap();
35+
let mut boundary_geo: Polygon = boundary_gj.try_into()?;
36+
map.mercator.to_mercator_in_place(&mut boundary_geo);
37+
let neighbourhood = Neighbourhood::new(&map, neighbourhood_name.to_string(), boundary_geo)?;
38+
39+
// TODO Include modal filters, once we detect existing ones
40+
41+
Ok(serde_json::to_string_pretty(&neighbourhood.to_gj(&map))?)
42+
}

tests/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Test cases
22

3-
This directory has project files for areas with boundaries that've proven buggy in the past. Eventually, we can also save the neighbourhood GeoJSON dump here too and have automated regression tests. Each file can be loaded from the title screen.
3+
This directory has project files for areas with boundaries that've proven buggy in the past. Each file can be loaded from the title screen.
4+
5+
The `output` directory has GeoJSON output capturing:
6+
7+
- Roads detected as interior to the neighbourhood
8+
- Cell boundaries
9+
- Shortcuts per interior road
10+
11+
The "unit" test in `backend/src/tests.rs` verifies this output doesn't change. When it does, we can manually load the savefile in the old and new web UI, check any differences, and manually approve them.
12+
13+
## Notes per test case
414

515
There are several stages of "working" for each of these:
616

0 commit comments

Comments
 (0)