|
| 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 | +} |
0 commit comments