Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

test(kg): guard the node-embedding tests on gensim, keeping the absent-case test

dex0shubham29ff3c6f54e3
diff --git a/pyproject.toml b/pyproject.toml
index 2835f90f..6b058448 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -354,4 +354,5 @@ profile = "black"
testpaths = ["tests"]
markers = [
"integration: marks tests that require external services or API keys (deselect with '-m not integration')",
+ "no_gensim: marks tests that must still run when gensim is absent (e.g. they assert the missing-dependency behaviour)",
]
diff --git a/tests/kg/test_node_embeddings.py b/tests/kg/test_node_embeddings.py
index 07c27b36..362951c0 100644
--- a/tests/kg/test_node_embeddings.py
+++ b/tests/kg/test_node_embeddings.py
@@ -12,6 +12,23 @@ from unittest.mock import Mock, patch
from semantica.kg.node_embeddings import NodeEmbedder
[email protected](autouse=True)
+def require_gensim(request):
+ """Skip tests that need a working NodeEmbedder when gensim is absent.
+
+ gensim is the optional ``graph-embeddings`` extra and
+ ``NodeEmbedder.__init__`` raises ImportError without it, so these tests
+ fail instead of skipping on a core install.
+
+ Tests marked ``no_gensim`` keep running: one asserts that ImportError
+ itself, and skipping it on exactly the install where it applies would
+ remove the only coverage of the missing-dependency path. Same shape as
+ ``require_adk`` in tests/integrations/google_adk/.
+ """
+ if request.node.get_closest_marker("no_gensim") is None:
+ pytest.importorskip("gensim")
+
+
class TestNodeEmbedder:
"""Test cases for NodeEmbedder class."""
@@ -56,11 +73,16 @@ class TestNodeEmbedder:
assert embedder.p == 2.0
assert embedder.q == 0.5
+ @pytest.mark.no_gensim
def test_init_invalid_method(self):
- """Test NodeEmbedder initialization with invalid method."""
+ """Test NodeEmbedder initialization with invalid method.
+
+ The method check runs before the gensim check, so this needs no extra.
+ """
with pytest.raises(ValueError, match="Unsupported embedding method"):
NodeEmbedder(method="invalid_method")
+ @pytest.mark.no_gensim
@patch('semantica.kg.node_embeddings.GENSIM_AVAILABLE', False)
def test_init_gensim_unavailable(self):
"""Test NodeEmbedder initialization when gensim is unavailable."""