mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-09-05 04:00:31 +00:00
The Neo4j example in the persistent graph store section was passing a raw Neo4jStore straight into GraphBuilder. GraphBuilder calls add_nodes() and add_edges() on whatever it's given, and those only exist on the GraphStore facade, not on Neo4jStore itself. Anyone who copied the example got: AttributeError: 'Neo4jStore' object has no attribute 'add_nodes' Swapped the import and construction to GraphStore(backend="neo4j", uri=..., user=..., password=...), which wraps Neo4jStore internally and actually has the methods GraphBuilder needs. Added a test in tests/kg/test_graph_builder_with_graph_store.py that builds a small graph through GraphBuilder with a mocked GraphStore and checks add_nodes/add_edges get called. Also kept a test for GraphBuilder without a graph_store at all, so that path doesn't regress either. Closes #1135
98 lines
3.2 KiB
Python
98 lines
3.2 KiB
Python
"""
|
|
Test for GraphBuilder with GraphStore backend (Issue #1135).
|
|
|
|
This test verifies that GraphBuilder correctly works with the GraphStore
|
|
facade interface, not with raw backend stores like Neo4jStore.
|
|
"""
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
class TestGraphBuilderWithGraphStore(unittest.TestCase):
|
|
"""Test GraphBuilder integration with GraphStore facade."""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures."""
|
|
# Mock progress tracker
|
|
self.mock_tracker_patcher = patch("semantica.utils.progress_tracker.get_progress_tracker")
|
|
self.mock_get_tracker = self.mock_tracker_patcher.start()
|
|
self.mock_tracker = MagicMock()
|
|
self.mock_get_tracker.return_value = self.mock_tracker
|
|
|
|
def tearDown(self):
|
|
"""Clean up after tests."""
|
|
self.mock_tracker_patcher.stop()
|
|
|
|
def test_graph_builder_with_graph_store_facade(self):
|
|
"""Test that GraphBuilder works with GraphStore facade (Issue #1135)."""
|
|
from semantica.kg.graph_builder import GraphBuilder
|
|
from semantica.graph_store import GraphStore
|
|
|
|
# Create a mock GraphStore facade
|
|
mock_store = MagicMock(spec=GraphStore)
|
|
mock_store.add_nodes.return_value = 2
|
|
mock_store.add_edges.return_value = 1
|
|
|
|
# Create GraphBuilder with the GraphStore facade
|
|
builder = GraphBuilder(
|
|
merge_entities=False,
|
|
resolve_conflicts=False,
|
|
graph_store=mock_store
|
|
)
|
|
|
|
# Build a simple graph
|
|
entities = [
|
|
{"id": "alice", "type": "Person"},
|
|
{"id": "bob", "type": "Person"},
|
|
]
|
|
relationships = [
|
|
{"source": "alice", "target": "bob", "type": "knows"},
|
|
]
|
|
|
|
graph = builder.build({
|
|
"entities": entities,
|
|
"relationships": relationships
|
|
})
|
|
|
|
# Verify the graph was built
|
|
self.assertEqual(len(graph["entities"]), 2)
|
|
self.assertEqual(len(graph["relationships"]), 1)
|
|
|
|
# Verify that add_nodes and add_edges were called on the GraphStore
|
|
mock_store.add_nodes.assert_called_once()
|
|
mock_store.add_edges.assert_called_once()
|
|
|
|
def test_graph_builder_without_graph_store_still_works(self):
|
|
"""Test that GraphBuilder still works without a graph_store parameter."""
|
|
from semantica.kg.graph_builder import GraphBuilder
|
|
|
|
# Create GraphBuilder without graph_store
|
|
builder = GraphBuilder(
|
|
merge_entities=False,
|
|
resolve_conflicts=False
|
|
)
|
|
|
|
# Build a simple graph
|
|
entities = [
|
|
{"id": "alice", "type": "Person"},
|
|
{"id": "bob", "type": "Person"},
|
|
]
|
|
relationships = [
|
|
{"source": "alice", "target": "bob", "type": "knows"},
|
|
]
|
|
|
|
graph = builder.build({
|
|
"entities": entities,
|
|
"relationships": relationships
|
|
})
|
|
|
|
# Verify the graph was built
|
|
self.assertEqual(len(graph["entities"]), 2)
|
|
self.assertEqual(len(graph["relationships"]), 1)
|
|
self.assertEqual(graph["metadata"]["num_entities"], 2)
|
|
self.assertEqual(graph["metadata"]["num_relationships"], 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|