MicroFish/backend/tests/test_ontology_attributes.py

65 lines
2.0 KiB
Python

from app.services.ontology_generator import OntologyGenerator
from app.services.graph_builder import GraphBuilderService
from app.utils.ontology import normalize_ontology_attribute
def test_normalize_string_attribute():
assert normalize_ontology_attribute("role") == {
"name": "role",
"type": "text",
"description": "role",
}
def test_preserve_valid_dictionary_attribute():
original = {"name": "role", "type": "text", "description": "Public role"}
assert normalize_ontology_attribute(original) == original
assert normalize_ontology_attribute(original) is not original
def test_reject_unusable_attribute_shapes():
for value in (None, 7, [], {}, {"name": None}, {"name": ""}, " "):
assert normalize_ontology_attribute(value) is None
def test_generator_normalizes_entity_and_edge_attributes():
result = OntologyGenerator(llm_client=object())._validate_and_process({
"entity_types": [{"name": "speaker", "attributes": ["role", None]}],
"edge_types": [{"name": "quotes", "attributes": ["source_url", {}]}],
})
assert result["entity_types"][0]["attributes"] == [{
"name": "role",
"type": "text",
"description": "role",
}]
assert result["edge_types"][0]["attributes"] == [{
"name": "source_url",
"type": "text",
"description": "source_url",
}]
def test_graph_builder_safety_net_accepts_strings_and_skips_invalid_values():
captured = {}
class GraphApi:
def set_ontology(self, **kwargs):
captured.update(kwargs)
class Client:
graph = GraphApi()
builder = object.__new__(GraphBuilderService)
builder.client = Client()
builder.set_ontology("graph-id", {
"entity_types": [{
"name": "Speaker",
"attributes": ["role", None, {"name": "summary"}],
}],
"edge_types": [],
})
speaker = captured["entities"]["Speaker"]
assert set(speaker.__annotations__) == {"role", "entity_summary"}