def test_predecessors(self): g = Graph() g.add_edge(edge("A", "B")) assert g.predecessors(node("A")) == [] assert g.predecessors(node("B")) == [node("A")] assert g.predecessors(node("C")) == []
def test_remove_cluster(self): # Graph所有のクラスタ削除 g = Graph() g.add_edge(edge("A", "B")) g.add_cluster(cluster("pkg", {"A"})) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A"], "clusters": {} } }, } g.remove_cluster(path("pkg")) assert g.to_dict() == { "nodes": ["B"], "edges": [], "clusters": {}, } # 冪等なこと g.remove_cluster(path("pkg")) assert g.to_dict() == { "nodes": ["B"], "edges": [], "clusters": {}, }
def test_focus__node_inside_a_package(self): g = Graph() g.add_edge(edge("A", "B")) g.add_cluster(cluster("pkg", {"A"})) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A"], "clusters": {} } }, } g.focus_nodes(node("A")) assert g.to_dict() == { "nodes": ["A"], "edges": [], "clusters": { "pkg": { "nodes": ["A"], "clusters": {} } }, }
def test_add_cluster(self): g = Graph() g.add_edge(edge("A", "B")) g.add_cluster(cluster("pkg", {"A", "B"})) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A", "B"], "clusters": {} } }, } g.clusters[path("pkg")].add_cluster(cluster("pkg.child", {"C"})) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A", "B"], "clusters": { "pkg.child": { "clusters": {}, "nodes": ["C"] } }, } }, } # 親グラフが持っていないノードを含むクラスタが追加されたら、 # 親グラフに含まれないノードを親グラフのノード管理に含める g.add_cluster(cluster("pkg_x", {"X"})) assert g.to_dict() == { "nodes": ["A", "B", "X"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A", "B"], "clusters": { "pkg.child": { "clusters": {}, "nodes": ["C"] } }, }, "pkg_x": { "nodes": ["X"], "clusters": {} }, }, }
def sample(cls) -> "GraphController": master_graph = MasterGraph(edges=ModuleEdgeCollection([ ModuleEdge.from_str("A.A", "B.B"), ModuleEdge.from_str("X.A", "Y.B") ])) g = Graph(master_graph=master_graph) g.add_edge(ModuleEdge.from_str("A", "B")) g.add_edge(ModuleEdge.from_str("X", "Y")) return cls(g)
def test_add_edge(self): g = Graph() g.add_edge(edge("A", "B")) expected = {"nodes": ["A", "B"], "edges": [("A", "B")], "clusters": {}} assert g.to_dict() == expected g.add_edge(edge("A", "B")) assert g.to_dict() == expected
def test_remove_node(self): g = Graph() g.add_edge(edge("A", "B")) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": {}, } g.remove_node(node("A")) assert g.to_dict() == {"nodes": ["B"], "edges": [], "clusters": {}}
def test_list_all_nodes(self): g = Graph() g.add_edge(edge("A", "B")) g.add_edge(edge("A", "C")) cluster_a = cluster("pkg", {"A"}) cluster_b = cluster("pkg.xxx", {"B"}) cluster_a.add_cluster(cluster_b) g.add_cluster(cluster_a) assert g.list_all_nodes() == [ node("A"), node("B"), node("C"), ]
def test_list_all_modules(self): g = Graph() g.add_edge(edge("A", "B")) cluster_a = cluster("pkg", {"A"}) cluster_b = cluster("pkg.xxx", {"B"}) cluster_a.add_cluster(cluster_b) g.add_cluster(cluster_a) assert g.list_all_modules() == [ path("A"), path("B"), path("pkg"), path("pkg.xxx"), ]
def test_focus_nodes_and_clusters(self): g = Graph() g.add_edge(edge("A", "B")) g.add_edge(edge("C", "D")) cluster_a = cluster("pkg_a", {"A"}) cluster_b = cluster("pkg_b", {"B"}) cluster_a.add_cluster(cluster_b) g.add_cluster(cluster_a) assert g.to_dict() == { "nodes": ["A", "B", "C", "D"], "edges": [("A", "B"), ("C", "D")], "clusters": { "pkg_a": { "clusters": { "pkg_b": { "clusters": {}, "nodes": ["B"] } }, "nodes": ["A"], } }, } g.focus_nodes_and_clusters(node("pkg_b"), node("C")) assert g.to_dict() == { "nodes": ["B", "C"], "edges": [], "clusters": { "pkg_a": { "clusters": { "pkg_b": { "clusters": {}, "nodes": ["B"] } }, "nodes": [], } }, }
def test_remove_node_from_cluster(self): g = Graph() g.add_edge(edge("A", "B")) g.add_cluster(cluster("pkg", {"A"})) assert g.to_dict() == { "nodes": ["A", "B"], "edges": [("A", "B")], "clusters": { "pkg": { "nodes": ["A"], "clusters": {} } }, } g.remove_node(node("A")) assert g.to_dict() == { "nodes": ["B"], "edges": [], "clusters": {}, }
def test_render(self): cluster = Cluster(module_path=path("jig"), children={node("jig.analyzer")}) sub_cluster = Cluster( module_path=path("jig.collector"), children={node("jig.collector.application"), node("jig.collector.domain")}, ) cluster.add_cluster(sub_cluster) graph = Graph() graph.add_edge(edge("jig.analyzer", "jig.collector")) graph.add_cluster(cluster) renderer = GraphRenderer(graph=graph) g = Digraph() node_style = NodeStyle().to_dict() g.node("jig.analyzer", **node_style) g.node("jig.collector", **node_style) edge_style = EdgeStyle().to_dict() g.edge("jig.analyzer", "jig.collector", **edge_style) child = Digraph(name="cluster_jig") child.attr(label="jig") child.node("jig.analyzer") grandchild = Digraph(name="cluster_jig.collector") grandchild.attr(label="jig.collector") grandchild.node("jig.collector.application") grandchild.node("jig.collector.domain") child.subgraph(grandchild) g.subgraph(child) assert str(renderer.render()) == str(g)
def test_focus__node_not_found(self): g = Graph() g.add_edge(edge("A", "B")) with pytest.raises(Exception): g.focus_nodes(node("C"))