def test_baseresource_chksum(): g = Graph() a = SomeTestResource("a", {}) with pytest.raises(RuntimeError): a.chksum g.add_node(a) assert isinstance(a.chksum, str)
def test_graph(): g = Graph() n1 = SomeTestResource("foo", {}) n2 = SomeTestResource("bar", {}) g.add_node(n1) g.add_node(n2) g.add_edge(n1, n2) assert len(g.nodes) == 2 assert len(g.edges) == 1
def test_graph_container(): gc = GraphContainer(cache_graph=False) g = Graph() n1 = SomeTestResource("foo", {}) n2 = SomeTestResource("bar", {}) g.add_node(n1) gc.graph.add_resource(gc.GRAPH_ROOT, n2) gc.add(g) gc.graph.add_edge(n1, n2) assert len(gc.graph.nodes) == 3 assert len(gc.graph.edges) == 2 assert gc.graph.search_first("id", "bar") == n2 assert gc.graph.search_first_parent_class(n2, SomeTestResource) == n1
def process_data_line(data: dict, graph: Graph): """Process a single line of resotocore graph data""" if data.get("type") == "node": node_id = data.get("id") node = node_from_dict(data) node_mapping[node_id] = node log.debug(f"Adding node {node} to the graph") graph.add_node(node) if node.kind == "graph_root": log.debug(f"Setting graph root {node}") graph.root = node elif data.get("type") == "edge": node_from = data.get("from") node_to = data.get("to") edge_type = EdgeType.from_value(data.get("edge_type")) if node_from not in node_mapping or node_to not in node_mapping: raise ValueError(f"One of {node_from} -> {node_to} unknown") graph.add_edge( node_mapping[node_from], node_mapping[node_to], edge_type=edge_type )
def test_graph_merge(): rg1 = Graph() rg2 = Graph() a = SomeTestResource("a", {}) b = SomeTestResource("b", {}) c = SomeTestResource("c", {}) d = SomeTestResource("d", {}) rg1.add_node(a) rg1.add_node(b) rg2.add_node(c) rg2.add_node(d) rg1.add_edge(a, b, edge_type=EdgeType.delete) rg2.add_edge(c, d, edge_type=EdgeType.delete) rg1.merge(rg2) assert len(rg1.nodes) == 4 assert len(rg1.edges) == 2 for edge in rg1.edges: assert len(edge) == 3 key = edge[2] assert len(key) == 3 edge_type = key[2] assert edge_type == EdgeType.delete