-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Cog 533 pydantic unit tests #230
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
628f192
Remove added_nodes and added_edges default dicts
0xideas 0ea011c
Adapt graph interfaces tests to debugged get_graph_from_model
0xideas 7be613e
WIP nested pydantic structures
0xideas 2c0fce3
WIP get_graph_from_model
0xideas 05ea357
Refactor get_graph_from_model
0xideas a586070
Remove include_root parameter
0xideas 3c8a52f
Fix inconsistent state between nodes and added_nodes and edges and ad…
0xideas afae70f
Add get_graph_from_model_generative_test
0xideas 5a464bf
Refactor get_model_instance_from_graph
0xideas 370b59b
Add get_graph_from_model_generative_test
0xideas f3f0bca
Revert making Person attributes optional
0xideas a1f7272
Revert model_rebuild order
0xideas 148eb4e
Add profile_graph_pydantic_conversion.py
0xideas 5b420eb
Autoformat graph pydantic conversion code
0xideas a334291
Apply cosmetic changes and autoformat
0xideas 8a2cf20
Add model_rebuild
0xideas 103eb13
Skip recursive pydantic tests for Python 3.9 and 3.10
0xideas fde56f0
Merge branch 'main' into COG-533-pydantic-unit-tests
0xideas b18f748
Merge dicts directly
0xideas ab1328d
Merge branch 'main' into COG-533-pydantic-unit-tests
borisarzentar 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
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
44 changes: 28 additions & 16 deletions
44
cognee/modules/graph/utils/get_model_instance_from_graph.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,29 +1,41 @@ | ||
| from typing import Callable | ||
|
|
||
| from pydantic_core import PydanticUndefined | ||
|
|
||
| from cognee.infrastructure.engine import DataPoint | ||
| from cognee.modules.storage.utils import copy_model | ||
|
|
||
|
|
||
| def get_model_instance_from_graph(nodes: list[DataPoint], edges: list, entity_id: str): | ||
| node_map = {} | ||
| def get_model_instance_from_graph( | ||
| nodes: list[DataPoint], | ||
| edges: list[tuple[str, str, str, dict[str, str]]], | ||
| entity_id: str, | ||
| ): | ||
| node_map = {node.id: node for node in nodes} | ||
|
|
||
| for node in nodes: | ||
| node_map[node.id] = node | ||
|
|
||
| for edge in edges: | ||
| source_node = node_map[edge[0]] | ||
| target_node = node_map[edge[1]] | ||
| edge_label = edge[2] | ||
| edge_properties = edge[3] if len(edge) == 4 else {} | ||
| for source_node_id, target_node_id, edge_label, edge_properties in edges: | ||
| source_node = node_map[source_node_id] | ||
| target_node = node_map[target_node_id] | ||
| edge_metadata = edge_properties.get("metadata", {}) | ||
| edge_type = edge_metadata.get("type") | ||
| edge_type = edge_metadata.get("type", "default") | ||
|
|
||
| if edge_type == "list": | ||
| NewModel = copy_model(type(source_node), { edge_label: (list[type(target_node)], PydanticUndefined) }) | ||
|
|
||
| node_map[edge[0]] = NewModel(**source_node.model_dump(), **{ edge_label: [target_node] }) | ||
| NewModel = copy_model( | ||
| type(source_node), | ||
| {edge_label: (list[type(target_node)], PydanticUndefined)}, | ||
| ) | ||
| source_node_dict = source_node.model_dump() | ||
| source_node_edge_label_values = source_node_dict.get(edge_label, []) | ||
| source_node_dict[edge_label] = source_node_edge_label_values + [target_node] | ||
|
|
||
| node_map[source_node_id] = NewModel(**source_node_dict) | ||
| else: | ||
| NewModel = copy_model(type(source_node), { edge_label: (type(target_node), PydanticUndefined) }) | ||
| NewModel = copy_model( | ||
| type(source_node), {edge_label: (type(target_node), PydanticUndefined)} | ||
| ) | ||
|
|
||
| node_map[edge[0]] = NewModel(**source_node.model_dump(), **{ edge_label: target_node }) | ||
| node_map[target_node_id] = NewModel( | ||
| **source_node.model_dump(), **{edge_label: target_node} | ||
| ) | ||
|
|
||
| return node_map[entity_id] |
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
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
37 changes: 37 additions & 0 deletions
37
cognee/tests/unit/interfaces/graph/get_graph_from_model_generative_test.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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import warnings | ||
|
|
||
| import pytest | ||
|
|
||
| from cognee.modules.graph.utils import get_graph_from_model | ||
| from cognee.tests.unit.interfaces.graph.util import ( | ||
| PERSON_NAMES, | ||
| count_society, | ||
| create_organization_recursive, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("recursive_depth", [1, 2, 3]) | ||
| def test_society_nodes_and_edges(recursive_depth): | ||
| import sys | ||
|
|
||
| if sys.version_info[0] == 3 and sys.version_info[1] >= 11: | ||
| society = create_organization_recursive( | ||
| "society", "Society", PERSON_NAMES, recursive_depth | ||
| ) | ||
|
|
||
| n_organizations, n_persons = count_society(society) | ||
| society_counts_total = n_organizations + n_persons | ||
|
|
||
| nodes, edges = get_graph_from_model(society) | ||
|
|
||
| assert ( | ||
| len(nodes) == society_counts_total | ||
| ), f"{society_counts_total = } != {len(nodes) = }, not all DataPoint instances were found" | ||
|
|
||
| assert len(edges) == ( | ||
| len(nodes) - 1 | ||
| ), f"{(len(nodes) - 1) = } != {len(edges) = }, there have to be n_nodes - 1 edges, as each node has exactly one parent node, except for the root node" | ||
| else: | ||
| warnings.warn( | ||
| "The recursive pydantic data structure cannot be reconstructed from the graph because the 'inner' pydantic class is not defined. Hence this test is skipped. This problem is solved in Python 3.11" | ||
| ) |
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.