-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from topoteretes/COG-533-pydantic-unit-tests
Cog 533 pydantic unit tests
- Loading branch information
Showing
11 changed files
with
471 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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.