Files
semantica/tests/export/test_neo4j_csv_exporter.py
T
5579851208 fix(export): harden YAML export input handling (#958)
* refactor(export): centralize graph-payload key normalization

Graph payloads circulate under two vocabularies, entities/relationships and nodes/edges, and consumers each reconciled them locally with competing idioms. The same payload could be exported, silently dropped, or rejected depending on which consumer read it.

Add normalize_graph_payload() to utils.helpers as the single place that decision is made. Both spellings present with one empty resolves to the populated one, which is the shape JSONExporter emits; both non-empty and different is refused, since there is no basis to prefer either and picking one would silently discard the other; a non-empty mapping with no recognized key raises rather than returning empty collections, with require_recognized=False for callers that should degrade.

Adopt it in the three exporters that genuinely alias. LPGExporter read nodes with entities as the default, so it dropped every entity when nodes was present but empty, losing everything on a JSON round-trip. ArangoAQLExporter had the same idiom plus a manual fallback. Neo4jCSVExporter routes its mapping branch through the shared resolver so the reference implementation cannot drift; its attribute branch stays local, since objects are not mappings.

Also feed LPGExporter._generate_indexes the resolved entities. It read entities directly, so a nodes/edges payload produced no indexes even once node generation was fixed.

CSVExporter and JSONExporter are deliberately excluded: they write entities, relationships, nodes and edges as separate outputs by design rather than reconciling two spellings of one collection, so normalizing there would rename output files.

* fix(export): reject non-mapping input to the YAML exporters

export_yaml declared Union[Dict[str, Any], List[Dict[str, Any]]], but both
YAML exporters read their payload by key, so a list reached .get() and
surfaced as a bare AttributeError from inside the exporter, naming neither
the offending argument nor the shape expected.

Reject rather than wrap. These formats distinguish entities from
relationships from triplets, so inferring which collection a bare list
represents would silently mislabel the records, and wrapping it under an
unrecognised key would write a structurally valid file with every
collection empty - trading a loud failure for silent data loss.

Validate in the exporters, matching the existing precedent in
Neo4jCSVExporter._normalize_graph, so direct users of the classes get the
same contract as callers of the convenience wrapper. Narrow the wrapper
type hint to Dict[str, Any] to match.

* fix(export): address YAML exporter review findings

- semantica/export/yaml_exporter.py — import Sequence from typing
  instead of collections.abc. `Sequence[str]` in _require_mapping's
  annotation is evaluated at function-definition time; collections.abc.Sequence
  only became subscriptable in Python 3.9, so on the 3.8 this project
  declares support for, importing this module raised TypeError.
  typing.Sequence has supported subscripting since 3.5.3. Mapping stays
  imported from collections.abc since it's only used for isinstance.
- tests/export/test_yaml_exporter_input_validation.py — clean up each
  test's tempfile.mkdtemp() dir via addCleanup instead of leaking it,
  and read exported YAML through a context manager instead of an
  unclosed yaml.safe_load(open(...)).

* fix(export): reject YAML export payloads with no recognized key

Both YAML exporters built their output from a fixed set of `.get(key, [])`
lookups, so a mapping keyed by anything else serialized to a structurally
valid file with every collection empty. Nothing signalled the loss: no
exception, no warning, and the progress log reported a completed export.
The only way to notice was to open the file. The realistic trigger is
re-exporting an `export_json` payload, whose `{"data", "count", "metadata"}`
envelope drops every record.

- SemanticNetworkYAMLExporter.export_semantic_network now resolves its
  collections through normalize_graph_payload(), which raises rather than
  returning empty collections for an unrecognized mapping. Adopting the
  shared resolver rather than repeating the check locally also brings the
  'nodes'/'edges' aliases, so ContextGraph.to_dict() — the most direct path
  from this library's own graph type to YAML, used in
  examples/capability_gap_context_graphs_example.py — exports its records
  instead of an empty file.
- export_for_pipeline built its nested semantic network from the same
  defaulted lookups and had the same defect; it goes through the resolver
  too.
- YAMLSchemaExporter.export_ontology_schema gets the equivalent check over
  its own key set. Schemas are a separate vocabulary with no aliasing, so
  _require_recognized_keys lives in this module rather than in the shared
  graph resolver.
- 'metadata' is deliberately not sufficient to make a payload recognized.
  An export_json envelope carries one, so accepting it would readmit the
  case this fix is most likely to be needed for.
- An empty mapping is still exported: an empty graph is legitimate and has
  no records to lose.
- SemanticNetworkYAMLExporter.export() serializes before creating the
  output directory, so a rejected export leaves nothing behind.

The two rejections keep distinct exception types, following what the
codebase already does: a payload of the wrong *type* cannot be exported at
all and raises ProcessingError, matching Neo4jCSVExporter._normalize_graph;
a mapping whose *contents* are unusable raises ValidationError, matching
normalize_graph_payload. _require_mapping therefore runs first at every
entry point, so a non-mapping never reaches the resolver.

Docstring Raises sections, export_usage.md and docs/reference/export.md
record the accepted input shapes and both failures.

Closes #953.

* fix(export): reject payloads whose records resolve to nothing

Addresses the Qodo findings on #958.

Presence-only recognition (finding 1): checking that a recognized key is
present answered "did the caller use our vocabulary" when the question that
matters is "did anything the caller supplied survive". A payload like
{"entities": [], "data": [...records...]} cleared the check, resolved to
empty, and dropped every record under 'data' -- the silent-empty export by a
narrower route.

- utils/helpers.py — split the check in two. _require_recognized_keys keeps
  the presence rule; _require_nothing_dropped runs after resolution and
  refuses a payload that resolved to nothing while an unread key still holds
  records. Only a non-empty list counts as evidence: ContextGraph.to_dict()
  always carries a populated 'statistics' dict, and an empty graph must stay
  exportable, so 'metadata', 'statistics' and 'count' are named as context
  rather than records.
- export/yaml_exporter.py — the schema path had the same hole and now runs
  both checks through the shared helpers rather than its own copy, so the
  two vocabularies cannot drift apart in what counts as a silent-empty
  export.

Progress reported success on a failed write (finding 3): export_semantic_
network stops its tracking as completed once serialization returns, but
export() then creates the directory and writes the file. A failure there
left the tracker showing a completed export with no output.

- export/yaml_exporter.py — the serialization span now says it serialized,
  not that it exported, and export() opens its own span around the
  filesystem work that stops as failed on error. Nothing reports a completed
  export until the bytes are on disk.

Finding 2 (export_yaml no longer accepts List[Dict]) is the intended
resolution of #952 rather than a regression: wrapping a bare list under a
guessed key is what would mislabel the records. The signature, docstring and
PR description already record the narrowed contract.

Tests cover both directions of each fix, including that an empty
ContextGraph still exports and that a failing write is not reported as
completed.

* fix(export): validate collection values and make Neo4j mappings strict

Two gaps at the boundary the shared normalizer is supposed to own.

_resolve_collection() resolved on truthiness alone, so a recognized key
could still hold something that is not a collection of records:
{"entities": "abc"} normalized to three single-character "records", and
{"entities": 42} surfaced as a raw TypeError from list() inside whichever
exporter happened to read it, naming the exporter rather than the payload
key at fault. Collection values are now validated before conversion --
strings, bytes, mappings, and non-iterable scalars are rejected by key
name, and each element must be a mapping or an attribute-carrying object,
the two record shapes the exporters actually read. None stays legal as an
absent collection, the spelling a JSON round-trip produces for []; it
cannot hide dropped records, since _require_nothing_dropped() still runs.
Every spelling present is validated, not just the one that wins, so a
malformed alias is not excused by a well-formed canonical key.

Neo4jCSVExporter._normalize_graph() opted out of the recognized-key check
for mappings, which left it able to turn {"data": [...]} into header-only
CSVs indistinguishable from a genuinely empty graph -- the exact failure
the rest of the change exists to prevent. Mapping payloads now go through
normalize_graph_payload() on its default terms. The attribute path for
graph objects is untouched. With no caller left opting out, the
require_recognized flag is removed rather than kept as a way back into
the silent-empty export.

Regression tests cover the malformed values end to end through every
export path that reads the normalizer, and assert the rejected Neo4j
export writes no CSV files.

* fix(export): close YAML schema and record validation gaps

Fix 1 -- _require_usable_schema silent data loss (P1):
_require_usable_schema() passed all values from _SCHEMA_KEYS into
_require_nothing_dropped() as evidence that records survived.  Scalar
metadata fields such as version='1.0' and uri='http://...' are truthy
strings, so any one of them caused _require_nothing_dropped() to return
early and silently discard records stored under an unread key alongside
them (e.g. {'version': '1.0', 'nodes': [{'id': 'c1'}]}).  Fixed by
building the resolved list from only non-empty list/tuple values of
recognised schema keys.

Fix 2 -- _is_record accepts modules and type objects (P2):
_is_record() accepted any object with __dict__, which includes Python
modules and class objects.  Elements that passed _coerce_records then
reached exporters and raised AttributeError (e.g. module 'math' has no
attribute 'get') rather than a ValidationError at the validation
boundary.  Fixed by excluding types.ModuleType and type from the
__dict__ branch while preserving support for all user-defined
attribute-bearing record objects.

Tests: 101 tests pass across
  tests/utils/test_normalize_graph_payload.py
  tests/export/test_yaml_exporter_key_recognition.py
  tests/export/test_yaml_exporter_input_validation.py
  tests/export/test_neo4j_csv_exporter.py

* fix(export): close exception-type and record-shape gaps in normalize_graph_payload

LPGExporter and ArangoAQLExporter called normalize_graph_payload() with no
type guard, so non-mapping input raised ValidationError from inside the
resolver while the YAML and Neo4j exporters raised ProcessingError for the
identical mistake -- inconsistent with the exception-type contract this PR
establishes. Both now use the shared _require_mapping() guard (moved from
yaml_exporter.py into utils/helpers.py so all three can use it).

Neo4jCSVExporter._normalize_graph checked isinstance(graph, dict), so a
non-dict Mapping (MappingProxyType, ChainMap) fell through to the
object-attribute branch and was rejected, even though the identical payload
exported fine via the other three exporters. Now checks isinstance(graph,
Mapping).

normalize_graph_payload() accepts dataclass/attribute-bearing object
records, but LPGExporter/ArangoAQLExporter call .get(...) directly on
resolved entities -- an object-shaped record passed validation only to
crash with a raw AttributeError once used, the exact failure this
boundary exists to prevent. Records are now converted to plain dicts at
the boundary (_coerce_records -> new _record_to_dict), so every consumer
gets a uniform shape regardless of which reading the caller used.

Two non-empty spellings of the same collection holding identical records
in a different order were rejected as conflicting, since the check used
plain list equality. Comparison is now an order-independent multiset of
each record's canonical JSON form.

* docs(changelog): add entry for #958 YAML export input hardening

Documents the full arc of #958 -- the normalize_graph_payload()
centralization, YAML input validation, both review rounds from
@Sameer6305, and the exception-type/record-shape follow-up fixes -- plus
closes #956, #952, #953.

---------

Co-authored-by: Pravit Ampapathini <pravitampapathini@Pravits-MacBook-Air-3.local>
Co-authored-by: Sameer Kadam <sskadam6305@gmail.com>
Co-authored-by: KaifAhmad1 <kaifahmad087@gmail.com>
2026-08-15 22:10:04 +05:30

361 lines
11 KiB
Python

"""Tests for Neo4j bulk CSV export."""
import csv
from pathlib import Path
import pytest
from semantica.export import Neo4jCSVExporter
from semantica.export.methods import export_knowledge_graph, export_neo4j_csv
from semantica.kg.knowledge_graph import KnowledgeGraph
from semantica.utils.exceptions import ValidationError
def _read_csv(path: Path):
with open(path, "r", encoding="utf-8", newline="") as handle:
return list(csv.reader(handle))
def _sample_graph():
return {
"entities": [
{
"id": "2",
"labels": ["Person", "Engineer"],
"name": "Alice",
"type": "user",
"properties": {
"age": 30,
"email": "alice@example.com",
},
},
{
"id": "1",
"type": "Company",
"name": "Acme, Inc.",
"properties": {"founded": 2024},
},
{
"text": "No ID concept",
"type": "Concept",
"properties": {
"empty": "",
"quote": 'He said "hello"',
"unicode": "東京",
},
},
],
"relationships": [
{
"source": "2",
"target": "1",
"type": "WORKS_FOR",
"properties": {"role": "Engineer", "since": 2024},
},
{
"source": "Alice",
"target": "Acme, Inc.",
"relationship_type": "KNOWS",
"properties": {"note": 'line1\nline2, "quoted"'},
},
],
}
def test_header_correctness_and_node_csv_structure(tmp_path):
exporter = Neo4jCSVExporter()
exporter.export_knowledge_graph(_sample_graph(), tmp_path)
rows = _read_csv(tmp_path / "nodes.csv")
assert rows[0] == [
":id",
":LABEL",
"age",
"email",
"empty",
"founded",
"name",
"quote",
"text",
"type",
"unicode",
]
by_id = {row[0]: row for row in rows[1:]}
assert list(by_id) == sorted(by_id)
assert by_id["2"][1] == "Person;Engineer"
assert by_id["2"][2] == "30"
assert by_id["2"][6] == "Alice"
assert by_id["2"][9] == "user"
assert by_id["1"][1] == "Company"
assert by_id["1"][2] == ""
assert by_id["1"][5] == "2024"
derived_ids = [node_id for node_id in by_id if node_id.startswith("n_")]
assert len(derived_ids) == 1
derived_row = by_id[derived_ids[0]]
assert derived_row[4] == ""
assert derived_row[7] == 'He said "hello"'
assert derived_row[8] == "No ID concept"
assert derived_row[10] == "東京"
def test_relationship_csv_structure_and_properties(tmp_path):
exporter = Neo4jCSVExporter()
exporter.export(_sample_graph(), tmp_path)
rows = _read_csv(tmp_path / "relationships.csv")
assert rows[0] == [":START_ID", ":END_ID", ":TYPE", "note", "role", "since"]
assert rows[1] == ["2", "1", "KNOWS", 'line1\nline2, "quoted"', "", ""]
assert rows[2] == ["2", "1", "WORKS_FOR", "", "Engineer", "2024"]
def test_missing_properties_create_empty_cells(tmp_path):
exporter = Neo4jCSVExporter()
exporter.export(
{
"nodes": [
{"id": "a", "type": "Thing", "properties": {"optional": "present"}},
{"id": "b", "type": "Thing"},
],
"edges": [{"source": "a", "target": "b", "type": "RELATED"}],
},
tmp_path,
)
rows = _read_csv(tmp_path / "nodes.csv")
optional_index = rows[0].index("optional")
by_id = {row[0]: row for row in rows[1:]}
assert by_id["a"][optional_index] == "present"
assert by_id["b"][optional_index] == ""
def test_deterministic_output_with_permuted_graph(tmp_path):
graph_a = {
"entities": [
{"name": "Bob", "type": "Person"},
{"name": "Alice", "type": "Person"},
],
"relationships": [
{"source": "Alice", "target": "Bob", "type": "KNOWS"},
],
}
graph_b = {
"entities": list(reversed(graph_a["entities"])),
"relationships": list(reversed(graph_a["relationships"])),
}
exporter = Neo4jCSVExporter()
out_a = tmp_path / "a"
out_b = tmp_path / "b"
exporter.export(graph_a, out_a)
exporter.export(graph_b, out_b)
assert (out_a / "nodes.csv").read_text(encoding="utf-8") == (
out_b / "nodes.csv"
).read_text(encoding="utf-8")
assert (out_a / "relationships.csv").read_text(encoding="utf-8") == (
out_b / "relationships.csv"
).read_text(encoding="utf-8")
def test_csv_quoting_escaping_and_unicode(tmp_path):
exporter = Neo4jCSVExporter()
exporter.export(_sample_graph(), tmp_path)
nodes_text = (tmp_path / "nodes.csv").read_text(encoding="utf-8")
relationships_text = (tmp_path / "relationships.csv").read_text(encoding="utf-8")
assert '"Acme, Inc."' in nodes_text
assert '"He said ""hello"""' in nodes_text
assert "東京" in nodes_text
assert '"line1\nline2, ""quoted"""' in relationships_text
assert _read_csv(tmp_path / "nodes.csv")
assert _read_csv(tmp_path / "relationships.csv")
def test_empty_graph_export_writes_importable_headers(tmp_path):
exporter = Neo4jCSVExporter()
exporter.export({"entities": [], "relationships": []}, tmp_path)
assert _read_csv(tmp_path / "nodes.csv") == [[":id", ":LABEL"]]
assert _read_csv(tmp_path / "relationships.csv") == [
[":START_ID", ":END_ID", ":TYPE"]
]
validation = exporter.validate_export(tmp_path)
assert validation == {"valid": True, "errors": []}
def test_dry_run_and_written_validation(tmp_path):
exporter = Neo4jCSVExporter()
summary = exporter.dry_run(_sample_graph())
assert summary["valid"] is True
assert summary["node_count"] == 3
assert summary["relationship_count"] == 2
assert summary["node_header"][:2] == [":id", ":LABEL"]
assert summary["relationship_header"][:3] == [
":START_ID",
":END_ID",
":TYPE",
]
exporter.export(_sample_graph(), tmp_path, validate=True)
assert exporter.validate_export(tmp_path)["valid"] is True
def test_knowledge_graph_dataclass_and_convenience_wrappers(tmp_path):
kg = KnowledgeGraph(
entities=[
{"id": "a", "type": "Person", "name": "Alice"},
{"id": "b", "type": "Person", "name": "Bob"},
],
relationships=[{"source": "a", "target": "b", "type": "KNOWS"}],
)
result = export_neo4j_csv(kg, tmp_path / "direct")
assert result["nodes"].name == "nodes.csv"
assert result["relationships"].name == "relationships.csv"
assert result["nodes"].exists()
export_knowledge_graph(kg, tmp_path / "unified", format="neo4j_csv")
assert (tmp_path / "unified" / "nodes.csv").exists()
assert (tmp_path / "unified" / "relationships.csv").exists()
def test_invalid_relationship_endpoint_fails_strict_validation(tmp_path):
exporter = Neo4jCSVExporter()
with pytest.raises(ValidationError):
exporter.export(
{
"entities": [{"id": "a", "type": "Thing"}],
"relationships": [
{"source": "a", "target": "missing", "type": "RELATED"}
],
},
tmp_path,
)
def test_ambiguous_aliases_are_not_resolved(tmp_path):
exporter = Neo4jCSVExporter()
# Create two nodes with the same alias/name "Alice"
graph = {
"entities": [
{"id": "node1", "name": "Alice", "type": "Person"},
{"id": "node2", "name": "Alice", "type": "Person"},
],
"relationships": [{"source": "Alice", "target": "node1", "type": "KNOWS"}],
}
# Verify that strict export raises ValidationError because the alias is ambiguous
with pytest.raises(ValidationError):
exporter.export(graph, tmp_path)
def test_duplicate_node_ids_fail_validation(tmp_path):
exporter = Neo4jCSVExporter()
# Create two nodes with the same explicit ID
graph = {
"entities": [
{"id": "node1", "name": "Alice", "type": "Person"},
{"id": "node1", "name": "Bob", "type": "Person"},
],
"relationships": [],
}
# Verify duplicate explicit node IDs raise ValidationError
with pytest.raises(ValidationError):
exporter.export(graph, tmp_path)
def test_nested_properties_are_json_serialized(tmp_path):
exporter = Neo4jCSVExporter()
graph = {
"entities": [
{
"id": "node1",
"type": "Person",
"properties": {"nested_dict": {"k": "v"}, "nested_list": [1, 2, 3]},
}
],
"relationships": [],
}
exporter.export(graph, tmp_path)
rows = _read_csv(tmp_path / "nodes.csv")
assert rows[0] == [":id", ":LABEL", "nested_dict", "nested_list", "type"]
# Verify that the nested properties are serialized as deterministic JSON strings
by_id = {row[0]: row for row in rows[1:]}
assert by_id["node1"][2] == '{"k":"v"}'
assert by_id["node1"][3] == "[1,2,3]"
def test_unrecognized_mapping_is_refused_rather_than_exported_empty(tmp_path):
"""The Neo4j path reads mappings on the shared normalizer's default terms.
An ``export_json`` envelope names no graph key, so it resolves to nothing.
Written out, that is a pair of header-only CSVs indistinguishable from a
genuinely empty graph -- the silent-empty export the shared contract
exists to prevent.
"""
exporter = Neo4jCSVExporter()
with pytest.raises(ValidationError) as excinfo:
exporter.export({"data": [{"id": "e1"}]}, tmp_path)
message = str(excinfo.value)
assert "data" in message
assert "entities" in message
assert not (tmp_path / "nodes.csv").exists()
assert not (tmp_path / "relationships.csv").exists()
def test_records_under_an_unread_key_are_not_dropped_silently(tmp_path):
"""Naming a recognized key is not enough if nothing resolves from it."""
exporter = Neo4jCSVExporter()
with pytest.raises(ValidationError):
exporter.export({"nodes": [], "data": [{"id": "e1"}]}, tmp_path)
assert not (tmp_path / "nodes.csv").exists()
def test_malformed_collection_value_is_refused(tmp_path):
"""``list("abc")`` would otherwise export one node per character."""
exporter = Neo4jCSVExporter()
for value in ("abc", 42, {"id": "n1"}):
with pytest.raises(ValidationError) as excinfo:
exporter.export({"nodes": value}, tmp_path)
assert "nodes" in str(excinfo.value)
assert not (tmp_path / "nodes.csv").exists()
def test_graph_objects_still_use_the_attribute_path(tmp_path):
"""Only mappings changed; objects are not mappings and are unaffected."""
class Graph:
def __init__(self):
self.nodes = [{"id": "e1", "type": "Person", "name": "Acme"}]
self.edges = []
exporter = Neo4jCSVExporter()
exporter.export(Graph(), tmp_path)
assert "Acme" in (tmp_path / "nodes.csv").read_text(encoding="utf-8")