|
1 | 1 | import os |
| 2 | +import zipfile |
2 | 3 | import unittest |
3 | 4 | from src.osm_osw_reformatter.osw2osm.osw2osm import OSW2OSM |
4 | 5 | import xml.etree.ElementTree as ET |
|
8 | 9 | TEST_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/osw.zip') |
9 | 10 | TEST_WIDTH_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/width-test.zip') |
10 | 11 | TEST_DATA_WITH_INCLINE_ZIP_FILE = os.path.join(ROOT_DIR, 'test_files/dataset_with_incline.zip') |
| 12 | +TEST_EDGES_WITH_INVALID_INCLINE_FILE = os.path.join(ROOT_DIR, 'test_files/edges_invalid_incline.geojson') |
| 13 | +TEST_NODES_WITH_INVALID_INCLINE_FILE = os.path.join(ROOT_DIR, 'test_files/nodes_invalid_incline.geojson') |
| 14 | + |
| 15 | + |
| 16 | +def _create_invalid_incline_zip(zip_path: str) -> str: |
| 17 | + """Create a temporary OSW dataset with invalid incline values. |
| 18 | +
|
| 19 | + The resulting ZIP mirrors the structure expected by ``OSW2OSM`` and contains |
| 20 | + both ``edges.geojson`` and ``nodes.geojson`` files. |
| 21 | +
|
| 22 | + Args: |
| 23 | + zip_path: Location where the archive will be written. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + The path to the generated ZIP archive. |
| 27 | + """ |
| 28 | + os.makedirs(os.path.dirname(zip_path), exist_ok=True) |
| 29 | + with zipfile.ZipFile(zip_path, 'w') as zf: |
| 30 | + zf.write(TEST_EDGES_WITH_INVALID_INCLINE_FILE, arcname='edges.geojson') |
| 31 | + zf.write(TEST_NODES_WITH_INVALID_INCLINE_FILE, arcname='nodes.geojson') |
| 32 | + return zip_path |
11 | 33 |
|
12 | 34 |
|
13 | 35 | class TestOSW2OSM(unittest.IsolatedAsyncioTestCase): |
@@ -105,6 +127,31 @@ def test_incline_tags_do_not_have_climb(self): |
105 | 127 |
|
106 | 128 | os.remove(result.generated_files) |
107 | 129 |
|
| 130 | + def test_invalid_incline_values_are_excluded(self): |
| 131 | + zip_path = os.path.join(OUTPUT_DIR, 'dataset_with_invalid_incline.zip') |
| 132 | + zip_file = _create_invalid_incline_zip(zip_path) |
| 133 | + osw2osm = OSW2OSM(zip_file_path=zip_file, workdir=OUTPUT_DIR, prefix='invalid') |
| 134 | + result = osw2osm.convert() |
| 135 | + |
| 136 | + # Ensure conversion succeeded so the XML file path is valid |
| 137 | + self.assertTrue(result.status, msg=getattr(result, 'error', 'Conversion failed')) |
| 138 | + xml_file_path = result.generated_files |
| 139 | + |
| 140 | + tree = ET.parse(xml_file_path) |
| 141 | + root = tree.getroot() |
| 142 | + |
| 143 | + for way in root.findall('.//way'): |
| 144 | + tags = {tag.get('k'): tag.get('v') for tag in way.findall('tag')} |
| 145 | + if tags.get('_id') == '2': |
| 146 | + self.assertNotIn('incline', tags) |
| 147 | + if tags.get('_id') == '1': |
| 148 | + self.assertEqual(tags.get('incline'), '0.1') |
| 149 | + |
| 150 | + self.assertEqual(len(root.findall(".//tag[@k='incline']")), 1) |
| 151 | + |
| 152 | + os.remove(result.generated_files) |
| 153 | + os.remove(zip_file) |
| 154 | + |
108 | 155 |
|
109 | 156 | if __name__ == '__main__': |
110 | 157 | unittest.main() |
0 commit comments