def test_graph_id(): first = GraphNode(content='n1') second = GraphNode(content='n2', nodes_from=[first]) third = GraphNode(content='n3', nodes_from=[first]) final = GraphNode(content='n4', nodes_from=[second, third]) assert final.descriptive_id == ('((/n1;)/' 'n2;;(/' 'n1;)/' 'n3;)/' 'n4')
def test_pipeline_repr(): first = GraphNode(content='n1') second = GraphNode(content='n2') third = GraphNode(content='n3') final = GraphNode(content='n4', nodes_from=[first, second, third]) graph = Graph(final) expected_graph_description = "{'depth': 2, 'length': 4, 'nodes': [n4, n1, n2, n3]}" assert repr(graph) == expected_graph_description
def connect_nodes(self, parent: GraphNode, child: GraphNode): if child.descriptive_id not in [ p.descriptive_id for p in parent.ordered_subnodes_hierarchy() ]: if child.nodes_from: # if not already connected child.nodes_from.append(parent) else: # add parent to initial node new_child = GraphNode(nodes_from=[], content=child.content) new_child.nodes_from.append(parent) self.update_node(child, new_child)
def test_graph_str(): # given first = GraphNode(content='n1') second = GraphNode(content='n2') third = GraphNode(content='n3') final = GraphNode(content='n4', nodes_from=[first, second, third]) graph = Graph(final) expected_graph_description = "{'depth': 2, 'length': 4, 'nodes': [n4, n1, n2, n3]}" # when actual_graph_description = str(graph) # then assert actual_graph_description == expected_graph_description
def test_delete_primary_node(): # given first = GraphNode(content='n1') second = GraphNode(content='n2') third = GraphNode(content='n3', nodes_from=[first]) final = GraphNode(content='n4', nodes_from=[second, third]) graph = Graph(final) # when graph.delete_node(first) new_primary_node = [node for node in graph.nodes if node.content == 'n2'][0] # then assert len(graph.nodes) == 3 assert isinstance(new_primary_node, GraphNode)
def update_node(self, old_node: GraphNode, new_node: GraphNode): self.actualise_old_node_children(old_node, new_node) if ((new_node.nodes_from is None and old_node.nodes_from is None) or (new_node.nodes_from is not None and old_node.nodes_from is not None)): new_node.nodes_from = old_node.nodes_from self._graph.nodes.remove(old_node) self._graph.nodes.append(new_node) self.sort_nodes()
def test_pipeline_deepcopy(): pipeline = Graph(GraphNode(content='n1')) pipeline_copy = deepcopy(pipeline) assert pipeline.uid != pipeline_copy.uid
def delete_subtree(self, node: GraphNode): """Delete node with all the parents it has""" for node_child in self.node_children(node): node_child.nodes_from.remove(node) for subtree_node in node.ordered_subnodes_hierarchy(): self._graph.nodes.remove(subtree_node)