-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: implement unit tests and extensive checks around the get_graph_from_model [COG-754] #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hajdul88
merged 14 commits into
dev
from
feature/cog-754-implement-unit-tests-and-extensive-checks-around-the
Jan 31, 2025
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd61f71
feat: adds some unit tests for get_graph_from_model
hajdul88 354b071
feat: updates neo4j add_edges cypher and deletes shallow get_graph_fr…
hajdul88 e904e64
Merge branch 'dev' into feature/cog-754-implement-unit-tests-and-exte…
hajdul88 e915ee5
Merge branch 'dev' into feature/cog-754-implement-unit-tests-and-exte…
hajdul88 fe71c7a
fix: fixing merge conflict false resolve
hajdul88 0a7fedd
chore: deletes old only_root unit test
hajdul88 a1a89df
feat: changes pydantic model reference in get_graph_from_model unit test
hajdul88 03fdc41
Merge branch 'dev' into feature/cog-754-implement-unit-tests-and-exte…
hajdul88 9e9ca24
feat: unit tests for get graph from model
hajdul88 41007aa
feat: adds pytest asyncio to cicd
hajdul88 3f5d309
fix: renames unit test
hajdul88 4c2d167
Revert "feat: adds pytest asyncio to cicd"
hajdul88 a7ab508
chore: removes feature/ trigger from cd.yaml
hajdul88 7f00700
Merge branch 'dev' into feature/cog-754-implement-unit-tests-and-exte…
hajdul88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 32 additions & 96 deletions
128
cognee/tests/unit/interfaces/graph/get_graph_from_model_unit_tests.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,56 @@ | ||
| import pytest | ||
| import asyncio | ||
| import random | ||
| from typing import List | ||
| from uuid import NAMESPACE_OID, uuid5 | ||
| from uuid import uuid4 | ||
|
|
||
| from IPython.utils.wildcard import is_type | ||
|
|
||
| from typing import List, Optional | ||
| from cognee.infrastructure.engine import DataPoint | ||
| from cognee.modules.engine.models.Entity import Entity, EntityType | ||
| from cognee.modules.chunking.models.DocumentChunk import DocumentChunk | ||
| from cognee.modules.data.processing.document_types import Document | ||
| from cognee.modules.graph.utils import get_graph_from_model | ||
|
|
||
| from cognee.modules.graph.utils import get_graph_from_model | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_graph_from_model_basic_initialization(): | ||
| """Test the basic behavior of get_graph_from_model with a simple data point - without connection.""" | ||
| data_point = DataPoint(id=uuid4(), attributes={"name": "Node1"}) | ||
| added_nodes = {} | ||
| added_edges = {} | ||
| visited_properties = {} | ||
|
|
||
| nodes, edges = await get_graph_from_model( | ||
| data_point, added_nodes, added_edges, visited_properties | ||
| ) | ||
|
|
||
| assert len(nodes) == 1 | ||
| assert len(edges) == 0 | ||
| assert str(data_point.id) in added_nodes | ||
| class Document(DataPoint): | ||
| path: str | ||
| _metadata = {"index_fields": [], "type": "Document"} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_graph_from_model_with_single_neighbor(): | ||
| """Test the behavior of get_graph_from_model when a data point has a single DataPoint property.""" | ||
| type_node = EntityType( | ||
| id=uuid4(), | ||
| name="Vehicle", | ||
| description="This is a Vehicle node", | ||
| ) | ||
| class DocumentChunk(DataPoint): | ||
| part_of: Document | ||
| text: str | ||
| contains: List["Entity"] = None | ||
| _metadata = {"index_fields": ["text"], "type": "DocumentChunk"} | ||
|
|
||
| entity_node = Entity( | ||
| id=uuid4(), | ||
| name="Car", | ||
| is_a=type_node, | ||
| description="This is a car node", | ||
| ) | ||
| added_nodes = {} | ||
| added_edges = {} | ||
| visited_properties = {} | ||
|
|
||
| nodes, edges = await get_graph_from_model( | ||
| entity_node, added_nodes, added_edges, visited_properties | ||
| ) | ||
| class EntityType(DataPoint): | ||
| name: str | ||
| _metadata = {"index_fields": ["name"], "type": "EntityType"} | ||
|
|
||
| assert len(nodes) == 2 | ||
| assert len(edges) == 1 | ||
| assert str(entity_node.id) in added_nodes | ||
| assert str(type_node.id) in added_nodes | ||
| assert (str(entity_node.id) + str(type_node.id) + "is_a") in added_edges | ||
|
|
||
| class Entity(DataPoint): | ||
| name: str | ||
| is_type: EntityType | ||
| _metadata = {"index_fields": ["name"], "type": "Entity"} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_graph_from_model_with_multiple_nested_connections(): | ||
| """Test the behavior of get_graph_from_model when a data point has multiple nested DataPoint property.""" | ||
| type_node = EntityType( | ||
| id=uuid4(), | ||
| name="Transportation tool", | ||
| description="This is a Vehicle node", | ||
| ) | ||
|
|
||
| entity_node_1 = Entity( | ||
| id=uuid4(), | ||
| name="Car", | ||
| is_a=type_node, | ||
| description="This is a car node", | ||
| ) | ||
| DocumentChunk.model_rebuild() | ||
|
|
||
| entity_node_2 = Entity( | ||
| id=uuid4(), | ||
| name="Bus", | ||
| is_a=type_node, | ||
| description="This is a bus node", | ||
| ) | ||
|
|
||
| document = Document( | ||
| name="main_document", raw_data_location="home/", metadata_id=uuid4(), mime_type="test" | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_get_graph_from_model_simple_structure(): | ||
| """ | ||
| Tests simple pydantic structure for get_graph_from_model | ||
| """ | ||
|
|
||
| chunk = DocumentChunk( | ||
| id=uuid4(), | ||
| word_count=8, | ||
| chunk_index=0, | ||
| cut_type="test", | ||
| text="The car and the bus are transportation tools", | ||
| is_part_of=document, | ||
| contains=[entity_node_1, entity_node_2], | ||
| entitytype = EntityType( | ||
| name="TestType", | ||
| ) | ||
|
|
||
| entity = Entity(name="TestEntity", is_type=entitytype) | ||
|
|
||
| added_nodes = {} | ||
| added_edges = {} | ||
| visited_properties = {} | ||
|
|
||
| nodes, edges = await get_graph_from_model(chunk, added_nodes, added_edges, visited_properties) | ||
|
|
||
| assert len(nodes) == 5 | ||
| assert len(edges) == 5 | ||
| nodes, edges = await get_graph_from_model(entity, added_nodes, added_edges, visited_properties) | ||
|
|
||
| assert str(entity_node_1.id) in added_nodes | ||
| assert str(entity_node_2.id) in added_nodes | ||
| assert str(type_node.id) in added_nodes | ||
| assert str(document.id) in added_nodes | ||
| assert str(chunk.id) in added_nodes | ||
| assert len(nodes) == 2, f"Expected 2 nodes, got {len(nodes)}" | ||
| assert len(edges) == 1, f"Expected 1 edges, got {len(edges)}" | ||
|
|
||
| assert (str(entity_node_1.id) + str(type_node.id) + "is_a") in added_edges | ||
| assert (str(entity_node_2.id) + str(type_node.id) + "is_a") in added_edges | ||
| assert (str(chunk.id) + str(document.id) + "is_part_of") in added_edges | ||
| assert (str(chunk.id) + str(entity_node_1.id) + "contains") in added_edges | ||
| assert (str(chunk.id) + str(entity_node_2.id) + "contains") in added_edges | ||
| edge_key = str(entity.id) + str(entitytype.id) + "is_type" | ||
| assert edge_key in added_edges, f"Edge {edge_key} not found" | ||
hajdul88 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.