From 79bbc84fc9d5a790c87c48168c6f90c378dd9207 Mon Sep 17 00:00:00 2001 From: Andreas Gemsa Date: Wed, 20 May 2026 20:00:41 +0200 Subject: [PATCH] Make test suite resilient to missing networkx; fix OSMWay default networkx is an optional dependency, but two tests failed outright when it was not installed: - convert_graph_test: importing/calling convert_to_networkx raised NameError. The test class is now skipped via @skipUnless when networkx is unavailable. - integration_test: run.py was always invoked with --networkx, which exits non-zero without networkx. --networkx is now only passed when networkx is installed, so the core pipeline is still exercised. Also give OSMWay.max_speed_int a default of 0. It was declared with field(init=False) and no default, so accessing it on a way that was never accepted (max_speed_int never assigned) raised AttributeError. Test suite now passes cleanly with or without networkx installed. Co-Authored-By: Claude Opus 4.7 (1M context) --- osm/osm_types.py | 2 +- tests/convert_graph_test.py | 4 ++++ tests/integration_test.py | 21 +++++++++------------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/osm/osm_types.py b/osm/osm_types.py index a8ad076..399abba 100644 --- a/osm/osm_types.py +++ b/osm/osm_types.py @@ -9,7 +9,7 @@ class OSMWay: highway: str = field(init=False, default="") area: Optional[str] = field(init=False, default=None) max_speed_str: Optional[str] = field(init=False, default=None) - max_speed_int: int = field(init=False) + max_speed_int: int = field(init=False, default=0) direction: str = field(init=False, default="") forward: bool = field(init=False, default=True) backward: bool = field(init=False, default=True) diff --git a/tests/convert_graph_test.py b/tests/convert_graph_test.py index d12c88c..6ab204a 100644 --- a/tests/convert_graph_test.py +++ b/tests/convert_graph_test.py @@ -1,3 +1,4 @@ +import importlib.util import random import string import unittest @@ -6,6 +7,8 @@ from graph.graph import Graph from graph.graph_types import Edge, EdgeData, Vertex, VertexData +networkx_available = importlib.util.find_spec("networkx") is not None + def random_string(length=8): return "".join(random.choice(string.ascii_lowercase) for i in range(length)) @@ -15,6 +18,7 @@ def random_uint(max_val=1000): return random.randint(1, max_val) +@unittest.skipUnless(networkx_available, "networkx is an optional dependency and is not installed") class TestConvertGraphTest(unittest.TestCase): def test_converting_K4_graph(self): g = self.create_graph(number_nodes=4) diff --git a/tests/integration_test.py b/tests/integration_test.py index 36152e5..e06b559 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -1,6 +1,9 @@ +import importlib.util import unittest from subprocess import call +networkx_available = importlib.util.find_spec("networkx") is not None + class IntegrationTest(unittest.TestCase): def test_python(self): @@ -21,18 +24,12 @@ def pedestrian_graph_contracted_ok(self): self.assertEqual(nmb_edges, 2039) def _execute_program(self): - returncode = call( - [ - "python3", - "run.py", - "-f", - "data/karlsruhe_small.osm", - "-n", - "p", - "-c", - "--networkx", - ] - ) + args = ["python3", "run.py", "-f", "data/karlsruhe_small.osm", "-n", "p", "-c"] + # --networkx requires the optional networkx dependency; only exercise + # it when networkx is actually installed + if networkx_available: + args.append("--networkx") + returncode = call(args) self.assertEqual(0, returncode) def _get_nmb_nodes_edges(self, path):