diff --git a/mgraph_ai/mgraph/domain/Domain__MGraph__Graph.py b/mgraph_ai/mgraph/domain/Domain__MGraph__Graph.py index b14d7b0..9417895 100644 --- a/mgraph_ai/mgraph/domain/Domain__MGraph__Graph.py +++ b/mgraph_ai/mgraph/domain/Domain__MGraph__Graph.py @@ -53,3 +53,6 @@ def node(self, node_id: Random_Guid) -> Domain__MGraph__Node: def nodes(self) -> List[Domain__MGraph__Node]: return [self.mgraph_node(node=node) for node in self.model.nodes()] + def nodes_ids(self): + return self.model.nodes_ids() + diff --git a/mgraph_ai/mgraph/domain/Domain__MGraph__Node.py b/mgraph_ai/mgraph/domain/Domain__MGraph__Node.py index eef1f46..22564b9 100644 --- a/mgraph_ai/mgraph/domain/Domain__MGraph__Node.py +++ b/mgraph_ai/mgraph/domain/Domain__MGraph__Node.py @@ -19,7 +19,7 @@ def add_node(self, node: Model__MGraph__Node) -> None: def models__edges(self) -> List[Model__MGraph__Edge]: # Get all model edges connected to this node connected_edges = [] for edge in self.graph.edges(): - if edge.from_node_id() == self.node_id() or edge.to_node_id() == self.node_id(): + if edge.from_node_id() == self.node_id or edge.to_node_id() == self.node_id: connected_edges.append(edge) return connected_edges @@ -33,7 +33,7 @@ def models__from_edges(self) -> List[Model__MGraph__Edge]: def models__to_edges(self) -> List[Model__MGraph__Edge]: # Get model edges where this node is the target incoming_edges = [] for edge in self.graph.edges(): - if edge.to_node_id() == self.node_id(): + if edge.to_node_id() == self.node_id: incoming_edges.append(edge) return incoming_edges diff --git a/mgraph_ai/mgraph/utils/MGraph__Random_Graph.py b/mgraph_ai/mgraph/utils/MGraph__Random_Graph.py index 50e17c8..c6549ed 100644 --- a/mgraph_ai/mgraph/utils/MGraph__Random_Graph.py +++ b/mgraph_ai/mgraph/utils/MGraph__Random_Graph.py @@ -61,9 +61,6 @@ def create_random_graph(self, num_nodes: int = 10, num_edges: Optional[int] = No return self.graph - def empty_graph(self): - return self.setup().graph - def create_random_mgraph(num_nodes=2, num_edges=2) -> MGraph: # Create an empty graph with no nodes or edges return MGraph__Random_Graph().setup().create_random_graph(num_nodes=num_nodes, num_edges=num_edges) diff --git a/mgraph_ai/mgraph/utils/MGraph__Static__Graph.py b/mgraph_ai/mgraph/utils/MGraph__Static__Graph.py index 130d9f9..c742977 100644 --- a/mgraph_ai/mgraph/utils/MGraph__Static__Graph.py +++ b/mgraph_ai/mgraph/utils/MGraph__Static__Graph.py @@ -8,14 +8,6 @@ class MGraph__Static__Graph(Type_Safe): node_ids: List[Random_Guid] edge_ids: List[Random_Guid] - @property - def total_nodes(self) -> int: # Returns total number of nodes in the graph - return len(self.node_ids) - - @property - def total_edges(self) -> int: # Returns total number of edges in the graph - return len(self.edge_ids) - def create_nodes(self, count: int) -> List[Random_Guid]: # Creates specified number of nodes with self.graph.edit() as edit: return [edit.new_node().node_id for _ in range(count)] diff --git a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Graph.py b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Graph.py index 9f05f5c..4511042 100644 --- a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Graph.py +++ b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Graph.py @@ -1,20 +1,26 @@ -from typing import Any, Optional -from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Types import Domain__MGraph__Json__Types -from mgraph_ai.providers.json.models.Model__MGraph__Json__Graph import Model__MGraph__Json__Graph -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__List import Domain__MGraph__Json__Node__List -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Value import Domain__MGraph__Json__Node__Value -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node +from typing import Any, Optional +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__List import Model__MGraph__Json__Node__List +from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Types import Domain__MGraph__Json__Types +from mgraph_ai.providers.json.models.Model__MGraph__Json__Graph import Model__MGraph__Json__Graph +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__List import Domain__MGraph__Json__Node__List +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Value import Domain__MGraph__Json__Node__Value +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Value import Model__MGraph__Json__Node__Value +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value__Data import Schema__MGraph__Json__Node__Value__Data class Domain__MGraph__Json__Graph(Domain__MGraph__Graph): domain_types : Domain__MGraph__Json__Types model : Model__MGraph__Json__Graph - def root(self) -> Domain__MGraph__Json__Node: # Get root node - """Get the root node, creating it if it doesn't exist""" + def root(self) -> Domain__MGraph__Json__Node: #Get the root node, creating it if it doesn't exist if not self.model.data.graph_data.root_id: schema_node = Schema__MGraph__Json__Node() # Create basic root node node = self.model.add_node(schema_node) @@ -22,20 +28,17 @@ def root(self) -> Domain__MGraph__Json__Node: return Domain__MGraph__Json__Node(node=node, graph=self.model) root_model = self.model.node(self.model.data.graph_data.root_id) - if not root_model: - raise ValueError("Root node ID exists but node not found") return Domain__MGraph__Json__Node(node=root_model, graph=self.model) - def root_content(self) -> Optional[Domain__MGraph__Json__Node]: # Get the content node - """Get the content node attached to root (if any)""" + def root_content(self) -> Optional[Domain__MGraph__Json__Node]: # Get the content node attached to root (if any)""" root = self.root() edges = self.model.node__from_edges(root.node_id) if not edges: return None - content_node = self.model.node(edges[0].to_node_id()) # Get first child - + edge__to_node_id = edges[0].to_node_id() # first edge + content_node = self.model.node(edge__to_node_id) # Get first child # Return appropriate domain node type if isinstance(content_node.data, Schema__MGraph__Json__Node__Dict): return Domain__MGraph__Json__Node__Dict(node=content_node, graph=self.model) @@ -44,20 +47,15 @@ def root_content(self) -> Optional[Domain__MGraph__Json__Node]: elif isinstance(content_node.data, Schema__MGraph__Json__Node__Value): return Domain__MGraph__Json__Node__Value(node=content_node, graph=self.model) - return None - - def set_root_content(self, data: Any) -> Domain__MGraph__Json__Node: # Set content node - """Set the JSON content, creating appropriate node type""" + def set_root_content(self, data: Any) -> Domain__MGraph__Json__Node: # Set the JSON content, creating appropriate node type root = self.root() - # Remove any existing content - edges = self.model.node__from_edges(root.node_id) + edges = self.model.node__from_edges(root.node_id) # Remove any existing content for edge in edges: self.model.delete_edge(edge.edge_id()) self.model.delete_node(edge.to_node_id()) - # Create appropriate node type for new content - if isinstance(data, dict): + if isinstance(data, dict): # Create appropriate node type for new content content = self.new_dict_node(data) elif isinstance(data, (list, tuple)): content = self.new_list_node(data) @@ -68,31 +66,34 @@ def set_root_content(self, data: Any) -> Domain__MGraph__Json__Node: self.model.new_edge(from_node_id=root.node_id, to_node_id=content.node_id) return content - def new_dict_node(self, properties=None) -> Domain__MGraph__Json__Node__Dict: # Create dictionary node - """Create a new dictionary node with optional initial properties""" + def new_dict_node(self, properties=None) -> Domain__MGraph__Json__Node__Dict: # Create a new dictionary node with optional initial properties""" schema_node = Schema__MGraph__Json__Node__Dict() - node = self.model.add_node(schema_node) - dict_node = Domain__MGraph__Json__Node__Dict(node=node, graph=self.model) + #node = self.model.add_node(schema_node) # todo:: find way to use self.model method + self.model.data.nodes[schema_node.node_id] = schema_node # so that we don't need to add this here + model_node = Model__MGraph__Json__Node__Dict(data=schema_node) + dict_node = Domain__MGraph__Json__Node__Dict(node=model_node, graph=self.model) if properties: dict_node.update(properties) return dict_node - def new_list_node(self, items=None) -> Domain__MGraph__Json__Node__List: # Create list node - """Create a new list node with optional initial items""" + def new_list_node(self, items=None) -> Domain__MGraph__Json__Node__List: # Create a new list node with optional initial items""" schema_node = Schema__MGraph__Json__Node__List() - node = self.model.add_node(schema_node) - list_node = Domain__MGraph__Json__Node__List(node=node, graph=self.model) + #node = self.model.add_node(schema_node) # todo:: find way to use self.model method + self.model.data.nodes[schema_node.node_id] = schema_node # so that we don't need to add this here + model_node = Model__MGraph__Json__Node__List(data=schema_node) + list_node = Domain__MGraph__Json__Node__List(node=model_node, graph=self.model) if items: list_node.extend(items) return list_node - def new_value_node(self, value: Any) -> Domain__MGraph__Json__Node__Value: # Create value node - """Create a new value node with the given value""" - node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value)) + def new_value_node(self, value: Any) -> Domain__MGraph__Json__Node__Value: # Create a new value node with the given value + node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value)) schema_node = Schema__MGraph__Json__Node__Value(node_data=node_data) - node = self.model.add_node(schema_node) - return Domain__MGraph__Json__Node__Value(node=node, graph=self.model) \ No newline at end of file + #node = self.model.add_node(schema_node) # todo:: find way to use self.model method + model_node = Model__MGraph__Json__Node__Value(data=schema_node) # so that we don't need to add this here + self.model.data.nodes[schema_node.node_id] = schema_node + return Domain__MGraph__Json__Node__Value(node=model_node, graph=self.model) \ No newline at end of file diff --git a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node.py b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node.py index 7b5c0e4..3c09a0b 100644 --- a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node.py +++ b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node.py @@ -1,21 +1,5 @@ -from typing import Optional from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value - class Domain__MGraph__Json__Node(Domain__MGraph__Node): - node: Model__MGraph__Json__Node # Reference to node model - - def get_type(self) -> Optional[str]: # Get node type - """Get the type of this JSON node (dict, list, or value)""" - node_type = self.node.data.node_type - if node_type == Schema__MGraph__Json__Node__Dict: - return 'dict' - elif node_type == Schema__MGraph__Json__Node__List: - return 'list' - elif node_type == Schema__MGraph__Json__Node__Value: - return 'value' - return None \ No newline at end of file + node: Model__MGraph__Json__Node # Reference to node model \ No newline at end of file diff --git a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Dict.py b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Dict.py index cf0b0cb..f175535 100644 --- a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Dict.py +++ b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Dict.py @@ -14,8 +14,6 @@ def properties(self) -> Dict[str, Any]: result = {} for edge in self.models__from_edges(): property_node = self.model__node_from_edge(edge) - if property_node.data.node_type != Schema__MGraph__Json__Node__Property: - continue for value_edge in self.graph.edges(): # todo: see why we need to use self.graph.edges() if value_edge.from_node_id() == property_node.node_id: diff --git a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__List.py b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__List.py index f39a0d4..0f44cb7 100644 --- a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__List.py +++ b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__List.py @@ -1,6 +1,7 @@ from typing import List, Any, Union from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__List import Model__MGraph__Json__Node__List from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List @@ -32,39 +33,57 @@ def items(self) -> List[Any]: # Get al def add(self, value: Any) -> None: """Add an item to the list. The item can be a primitive value, dict, or list.""" - if isinstance(value, (str, int, float, bool)) or value is None: - # Handle primitive values - node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value)) - schema_node = Schema__MGraph__Json__Node__Value (node_data=node_data) - value_node = self.graph.add_node(schema_node) + if isinstance(value, (str, int, float, bool)) or value is None: # Handle primitive values + node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value)) + schema_node = Schema__MGraph__Json__Node__Value (node_data=node_data) + new_node = self.graph.add_node(schema_node) - elif isinstance(value, dict): - # Handle dictionaries - dict_node = Schema__MGraph__Json__Node__Dict() - value_node = self.graph.add_node(dict_node) - domain_dict = Domain__MGraph__Json__Node__Dict(node=value_node, graph=self.graph) + + elif isinstance(value, dict): # Handle dictionaries + dict_node = Schema__MGraph__Json__Node__Dict() + #value_node = self.graph.add_node(dict_node) + model_node = Model__MGraph__Json__Node__Dict (data=dict_node) + domain_dict = Domain__MGraph__Json__Node__Dict(node=model_node, graph=self.graph) + new_node = self.graph.add_node(dict_node) domain_dict.update(value) - elif isinstance(value, (list, tuple)): - # Handle lists - list_node = Schema__MGraph__Json__Node__List() - value_node = self.graph.add_node(list_node) - domain_list = Domain__MGraph__Json__Node__List(node=value_node, graph=self.graph) + elif isinstance(value, (list, tuple)): # Handle lists + list_node = Schema__MGraph__Json__Node__List() + #value_node = self.graph.add_node(list_node) + model_node = Model__MGraph__Json__Node__List(data=list_node) + domain_list = Domain__MGraph__Json__Node__List(node=model_node, graph=self.graph) + new_node = self.graph.add_node(list_node) for item in value: domain_list.add(item) - else: - raise ValueError(f"Unsupported value type: {type(value)}") - print() - print("from_node_id = ", self.node.node_id, " | to_node_id=", value_node.node_id) # Connect the new node to this list - #self.graph.new_edge(from_node_id=self.node.node_id, to_node_id=value_node.node_id) + self.graph.new_edge(from_node_id=self.node.node_id, to_node_id=new_node.node_id) - def clear(self) -> None: # Remove all items - for edge in self.models__from_edges(): - target_node = self.model__node_from_edge(edge) - self.graph.delete_edge(edge.edge_id()) - self.graph.delete_node(target_node.node_id()) + def clear(self) -> None: # Remove all items + for edge in self.models__from_edges(): # todo: this doesn't work 100% since the nodes linked from the + target_node = self.model__node_from_edge(edge) # list items are note deleted + self.graph.delete_edge(edge.edge_id()) # see the code below for one way to do this + self.graph.delete_node(target_node.node_id) + + # def clear(self) -> None: # todo: this needs a refactoring since we shouldn't be doing so much work here + # def delete_node_recursive(node_id): # basically there should be a method that returns that tree of linked nodes + # # Get all outgoing edges from this node # and edges. We also need need to take into account recursive loops + # for edge in self.graph.model.models__from_edges(node_id): + # target_id = edge.to_node_id() + # # Recursively delete the target node and its subgraph + # delete_node_recursive(target_id) + # # Delete the edge + # self.graph.delete_edge(edge.edge_id) + # # Delete the node itself + # self.graph.delete_node(node_id) + # + # # For each direct child of the list + # for edge in self.models__from_edges(): + # target_node = self.model__node_from_edge(edge) + # # Recursively delete the node and its subgraph + # delete_node_recursive(target_node.node_id) + # # Delete the edge connecting it to the list + # self.graph.delete_edge(edge.edge_id()) def extend(self, items: List[Any]) -> None: """Add multiple items to the list""" @@ -77,6 +96,6 @@ def remove(self, value: Any) -> bool: # Remove f if target_node.data.node_type == Schema__MGraph__Json__Node__Value: if target_node.data.node_data.value == value: self.graph.delete_edge(edge.edge_id()) - self.graph.delete_node(target_node.node_id()) + self.graph.delete_node(target_node.node_id) return True return False \ No newline at end of file diff --git a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Property.py b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Property.py index bc6fc96..783f78a 100644 --- a/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Property.py +++ b/mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__Property.py @@ -1,6 +1,7 @@ -from osbot_utils.type_safe.methods.type_safe_property import set_as_property -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node -from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node__Property +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Property import Model__MGraph__Json__Node__Property +from osbot_utils.type_safe.methods.type_safe_property import set_as_property +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node + diff --git a/mgraph_ai/providers/json/models/Model__MGraph__Json__Graph.py b/mgraph_ai/providers/json/models/Model__MGraph__Json__Graph.py index 4702b84..fc79072 100644 --- a/mgraph_ai/providers/json/models/Model__MGraph__Json__Graph.py +++ b/mgraph_ai/providers/json/models/Model__MGraph__Json__Graph.py @@ -1,18 +1,31 @@ -from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph -from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict -from mgraph_ai.providers.json.models.Model__MGraph__Json__Types import Model__MGraph__Json__Types -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Graph import Schema__MGraph__Json__Graph -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict - +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__List import Model__MGraph__Json__Node__List +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Property import Model__MGraph__Json__Node__Property +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Value import Model__MGraph__Json__Node__Value +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Property import Schema__MGraph__Json__Node__Property +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value +from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph +from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict +from mgraph_ai.providers.json.models.Model__MGraph__Json__Types import Model__MGraph__Json__Types +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Graph import Schema__MGraph__Json__Graph +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict class Model__MGraph__Json__Graph(Model__MGraph__Graph): data : Schema__MGraph__Json__Graph model_types: Model__MGraph__Json__Types - def add_node__dict(self): - data = Schema__MGraph__Json__Node__Dict() - node = Model__MGraph__Json__Node__Dict(data=data) - self.data.nodes[node.node_id] = data # for now, we need to add this directly here - # self.add_node(data) # todo: improve add_node to receive the model to create since at the not going ot create a Model__MGraph__Json__Node__Dict - - return node \ No newline at end of file + def node(self, node_id): + node = self.data.nodes.get(node_id) + if node: + if node.node_type is Schema__MGraph__Json__Node: # todo: add a type resolver so that we don't have to hard code all these mappings here + return Model__MGraph__Json__Node(data=node) + elif node.node_type is Schema__MGraph__Json__Node__Dict: + return Model__MGraph__Json__Node__Dict(data=node) + elif node.node_type is Schema__MGraph__Json__Node__List: + return Model__MGraph__Json__Node__List(data=node) + elif node.node_type is Schema__MGraph__Json__Node__Value: + return Model__MGraph__Json__Node__Value(data=node) + elif node.node_type is Schema__MGraph__Json__Node__Property: + return Model__MGraph__Json__Node__Property(data=node) \ No newline at end of file diff --git a/tests/unit/mgraph/domain/test_MGraph__Edge.py b/tests/unit/mgraph/domain/test_Domain__MGraph__Edge.py similarity index 98% rename from tests/unit/mgraph/domain/test_MGraph__Edge.py rename to tests/unit/mgraph/domain/test_Domain__MGraph__Edge.py index d55dd1e..67cdacc 100644 --- a/tests/unit/mgraph/domain/test_MGraph__Edge.py +++ b/tests/unit/mgraph/domain/test_Domain__MGraph__Edge.py @@ -12,7 +12,7 @@ from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data from osbot_utils.helpers.Random_Guid import Random_Guid -class test_MGraph__Edge(TestCase): +class test_Domain__MGraph__Edge(TestCase): def setUp(self): # Initialize test data self.graph = Model__MGraph__Graph ( data=None) # Mock graph for testing diff --git a/tests/unit/mgraph/domain/test_MGraph__Graph.py b/tests/unit/mgraph/domain/test_Domain__MGraph__Graph.py similarity index 85% rename from tests/unit/mgraph/domain/test_MGraph__Graph.py rename to tests/unit/mgraph/domain/test_Domain__MGraph__Graph.py index e19306f..6e4995f 100644 --- a/tests/unit/mgraph/domain/test_MGraph__Graph.py +++ b/tests/unit/mgraph/domain/test_Domain__MGraph__Graph.py @@ -1,16 +1,16 @@ -from unittest import TestCase -from mgraph_ai.mgraph.schemas.Schema__MGraph__Types import Schema__MGraph__Types -from mgraph_ai.mgraph.domain.Domain__MGraph__Edge import Domain__MGraph__Edge -from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node -from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node -from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph -from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph -from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph import Schema__MGraph__Graph -from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node +from unittest import TestCase +from mgraph_ai.mgraph.schemas.Schema__MGraph__Types import Schema__MGraph__Types +from mgraph_ai.mgraph.domain.Domain__MGraph__Edge import Domain__MGraph__Edge +from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node +from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node +from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph +from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph +from mgraph_ai.mgraph.schemas.Schema__MGraph__Graph import Schema__MGraph__Graph +from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node class Simple_Node(Schema__MGraph__Node): pass # Helper class for testing -class test_MGraph__Graph(TestCase): +class test_Domain__MGraph__Graph(TestCase): def setUp(self): # Initialize test data self.schema_types = Schema__MGraph__Types (node_type = Simple_Node, diff --git a/tests/unit/mgraph/domain/test_Domain__MGraph__Node.py b/tests/unit/mgraph/domain/test_Domain__MGraph__Node.py new file mode 100644 index 0000000..4c9a92c --- /dev/null +++ b/tests/unit/mgraph/domain/test_Domain__MGraph__Node.py @@ -0,0 +1,148 @@ +from unittest import TestCase +from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node +from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node +from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph +from mgraph_ai.mgraph.models.Model__MGraph__Edge import Model__MGraph__Edge +from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node +from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data +from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge +from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge__Config import Schema__MGraph__Edge__Config +from osbot_utils.helpers.Random_Guid import Random_Guid + +class test_Domain__MGraph__Node(TestCase): + + def setUp(self): # Initialize test data + self.node_data = Schema__MGraph__Node__Data() + self.schema_node = Schema__MGraph__Node (node_data = self.node_data , + node_type = Schema__MGraph__Node) + self.model_node = Model__MGraph__Node (data = self.schema_node) + self.graph = Model__MGraph__Graph (data = None ) # Mock graph for testing + self.node = Domain__MGraph__Node (node = self.model_node, + graph = self.graph ) + self.graph.add_node(self.node.node.data) + + def test_init(self): # Tests basic initialization + assert type(self.node) is Domain__MGraph__Node + assert self.node.node is self.model_node + assert self.node.graph is self.graph + assert type(self.node.node_id) is Random_Guid + + def test_add_node(self): # Tests node addition functionality + new_node_data = Schema__MGraph__Node__Data() + new_schema_node = Schema__MGraph__Node (node_data = new_node_data , + node_type = Schema__MGraph__Node) + new_model_node = Model__MGraph__Node (data = new_schema_node) + + self.node.add_node(new_model_node) + + # Verify the node was added to the graph + added_node = self.graph.node(new_schema_node.node_id) + assert added_node is not None + assert added_node.data is new_schema_node + + def test_models__edges(self): # Tests retrieving all connected edges + node_1_id = self.graph.new_node().node_id + # Create test edges + edge_config_1 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + edge_1 = Schema__MGraph__Edge (edge_config = edge_config_1, + from_node_id = self.node.node_id, + to_node_id = node_1_id, + edge_type = Schema__MGraph__Edge) + + edge_config_2 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + edge_2 = Schema__MGraph__Edge (edge_config = edge_config_2, + from_node_id = node_1_id, + to_node_id = self.node.node_id, + edge_type = Schema__MGraph__Edge) + + # Add edges to graph + self.graph.add_edge(edge_1) + self.graph.add_edge(edge_2) + + # Test edge retrieval + connected_edges = self.node.models__edges() + assert len(connected_edges) == 2 + assert any(edge.edge_id() == edge_config_1.edge_id for edge in connected_edges) + assert any(edge.edge_id() == edge_config_2.edge_id for edge in connected_edges) + + def test_models__from_edges(self): # Tests retrieving outgoing edges + node_1_id = self.graph.new_node().node_id + # Create test edges + edge_config_1 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + outgoing_edge = Schema__MGraph__Edge (edge_config = edge_config_1, + from_node_id = self.node.node_id, + to_node_id = node_1_id, + edge_type = Schema__MGraph__Edge) + + edge_config_2 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + incoming_edge = Schema__MGraph__Edge (edge_config = edge_config_2, + from_node_id = node_1_id, + to_node_id = self.node.node_id, + edge_type = Schema__MGraph__Edge) + + # Add edges to graph + self.graph.add_edge(outgoing_edge) + self.graph.add_edge(incoming_edge) + + # Test outgoing edge retrieval + outgoing_edges = self.node.models__from_edges() + assert len(outgoing_edges) == 1 + assert outgoing_edges[0].edge_id() == edge_config_1.edge_id + + def test_models__to_edges(self): # Tests retrieving incoming edges + node_1_id = self.graph.new_node().node_id + # Create test edges + edge_config_1 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + outgoing_edge = Schema__MGraph__Edge (edge_config = edge_config_1, + from_node_id = self.node.node_id, + to_node_id = node_1_id, + edge_type = Schema__MGraph__Edge) + + edge_config_2 = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + incoming_edge = Schema__MGraph__Edge (edge_config = edge_config_2, + from_node_id = node_1_id, + to_node_id = self.node.node_id, + edge_type = Schema__MGraph__Edge) + + # Add edges to graph + self.graph.add_edge(outgoing_edge) + self.graph.add_edge(incoming_edge) + + # Test incoming edge retrieval + incoming_edges = self.node.models__to_edges() + assert len(incoming_edges) == 1 + assert incoming_edges[0].edge_id() == edge_config_2.edge_id + + def test_model__node_from_edge(self): # Tests retrieving connected nodes from edge + # Create another node to connect to + other_node_data = Schema__MGraph__Node__Data() + other_schema_node = Schema__MGraph__Node (node_data = other_node_data, + node_type = Schema__MGraph__Node) + other_model_node = Model__MGraph__Node (data = other_schema_node) + self.graph.add_node(other_schema_node) + + # Create edge connecting nodes + edge_config = Schema__MGraph__Edge__Config(edge_id=Random_Guid()) + edge = Schema__MGraph__Edge (edge_config = edge_config, + from_node_id = self.node.node_id, + to_node_id = other_schema_node.node_id, + edge_type = Schema__MGraph__Edge) + model_edge = Model__MGraph__Edge (data = edge) + self.graph.add_edge(edge) + + # Test getting connected node from outgoing edge + connected_node = self.node.model__node_from_edge(model_edge) + assert connected_node is not None + assert connected_node.data is other_schema_node + + # Test getting connected node from incoming edge + edge.from_node_id = other_schema_node.node_id + edge.to_node_id = self.node.node_id + connected_node = self.node.model__node_from_edge(model_edge) + assert connected_node is not None + assert connected_node.data is other_schema_node + + # Test with unconnected edge + edge.from_node_id = Random_Guid() + edge.to_node_id = Random_Guid() + assert self.node.model__node_from_edge(model_edge) is None \ No newline at end of file diff --git a/tests/unit/mgraph/domain/test_MGraph__Node.py b/tests/unit/mgraph/domain/test_MGraph__Node.py deleted file mode 100644 index ec65a65..0000000 --- a/tests/unit/mgraph/domain/test_MGraph__Node.py +++ /dev/null @@ -1,24 +0,0 @@ -from unittest import TestCase -from osbot_utils.helpers.Safe_Id import Safe_Id -from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node -from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node -from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph -from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node -from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data -from osbot_utils.helpers.Random_Guid import Random_Guid - -class test_MGraph__Node(TestCase): - - def setUp(self): # Initialize test data - self.node_data = Schema__MGraph__Node__Data() - self.schema_node = Schema__MGraph__Node (node_data = self.node_data, - node_type = Schema__MGraph__Node) - self.model_node = Model__MGraph__Node(data=self.schema_node) - self.graph = Model__MGraph__Graph(data=None) # Mock graph for testing - self.node = Domain__MGraph__Node(node=self.model_node, graph=self.graph) - - def test_init(self): # Tests basic initialization - assert type(self.node) is Domain__MGraph__Node - assert self.node.node is self.model_node - assert self.node.graph is self.graph - assert type(self.node.node_id) is Random_Guid \ No newline at end of file diff --git a/tests/unit/mgraph/models/test_Model__MGraph__Graph.py b/tests/unit/mgraph/models/test_Model__MGraph__Graph.py index 1c7c0a3..47193a5 100644 --- a/tests/unit/mgraph/models/test_Model__MGraph__Graph.py +++ b/tests/unit/mgraph/models/test_Model__MGraph__Graph.py @@ -51,15 +51,18 @@ def test_edge_operations(self): edge_id = edge.edge_id() retrieved = self.graph.edge(edge_id) # Test edge retrieval - assert isinstance(edge, Model__MGraph__Edge) is True - assert edge.from_node_id() == node_1_id - assert edge.to_node_id () == node_2_id - assert is_guid(edge_id) is True - assert retrieved.json() == edge.json() - assert self.graph.delete_edge(edge_id) is True # Test edge removal - assert self.graph.delete_edge(edge_id) is False - assert edge_id not in self.graph.data.edges - assert self.graph.delete_edge(Random_Guid()) is False # Test removing non-existent edge + assert isinstance(edge, Model__MGraph__Edge) is True + assert edge.from_node_id() == node_1_id + assert edge.to_node_id () == node_2_id + assert is_guid(edge_id) is True + assert retrieved.json() == edge.json() + assert len(self.graph.node__to_edges(node_1_id)) == 0 + assert len(self.graph.node__to_edges(node_2_id)) == 1 + assert self.graph.delete_edge(edge_id) is True # Test edge removal + assert self.graph.delete_edge(edge_id) is False + assert edge_id not in self.graph.data.edges + assert len(self.graph.node__to_edges(node_2_id)) == 0 + assert self.graph.delete_edge(Random_Guid()) is False # Test removing non-existent edge def test_node_removal_cascades_to_edges(self): # Tests that removing a node removes connected edges node_1 = self.graph.new_node() diff --git a/tests/unit/mgraph/domain/test_MGraph.py b/tests/unit/mgraph/test_MGraph.py similarity index 100% rename from tests/unit/mgraph/domain/test_MGraph.py rename to tests/unit/mgraph/test_MGraph.py diff --git a/tests/unit/mgraph/test_mgraph.py b/tests/unit/mgraph/test_mgraph.py deleted file mode 100644 index 38afda5..0000000 --- a/tests/unit/mgraph/test_mgraph.py +++ /dev/null @@ -1 +0,0 @@ -# so that pytest in Pycharm recognises this as a folder with tests \ No newline at end of file diff --git a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Edge.py b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Edge.py index 715d938..7a6470c 100644 --- a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Edge.py +++ b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Edge.py @@ -1,10 +1,8 @@ from unittest import TestCase - -from mgraph_ai.mgraph.domain.Domain__MGraph__Edge import Domain__MGraph__Edge -from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge +from mgraph_ai.mgraph.domain.Domain__MGraph__Edge import Domain__MGraph__Edge +from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Edge import Domain__MGraph__Json__Edge -from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node -from osbot_utils.utils.Objects import __, type_full_name +from osbot_utils.utils.Objects import __, type_full_name class test_Domain__MGraph__Json__Edge(TestCase): diff --git a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Graph.py b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Graph.py index ed59da4..4f32931 100644 --- a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Graph.py +++ b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Graph.py @@ -1,4 +1,3 @@ -import pytest from unittest import TestCase from osbot_utils.utils.Misc import is_guid from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node @@ -25,7 +24,6 @@ class test_Domain__MGraph__Json__Graph(TestCase): @classmethod def setUpClass(cls): - pytest.skip("todo: fix tests") cls.graph = Domain__MGraph__Json__Graph() def test__init__(self): @@ -63,46 +61,57 @@ def test_new_edge(self): assert _.to_node_id () == node_2.node_id - def test_root_node(self): # Test basic root node behavior - assert self.graph.model.data.graph_data.root_id is None # Initially no root + def test_root_node(self): # Test comprehensive root node behavior and constraints + with Domain__MGraph__Json__Graph() as _: + assert type(_) is Domain__MGraph__Json__Graph # Verify correct graph type + assert _.model.data.graph_data.root_id is None # Verify no root exists initially + assert _.nodes_ids() == [] # Verify graph starts empty + assert _.root_content() is None # Verify no content attached to root - # Get root creates basic node - root = self.graph.root() - root_id = root.node_id - assert is_guid(root_id) - assert self.graph.nodes_ids() - assert isinstance(root, Domain__MGraph__Json__Node) - assert base_classes(root) == [Domain__MGraph__Node, Type_Safe, object ] - assert self.graph.model.data.graph_data.root_id == root.node_id + root = _.root() # Create root node on first access + root_id = root.node_id # Store root ID for validations - # Initially no content - assert self.graph.root_content() is None + assert is_guid(root_id) is True # Validate root has proper GUID + assert _.nodes_ids() == [root_id] # Verify root added to graph nodes + assert isinstance(root, Domain__MGraph__Json__Node) is True # Confirm correct root node type + assert base_classes(root) == [Domain__MGraph__Node, Type_Safe, object] # Validate inheritance chain + assert _.model.data.graph_data.root_id == root.node_id # Verify root ID properly stored + + root_2 = _.root() # Request root node again + assert root_2.node_id == root_id # Verify same root returned + assert len(_.model.node__to_edges(root_id)) == 0 # Verify no incoming edges allowed + assert len(_.model.node__from_edges(root_id)) <= 1 # Verify maximum one outgoing edge + + # Initially no content + assert _.root_content() is None # confirm that it has no content def test_root_with_value(self): # Test root with value content - test_value = "test_string" - content = self.graph.set_root_content(test_value) + test_value = "test_string" # new root value + content = self.graph.set_root_content(test_value) # assign it to root - assert isinstance(content, Domain__MGraph__Json__Node__Value) + assert type(content) is Domain__MGraph__Json__Node__Value # confirm that type is a Node__Value assert content.value == test_value - # Verify through root_content - root_content = self.graph.root_content() - assert isinstance(root_content, Domain__MGraph__Json__Node__Value) - assert root_content.node_id == content.node_id - assert root_content.value == test_value + root_content = self.graph.root_content() # get the content node again + + assert isinstance(root_content, Domain__MGraph__Json__Node__Value) # confirm type + assert root_content.node_id == content.node_id # confirm is the same as the created in the previous step + assert root_content.value == test_value + assert self.graph.delete_node(content.node_id) is True # delete root content + assert self.graph.root_content() is None # confirm it is not there def test_root_with_list(self): # Test root with list content test_list = [1, "two", True] content = self.graph.set_root_content(test_list) assert isinstance(content, Domain__MGraph__Json__Node__List) + assert content.items() == test_list - # Verify through root_content - root_content = self.graph.root_content() - assert isinstance(root_content, Domain__MGraph__Json__Node__List) - assert root_content.node_id == content.node_id - assert root_content.items() == test_list + root_content = self.graph.root_content() # Verify through root_content + assert type(root_content) is Domain__MGraph__Json__Node__List + assert root_content.node_id == content.node_id + assert root_content.items() == test_list def test_root_with_dict(self): # Test root with dict content test_dict = {"key1": "value1", "key2": 42} @@ -114,7 +123,7 @@ def test_root_with_dict(self): # Verify through root_content root_content = self.graph.root_content() assert isinstance(root_content, Domain__MGraph__Json__Node__Dict) - assert root_content.node_id == content.node_id + assert root_content.node_id == content.node_id assert root_content.properties() == test_dict def test_change_root_content(self): # Test changing root content diff --git a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__List.py b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__List.py index fcda250..bef850e 100644 --- a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__List.py +++ b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__List.py @@ -1,4 +1,3 @@ -import pytest from unittest import TestCase from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Graph import Domain__MGraph__Json__Graph from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List @@ -10,11 +9,20 @@ class test_Domain__MGraph__Json__Node__List(TestCase): @classmethod def setUpClass(cls): - pytest.skip("todo: fix tests") + cls.domain_graph = Domain__MGraph__Json__Graph() + cls.domain_node_list = cls.domain_graph.new_list_node() + cls.list_node_id = cls.domain_node_list.node_id - def setUp(self): # Initialize test data - self.domain_graph = Domain__MGraph__Json__Graph() - self.domain_node_list = Domain__MGraph__Json__Node__List() + def tearDown(self): + with self.domain_node_list as _: + assert _.graph.nodes_ids() == [self.list_node_id] # confirm that all list nodes + assert _.graph.edges_ids() == [] # and edges have been deleted + + def remove_all_nodes(self): # BUG: we shouldn't need to delete the nodes like htis + with self.domain_node_list as _: + for node_id in _.graph.nodes_ids(): + if node_id != self.list_node_id: + assert _.graph.delete_node(node_id) is True def test__init__(self): # Test basic initialization with self.domain_node_list as _: @@ -27,52 +35,69 @@ def test__init__(self): # Test basi def test_add_primitive_values(self): # Test adding primitive values with self.domain_node_list as _: - assert _.graph.nodes_ids() == [] + assert _.graph.nodes_ids() == [self.list_node_id] assert _.graph.edges_ids() == [] _.add("test_string") - #pprint(_.graph.json()) - return - _.add(42) - _.add(True) - _.add(None) - - items = _.items() - assert len(items) == 4 - assert items == ["test_string", 42, True, None] + _.add(42 ) + _.add(True ) + _.add(None ) + assert _.items() == ["test_string", 42, True, None] # confirm 4 items added are there + assert len(_.graph.nodes_ids()) == 5 # there should be 4 nodes + the list node + assert len(_.graph.edges_ids()) == 4 # and 4 edges (one to each list value) + _.clear() # clear list + assert _.items() == [] # confirm list is empty + assert _.graph.nodes_ids() == [self.list_node_id] # and that all nodes have been deleted + assert _.graph.edges_ids() == [] # and that all edges have been deleted - def test_add_dict(self): # Test adding dictionary + def test_add_dict(self): # Test adding dictionary with self.domain_node_list as _: test_dict = {"key1": "value1", "key2": 42} + assert len(_.graph.nodes_ids()) == 1 # initial we have 1 node + assert len(_.graph.edges_ids()) == 0 # and 0 nodes _.add(test_dict) - - items = _.items() - assert len(items) == 1 - assert items[0] == test_dict + assert _.items() == [test_dict] + assert len(_.graph.nodes_ids()) == 6 # now we have 5 more nodes (one for the dict and two per the 2 properties) + assert len(_.graph.edges_ids()) == 5 # and 5 edges (one to the dict and two per the 2 properties) + _.clear() # clear list + assert _.items() == [] # confirm list is empty + assert len(_.graph.nodes_ids()) == 5 # BUG: this should be 1 (the property values are not being deleted) + assert len(_.graph.edges_ids()) == 2 # BUG: this should be 0 (the property edges are not being deleted + + self.remove_all_nodes() # BUG: we shouldn't need to delete the nodes like htis def test_add_nested_list(self): # Test adding nested list with self.domain_node_list as _: test_list = [1, 2, ["a", "b"]] _.add(test_list) - - items = _.items() - assert len(items) == 1 - assert items[0] == [1, 2, ["a", "b"]] + + assert _.items() == [[1, 2, ["a", "b"]]] + _.clear() + assert _.items() == [] + + self.remove_all_nodes() # BUG: we shouldn't need to delete the nodes like htis + def test_complex_structure(self): # Test complex nested structure with self.domain_node_list as _: - complex_data = [ - "string", - {"key": "value"}, - [1, 2, 3], - {"nested": {"a": 1, "b": [True, False]}} - ] + complex_data = ["string" , + { "key" : "value"} , + [ 1, 2, 3] , + { "nested": { "a" : 1 , + "b" : [True, False] } }] for item in complex_data: _.add(item) items = _.items() - assert len(items) == 4 - assert items == complex_data + assert len(items) == 4 + assert items == complex_data + assert len(_.graph.nodes_ids()) == 12 # we should have 12 nodes + assert len(_.graph.edges_ids()) == 11 # and 11 edges + + _.clear() + assert _.items() == [] + + self.remove_all_nodes() # BUG: we shouldn't need to delete the nodes like htis def test_clear(self): # Test clearing all items with self.domain_node_list as _: @@ -84,6 +109,8 @@ def test_clear(self): # Test cleari _.clear() assert len(_.items()) == 0 + self.remove_all_nodes() # BUG: we shouldn't need to delete the nodes like htis + def test_extend(self): # Test extending with multiple items with self.domain_node_list as _: items = ["string", 42, {"key": "value"}, [1, 2, 3]] @@ -91,16 +118,22 @@ def test_extend(self): # Test extend assert _.items() == items + self.remove_all_nodes() # BUG: we shouldn't need to delete the nodes like htis + def test_remove(self): # Test removing items with self.domain_node_list as _: _.add("value1") _.add("value2") _.add("value1") # Duplicate value - assert _.remove("value1") is True + assert _.remove("value1") is True items = _.items() - assert len(items) == 2 + assert len(items) == 2 assert items.count("value1") == 1 + assert items == ['value2', 'value1'] # Try removing non-existent value - assert _.remove("non_existent") is False \ No newline at end of file + assert _.remove("non_existent") is False + + _.clear() + assert _.items() == [] \ No newline at end of file diff --git a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__Property.py b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__Property.py index 69418e8..4f6edbb 100644 --- a/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__Property.py +++ b/tests/unit/providers/json/domain/test_Domain__MGraph__Json__Node__Property.py @@ -1,17 +1,29 @@ -from unittest import TestCase - -import pytest +from unittest import TestCase +from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node +from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Property import Schema__MGraph__Json__Node__Property +from osbot_utils.type_safe.Type_Safe import Type_Safe +from osbot_utils.utils.Objects import __, type_full_name, base_types +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node +from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Property import Domain__MGraph__Json__Node__Property class test_Domain__MGraph__Json__Node__Property(TestCase): - def setUp(self): - pytest.skip("todo: fix test")# Initialize test data + @classmethod + def setUpClass(cls): # Initialize test data + cls.domain_node_property = Domain__MGraph__Json__Node__Property() + def test__init__(self): # Test basic initialization + with self.domain_node_property as _: + assert type(_) is Domain__MGraph__Json__Node__Property + assert base_types(_) == [Domain__MGraph__Json__Node, Domain__MGraph__Node, Type_Safe, object] + assert isinstance(_, Domain__MGraph__Json__Node) is True + assert _.obj() == __(node = __(data=__(node_data = __(name='') , + node_id = _.node_id , + node_type = type_full_name(Schema__MGraph__Json__Node__Property))), + graph = _.graph.obj()) def test_property_name(self): # Test property name handling property_name = "test_property" - node = self.graph.new_node(name=property_name) - - assert isinstance(node, Domain__MGraph__Json__Node__Property) + node = Domain__MGraph__Json__Node__Property(name=property_name) assert node.name == property_name \ No newline at end of file