|
1 | 1 | import os |
2 | 2 | import re |
| 3 | +import json |
3 | 4 | import asyncio |
4 | 5 | import unittest |
5 | 6 | from src.osm_osw_reformatter.osm2osw.osm2osw import OSM2OSW |
@@ -69,6 +70,120 @@ async def mock_count_entities_error(osm_file_path, counter_cls): |
69 | 70 | result = await osm2osw.convert() |
70 | 71 | self.assertFalse(result.status) |
71 | 72 |
|
| 73 | + def test_ext_tags_present_in_output(self): |
| 74 | + osm_file_path = TEST_FILE |
| 75 | + |
| 76 | + async def run_test(): |
| 77 | + osm2osw = OSM2OSW(osm_file=osm_file_path, workdir=OUTPUT_DIR, prefix='test') |
| 78 | + result = await osm2osw.convert() |
| 79 | + self.assertTrue(result.status) |
| 80 | + |
| 81 | + has_ext_tag = False |
| 82 | + for file_path in result.generated_files: |
| 83 | + if file_path.endswith('.geojson'): |
| 84 | + with open(file_path) as f: |
| 85 | + geojson = json.load(f) |
| 86 | + for feature in geojson.get('features', []): |
| 87 | + props = feature.get('properties', {}) |
| 88 | + if any(k.startswith("ext:") for k in props): |
| 89 | + has_ext_tag = True |
| 90 | + break |
| 91 | + if has_ext_tag: |
| 92 | + break |
| 93 | + |
| 94 | + self.assertTrue(has_ext_tag, "No ext: tags found in generated GeoJSON features") |
| 95 | + |
| 96 | + for file_path in result.generated_files: |
| 97 | + os.remove(file_path) |
| 98 | + |
| 99 | + asyncio.run(run_test()) |
| 100 | + |
| 101 | + def test_nodes_file_has_point_geometry(self): |
| 102 | + osm_file_path = TEST_FILE |
| 103 | + |
| 104 | + async def run_test(): |
| 105 | + osm2osw = OSM2OSW(osm_file=osm_file_path, workdir=OUTPUT_DIR, prefix='test') |
| 106 | + result = await osm2osw.convert() |
| 107 | + self.assertTrue(result.status) |
| 108 | + |
| 109 | + for file_path in result.generated_files: |
| 110 | + if "nodes" in file_path: |
| 111 | + with open(file_path) as f: |
| 112 | + geojson = json.load(f) |
| 113 | + for feature in geojson["features"]: |
| 114 | + self.assertEqual(feature["geometry"]["type"], "Point") |
| 115 | + break |
| 116 | + |
| 117 | + for file_path in result.generated_files: |
| 118 | + os.remove(file_path) |
| 119 | + |
| 120 | + asyncio.run(run_test()) |
| 121 | + |
| 122 | + def test_all_feature_ids_are_strings(self): |
| 123 | + osm_file_path = TEST_FILE |
| 124 | + |
| 125 | + async def run_test(): |
| 126 | + osm2osw = OSM2OSW(osm_file=osm_file_path, workdir=OUTPUT_DIR, prefix='test') |
| 127 | + result = await osm2osw.convert() |
| 128 | + self.assertTrue(result.status) |
| 129 | + |
| 130 | + for file_path in result.generated_files: |
| 131 | + with open(file_path) as f: |
| 132 | + geojson = json.load(f) |
| 133 | + for feature in geojson.get("features", []): |
| 134 | + self.assertIn("_id", feature["properties"]) |
| 135 | + self.assertIsInstance(feature["properties"]["_id"], str) |
| 136 | + |
| 137 | + for file_path in result.generated_files: |
| 138 | + os.remove(file_path) |
| 139 | + |
| 140 | + asyncio.run(run_test()) |
| 141 | + |
| 142 | + |
| 143 | + def test_no_empty_features(self): |
| 144 | + osm_file_path = TEST_FILE |
| 145 | + |
| 146 | + async def run_test(): |
| 147 | + osm2osw = OSM2OSW(osm_file=osm_file_path, workdir=OUTPUT_DIR, prefix='test') |
| 148 | + result = await osm2osw.convert() |
| 149 | + self.assertTrue(result.status) |
| 150 | + |
| 151 | + for file_path in result.generated_files: |
| 152 | + with open(file_path) as f: |
| 153 | + geojson = json.load(f) |
| 154 | + for feature in geojson.get("features", []): |
| 155 | + self.assertIn("geometry", feature) |
| 156 | + self.assertIsNotNone(feature["geometry"]) |
| 157 | + self.assertIn("type", feature["geometry"]) |
| 158 | + self.assertIn("coordinates", feature["geometry"]) |
| 159 | + |
| 160 | + for file_path in result.generated_files: |
| 161 | + os.remove(file_path) |
| 162 | + |
| 163 | + asyncio.run(run_test()) |
| 164 | + |
| 165 | + def test_no_duplicate_ids_in_file(self): |
| 166 | + osm_file_path = TEST_FILE |
| 167 | + |
| 168 | + async def run_test(): |
| 169 | + osm2osw = OSM2OSW(osm_file=osm_file_path, workdir=OUTPUT_DIR, prefix='test') |
| 170 | + result = await osm2osw.convert() |
| 171 | + self.assertTrue(result.status) |
| 172 | + |
| 173 | + for file_path in result.generated_files: |
| 174 | + with open(file_path) as f: |
| 175 | + geojson = json.load(f) |
| 176 | + seen_ids = set() |
| 177 | + for feature in geojson.get("features", []): |
| 178 | + _id = feature["properties"].get("_id") |
| 179 | + self.assertNotIn(_id, seen_ids, f"Duplicate _id: {_id} in {file_path}") |
| 180 | + seen_ids.add(_id) |
| 181 | + |
| 182 | + for file_path in result.generated_files: |
| 183 | + os.remove(file_path) |
| 184 | + |
| 185 | + asyncio.run(run_test()) |
| 186 | + |
72 | 187 |
|
73 | 188 | if __name__ == '__main__': |
74 | 189 | unittest.main() |
0 commit comments