test(visualization): isolate optional dependency mocks (#897)

* test(visualization): isolate optional dependency mocks

* test(visualization): stop requiring Plotly in unit tests

Removing the global sys.modules stubs left the tests that patch
`...go.Bar`, or call a visualizer, with nothing standing in for the
module level `px` and `go` aliases. Those are None when Plotly is
missing, so patch resolution and _check_dependencies() both failed.

Add a helper that substitutes a double only for the aliases that are
None, leaving the real module in place when Plotly is installed.

---------
This commit is contained in:
Luan Taraschi
2026-08-20 17:58:20 +05:00
committed by GitHub
parent 54c274e02c
commit c5d382ee81
7 changed files with 187 additions and 245 deletions
+1 -23
View File
@@ -1,27 +1,5 @@
import unittest
from unittest.mock import MagicMock, patch, ANY
import sys
import types
# Helper to create a mock package
def mock_package(name):
m = MagicMock()
m.__path__ = []
sys.modules[name] = m
return m
# Mock libraries before importing module under test
# We need to ensure matplotlib behaves like a package for seaborn
sys.modules['matplotlib'] = MagicMock()
sys.modules['matplotlib.colors'] = MagicMock()
sys.modules['matplotlib.pyplot'] = MagicMock()
sys.modules['matplotlib.patches'] = MagicMock()
sys.modules['plotly'] = MagicMock()
sys.modules['plotly.express'] = MagicMock()
sys.modules['plotly.graph_objects'] = MagicMock()
sys.modules['plotly.subplots'] = MagicMock()
sys.modules['graphviz'] = MagicMock()
sys.modules['seaborn'] = MagicMock()
from unittest.mock import MagicMock, patch
from semantica.visualization.kg_visualizer import KGVisualizer
from semantica.visualization.ontology_visualizer import OntologyVisualizer