Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(export): quote LPG node id and name like other Cypher values

kartsan0399c7c4a4aa29
diff --git a/semantica/export/lpg_exporter.py b/semantica/export/lpg_exporter.py
index 3ec9ac19..0a30608a 100644
--- a/semantica/export/lpg_exporter.py
+++ b/semantica/export/lpg_exporter.py
@@ -245,12 +245,11 @@ class LPGExporter:
node_type = node.get("type") or node.get("entity_type", "Entity")
label = node.get("label") or node.get("name") or node.get("text", "")
- # Escape special characters
- node_id_escaped = self._escape_cypher_string(str(node_id))
- label_escaped = self._escape_cypher_string(str(label))
-
# Build properties
- properties = {"id": node_id_escaped, "name": label_escaped}
+ properties = {
+ "id": self._format_cypher_value(node_id),
+ "name": self._format_cypher_value(str(label)),
+ }
# Add other properties
for key, value in node.items():
@@ -315,8 +314,8 @@ class LPGExporter:
continue
# Escape special characters
- source_escaped = self._escape_cypher_string(str(source_id))
- target_escaped = self._escape_cypher_string(str(target_id))
+ source_escaped = self._format_cypher_value(source_id)
+ target_escaped = self._format_cypher_value(target_id)
rel_type_escaped = rel_type.upper().replace(" ", "_")
# Build properties
@@ -340,7 +339,7 @@ class LPGExporter:
# Create relationship
lines.append(
- f"MATCH (a {{id: '{source_escaped}'}}), (b {{id: '{target_escaped}'}}) "
+ f"MATCH (a {{id: {source_escaped}}}), (b {{id: {target_escaped}}}) "
f"CREATE (a)-[r:{rel_type_escaped}{props_str}]->(b);"
)
diff --git a/tests/export/test_lpg_exporter_endpoint_names.py b/tests/export/test_lpg_exporter_endpoint_names.py
index 05e097ad..bf228dab 100644
--- a/tests/export/test_lpg_exporter_endpoint_names.py
+++ b/tests/export/test_lpg_exporter_endpoint_names.py
@@ -78,7 +78,7 @@ def test_export_retains_zero_endpoint_ids(tmp_path, source, target):
text = path.read_text(encoding="utf-8")
assert (
- f"MATCH (a {{id: '{source}'}}), (b {{id: '{target}'}}) "
+ f"MATCH (a {{id: {source}}}), (b {{id: {target}}}) "
"CREATE (a)-[r:RELATED_TO]->(b);"
) in text
# A node with id 0 keeps its id instead of a generated node_<n> placeholder.
diff --git a/tests/export/test_lpg_exporter_values.py b/tests/export/test_lpg_exporter_values.py
new file mode 100644
index 00000000..698de31e
--- /dev/null
+++ b/tests/export/test_lpg_exporter_values.py
@@ -0,0 +1,38 @@
+"""Node properties and relationship matches must use the same Cypher literals."""
+
+import pytest
+
+from semantica.export import LPGExporter
+
+
[email protected]("batch_size", [1, 1000])
+def test_export_formats_node_values_and_endpoint_ids(tmp_path, batch_size):
+ path = tmp_path / "graph.cypher"
+ LPGExporter(include_indexes=False, batch_size=batch_size).export(
+ {
+ "nodes": [
+ {"id": "alice", "name": "Alice"},
+ {"id": 7, "name": "Seven"},
+ {"id": "7"},
+ ],
+ "edges": [
+ {"source": "alice", "target": 7, "id": "r1"},
+ {"source": 7, "target": "7", "id": 2},
+ ],
+ },
+ path,
+ )
+
+ statements = [
+ line
+ for line in path.read_text(encoding="utf-8").splitlines()
+ if line.startswith(("CREATE (", "MATCH ("))
+ ]
+ assert statements == [
+ "CREATE (n0:Entity {id: 'alice', name: 'Alice'});",
+ f"CREATE (n{1 if batch_size > 1 else 0}:Entity {{id: 7, name: 'Seven'}});",
+ f"CREATE (n{2 if batch_size > 1 else 0}:Entity {{id: '7', name: ''}});",
+ "MATCH (a {id: 'alice'}), (b {id: 7}) "
+ "CREATE (a)-[r:RELATED_TO {id: 'r1'}]->(b);",
+ "MATCH (a {id: 7}), (b {id: '7'}) CREATE (a)-[r:RELATED_TO {id: 2}]->(b);",
+ ]