Skip to content

Commit 433973c

Browse files
Fix unit tests
1 parent c17631f commit 433973c

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

src/neo4j_graphrag/experimental/components/schema.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@ class NodeType(BaseModel):
119119
@classmethod
120120
def validate_input_if_string(cls, data: EntityInputType) -> EntityInputType:
121121
if isinstance(data, str):
122+
logger.info(
123+
f"Converting string '{data}' to NodeType with default 'name' property "
124+
f"and additional_properties=True to allow flexible property extraction."
125+
)
122126
return {
123127
"label": data,
124128
# added to satisfy the model validation (min_length=1 for properties of node types)
125129
"properties": [{"name": "name", "type": "STRING"}],
130+
# allow LLM to extract additional properties beyond the default "name"
131+
"additional_properties": True, # type: ignore[dict-item]
126132
}
127133
return data
128134

@@ -741,7 +747,18 @@ def _filter_items_without_labels(
741747
for item in items:
742748
if isinstance(item, str):
743749
if item and " " not in item and not item.startswith("{"):
744-
filtered_items.append({"label": item})
750+
# Add default property for node types to satisfy min_length=1 constraint
751+
# This matches the behavior of NodeType.validate_input_if_string
752+
if item_type == "node type":
753+
filtered_items.append(
754+
{
755+
"label": item,
756+
"properties": [{"name": "name", "type": "STRING"}],
757+
"additional_properties": True,
758+
}
759+
)
760+
else:
761+
filtered_items.append({"label": item})
745762
elif item:
746763
logging.info(
747764
f"Filtering out {item_type} with invalid label: {item}"

tests/unit/experimental/components/test_schema.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ def test_relationship_type_additional_properties_default() -> None:
125125
assert relationship_type.additional_properties is False
126126

127127
# manually changing the default value
128-
# impossible cases: no properties and no additional
129-
with pytest.raises(ValidationError):
130-
RelationshipType.model_validate(
131-
{"label": "REL", "additional_properties": False}
132-
)
133-
with pytest.raises(ValidationError):
134-
RelationshipType.model_validate(
135-
{"label": "REL", "properties": [], "additional_properties": False}
136-
)
128+
# auto-correction: no properties and additional_properties=False -> auto-corrected to True
129+
relationship_type = RelationshipType.model_validate(
130+
{"label": "REL", "additional_properties": False}
131+
)
132+
assert relationship_type.additional_properties is True # auto-corrected
133+
relationship_type = RelationshipType.model_validate(
134+
{"label": "REL", "properties": [], "additional_properties": False}
135+
)
136+
assert relationship_type.additional_properties is True # auto-corrected
137137

138138
# working case: properties and additional allowed
139139
relationship_type = RelationshipType.model_validate(
@@ -365,8 +365,15 @@ def valid_node_types() -> tuple[NodeType, ...]:
365365
NodeType(
366366
label="ORGANIZATION",
367367
description="A structured group of people with a common purpose.",
368+
properties=[PropertyType(name="name", type="STRING")],
369+
additional_properties=True,
370+
),
371+
NodeType(
372+
label="AGE",
373+
description="Age of a person in years.",
374+
properties=[PropertyType(name="value", type="INTEGER")],
375+
additional_properties=True,
368376
),
369-
NodeType(label="AGE", description="Age of a person in years."),
370377
)
371378

372379

tests/unit/experimental/pipeline/config/template_pipeline/test_simple_kg_builder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ def test_simple_kg_pipeline_config_process_schema_with_precedence_schema_dict()
427427
patterns = schema_dict["patterns"]
428428
assert len(node_types) == 2
429429
assert node_types[0]["label"] == "Person"
430-
assert len(node_types[0]["properties"]) == 0
430+
# String input gets default "name" property and additional_properties=True
431+
assert len(node_types[0]["properties"]) == 1
432+
assert node_types[0]["properties"][0]["name"] == "name"
433+
assert node_types[0]["additional_properties"] is True
431434
assert node_types[1]["label"] == "Organization"
432435
assert len(node_types[1]["properties"]) == 1
433436
assert relationship_types is not None
@@ -492,7 +495,10 @@ def test_simple_kg_pipeline_config_process_schema_with_precedence_schema_object(
492495
patterns = schema_dict["patterns"]
493496
assert len(node_types) == 2
494497
assert node_types[0]["label"] == "Person"
495-
assert len(node_types[0]["properties"]) == 0
498+
# String input gets default "name" property and additional_properties=True
499+
assert len(node_types[0]["properties"]) == 1
500+
assert node_types[0]["properties"][0]["name"] == "name"
501+
assert node_types[0]["additional_properties"] is True
496502
assert node_types[1]["label"] == "Organization"
497503
assert len(node_types[1]["properties"]) == 1
498504
assert relationship_types is not None

0 commit comments

Comments
 (0)