Files
topoteretes--cognee/cognee/tests/test_external_source_import.py
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

134 lines
5.2 KiB
Python

"""E2E: import a NON-cognee memory export (Zep/Graphiti graph shape) and
search it.
The cross-provider half of the COGX contract, driven through the public
``cognee.remember(ZepSource(...))`` path against real stores: external
entities get cognee's CURRENT class-namespaced ids (``Entity.id_for(name)``,
never the source system's ids), same-named records merge into one node,
facts that resolve onto one edge key are deduplicated (reported in the
import summary), labels become EntityType nodes, and the imported graph
answers a search.
Runs single-tenant: external imports are identity-agnostic (name-keyed), and
the multi-user posture is covered by test_cogx_roundtrip.py.
"""
import asyncio
import json
import os
import pathlib
import tempfile
from uuid import uuid4
import cognee
from cognee.modules.migration.sources.zep import ZepSource
from cognee.modules.search.types import SearchType
from cognee.shared.logging_utils import get_logger
logger = get_logger()
QUERY = "Who did Ada Lovelace collaborate with?"
def build_zep_export(path: pathlib.Path) -> None:
"""A realistic Zep/Graphiti graph export: UUID-keyed entities, edges by
node uuid, one episode. Two same-named entities and two facts that
resolve onto one edge key exercise merging + dedup."""
ada_one, ada_two, babbage = str(uuid4()), str(uuid4()), str(uuid4())
export = {
"episodes": [
{
"uuid": str(uuid4()),
"name": "history-chat",
"content": "We discussed how Ada Lovelace collaborated with Charles Babbage.",
"created_at": "2026-01-01T10:00:00Z",
}
],
"entities": [
{"uuid": ada_one, "name": "Ada Lovelace", "labels": ["Person"]},
{"uuid": ada_two, "name": "Ada Lovelace", "labels": ["Person"]},
{"uuid": babbage, "name": "Charles Babbage", "labels": ["Person"]},
],
"edges": [
{
"uuid": str(uuid4()),
"source_node_uuid": ada_one,
"target_node_uuid": babbage,
"name": "COLLABORATED_WITH",
"fact": "Ada Lovelace collaborated with Charles Babbage.",
},
{
"uuid": str(uuid4()),
"source_node_uuid": ada_two,
"target_node_uuid": babbage,
"name": "COLLABORATED_WITH",
"fact": "Lovelace worked with Babbage on the Analytical Engine.",
},
],
}
path.write_text(json.dumps(export), encoding="utf-8")
async def main():
os.environ["AUTO_FEEDBACK"] = "False"
# Single-tenant: read dynamically, so this wins over any ambient .env.
os.environ["ENABLE_BACKEND_ACCESS_CONTROL"] = "false"
base = pathlib.Path(__file__).parent
cognee.config.data_root_directory(
str((base / ".data_storage/test_external_source_import").resolve())
)
cognee.config.system_root_directory(
str((base / ".cognee_system/test_external_source_import").resolve())
)
await cognee.prune.prune_data()
await cognee.prune.prune_system(metadata=True)
with tempfile.TemporaryDirectory() as temporary_directory:
export_path = pathlib.Path(temporary_directory) / "zep_export.json"
build_zep_export(export_path)
source = ZepSource(str(export_path), mode="preserve")
result = await cognee.remember(source, dataset_name="zep_import")
assert result.status == "completed", f"Import did not complete: {result.status!r}"
(summary,) = [item for item in result.items if item.get("kind") == "migration_import"]
logger.info("Import summary: %s", summary)
assert summary["record_counts"] == {"episode": 1, "entity": 3, "fact": 2}
assert summary["deduped_edges"] == 1, (
"Two facts resolving onto one edge key must dedupe to a single edge."
)
assert summary["skipped_facts"] == 0
from cognee.infrastructure.databases.graph import get_graph_engine
from cognee.modules.engine.models import Entity
engine = await get_graph_engine()
nodes, edges = await engine.get_graph_data()
adas = [(node_id, props) for node_id, props in nodes if props.get("name") == "Ada Lovelace"]
assert len(adas) == 1, "Same-named external entities must merge into one node."
assert str(adas[0][0]) == str(Entity.id_for("Ada Lovelace")), (
"External entities must use cognee's current class-namespaced ids."
)
assert any(
props.get("type") == "EntityType" and props.get("name") == "Person" for _, props in nodes
), "Zep labels must become EntityType nodes."
collaborated = [edge for edge in edges if edge[2] == "COLLABORATED_WITH"]
assert len(collaborated) == 1, (
f"Expected exactly 1 COLLABORATED_WITH edge after dedup, got {len(collaborated)}."
)
results = await cognee.search(
query_type=SearchType.GRAPH_COMPLETION, query_text=QUERY, datasets=["zep_import"]
)
assert len(results) != 0, "Search on the imported external graph returned nothing."
logger.info("Search results: %s", results)
print("External source import e2e passed: Zep export -> import -> search.")
if __name__ == "__main__":
asyncio.run(main())