@@ -521,29 +521,16 @@ def test_construct_geometries_line_node(self):
521521class TestFromGeoJSON (unittest .TestCase ):
522522 def setUp (self ):
523523 # Create valid test GeoJSON files
524- self .tempdir = TemporaryDirectory ()
525- self .nodes_path = os .path .join (self .tempdir .name , "nodes.geojson" )
526- self .edges_path = os .path .join (self .tempdir .name , "edges.geojson" )
524+ self .nodes_path = "test_nodes.geojson"
525+ self .edges_path = "test_edges.geojson"
527526
528- def tearDown (self ):
529- self .tempdir .cleanup ()
530-
531- def _write_geojson (self , node_data , edge_data ):
532- with open (self .nodes_path , "w" ) as f :
533- json .dump (node_data , f )
534-
535- with open (self .edges_path , "w" ) as f :
536- json .dump (edge_data , f )
537-
538-
539- def test_from_geojson_populates_graph (self ):
540- node_data = {
527+ self .node_data = {
541528 "type" : "FeatureCollection" ,
542529 "features" : [
543530 {
544531 "type" : "Feature" ,
545532 "geometry" : {"type" : "Point" , "coordinates" : [1 , 1 ]},
546- "properties" : {"_id" : "1" , "attribute" : "value1" , "ext:osm_id" : "11" },
533+ "properties" : {"_id" : "1" , "attribute" : "value1" },
547534 },
548535 {
549536 "type" : "Feature" ,
@@ -553,88 +540,45 @@ def test_from_geojson_populates_graph(self):
553540 ],
554541 }
555542
556- edge_data = {
543+ self . edge_data = {
557544 "type" : "FeatureCollection" ,
558545 "features" : [
559546 {
560547 "type" : "Feature" ,
561548 "geometry" : {"type" : "LineString" , "coordinates" : [[1 , 1 ], [2 , 2 ]]},
562- "properties" : {
563- "_id" : "5" ,
564- "_u_id" : "1" ,
565- "_v_id" : "2" ,
566- "attribute" : "edge_value" ,
567- "ext:osm_id" : "99" ,
568- },
549+ "properties" : {"_id" : "1" , "_u_id" : "1" , "_v_id" : "2" , "attribute" : "edge_value" },
569550 },
570551 ],
571552 }
572553
573- self ._write_geojson (node_data , edge_data )
574- osm_graph = OSMGraph .from_geojson (self .nodes_path , self .edges_path )
575- graph = osm_graph .get_graph ()
576-
577- self .assertEqual (set (graph .nodes ), {1 , 2 })
578- node_attrs = graph .nodes [1 ]
579- self .assertIsInstance (node_attrs ["geometry" ], Point )
580- self .assertEqual (node_attrs ["lon" ], 1 )
581- self .assertEqual (node_attrs ["lat" ], 1 )
582- self .assertEqual (node_attrs ["osm_id" ], 11 )
583-
584- edges = list (graph .edges (keys = True , data = True ))
585- self .assertEqual (len (edges ), 1 )
586- u , v , key , attrs = edges [0 ]
587- self .assertEqual ((u , v , key ), (1 , 2 , 5 ))
588- self .assertIsInstance (attrs ["geometry" ], LineString )
589- self .assertEqual (attrs ["osm_id" ], 99 )
590- self .assertEqual (attrs ["attribute" ], "edge_value" )
591-
592- def test_from_geojson_preserves_non_numeric_identifiers (self ):
593- node_data = {
594- "type" : "FeatureCollection" ,
595- "features" : [
596- {
597- "type" : "Feature" ,
598- "geometry" : {"type" : "Point" , "coordinates" : [3 , 3 ]},
599- "properties" : {"_id" : "p123" , "ext:osm_id" : "node-1" },
600- },
601- {
602- "type" : "Feature" ,
603- "geometry" : {"type" : "Point" , "coordinates" : [4 , 4 ]},
604- "properties" : {"_id" : "p456" },
605- },
606- ],
607- }
554+ # Write the data to files
555+ with open (self .nodes_path , "w" ) as f :
556+ json .dump (self .node_data , f )
608557
609- edge_data = {
610- "type" : "FeatureCollection" ,
611- "features" : [
612- {
613- "type" : "Feature" ,
614- "geometry" : {"type" : "LineString" , "coordinates" : [[3 , 3 ], [4 , 4 ]]},
615- "properties" : {
616- "_id" : "edge-7" ,
617- "_u_id" : "p123" ,
618- "_v_id" : "p456" ,
619- "ext:osm_id" : "edge-A" ,
620- },
621- },
622- ],
623- }
558+ with open (self .edges_path , "w" ) as f :
559+ json .dump (self .edge_data , f )
624560
625- self ._write_geojson (node_data , edge_data )
561+ def tearDown (self ):
562+ # Clean up files after tests
563+ import os
564+ if os .path .exists (self .nodes_path ):
565+ os .remove (self .nodes_path )
566+ if os .path .exists (self .edges_path ):
567+ os .remove (self .edges_path )
568+
569+ @patch ("src.osm_osw_reformatter.serializer.osm.osm_graph.OSMGraph.from_geojson" )
570+ def test_from_geojson (self , mock_from_geojson ):
571+ mock_graph = MagicMock ()
572+ mock_graph .get_graph .return_value .nodes = {"1" : {"geometry" : Point (1 , 1 )}}
573+ mock_graph .get_graph .return_value .edges = {("1" , "2" ): {"geometry" : LineString ([(1 , 1 ), (2 , 2 )])}}
574+ mock_from_geojson .return_value = mock_graph
626575
627576 osm_graph = OSMGraph .from_geojson (self .nodes_path , self .edges_path )
628- graph = osm_graph .get_graph ()
629577
630- self .assertIn ("p123" , graph .nodes )
631- self .assertEqual (graph .nodes ["p123" ]["osm_id" ], "node-1" )
632-
633- edges = list (graph .edges (keys = True , data = True ))
634- self .assertEqual (len (edges ), 1 )
635- u , v , key , attrs = edges [0 ]
636- self .assertEqual ((u , v , key ), ("p123" , "p456" , "edge-7" ))
637- self .assertEqual (attrs ["osm_id" ], "edge-A" )
578+ # Assertions
579+ self .assertIsNotNone (osm_graph , "OSMGraph object should not be None" )
580+ self .assertEqual (len (osm_graph .get_graph ().nodes ), 1 )
581+ self .assertEqual (len (osm_graph .get_graph ().edges ), 1 )
638582
639583 def test_tagged_node_parser_skips_non_osw_nodes (self ):
640584 graph = nx .MultiDiGraph ()
@@ -678,41 +622,6 @@ def test_tagged_node_parser_adds_osw_nodes(self):
678622 self .assertIn (123 , graph .nodes )
679623 self .assertEqual (graph .nodes [123 ]['barrier' ], 'kerb' )
680624
681- def test_to_geojson_skips_non_osw_nodes (self ):
682- graph = nx .MultiDiGraph ()
683- graph .add_node (
684- 5959268989 ,
685- geometry = Point (- 77.05091 , 38.8598812 ),
686- lon = - 77.05091 ,
687- lat = 38.8598812 ,
688- highway = 'crossing' ,
689- ** {
690- 'crossing:markings' : 'zebra' ,
691- 'tactile_paving' : 'yes' ,
692- }
693- )
694-
695- osm_graph = OSMGraph (G = graph )
696-
697- with TemporaryDirectory () as tmpdir :
698- nodes_path = os .path .join (tmpdir , 'nodes.geojson' )
699- edges_path = os .path .join (tmpdir , 'edges.geojson' )
700- points_path = os .path .join (tmpdir , 'points.geojson' )
701- lines_path = os .path .join (tmpdir , 'lines.geojson' )
702- zones_path = os .path .join (tmpdir , 'zones.geojson' )
703- polygons_path = os .path .join (tmpdir , 'polygons.geojson' )
704-
705- osm_graph .to_geojson (
706- nodes_path ,
707- edges_path ,
708- points_path ,
709- lines_path ,
710- zones_path ,
711- polygons_path ,
712- )
713-
714- self .assertFalse (os .path .exists (nodes_path ))
715-
716625 def test_to_geojson_node_ids_preserved (self ):
717626 graph = nx .MultiDiGraph ()
718627 graph .add_node (
0 commit comments