comparison env/lib/python3.9/site-packages/networkx/drawing/tests/test_pydot.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for pydot drawing functions."""
2 from io import StringIO
3 import tempfile
4 import networkx as nx
5 from networkx.testing import assert_graphs_equal
6
7 import pytest
8
9 pydot = pytest.importorskip("pydot")
10
11
12 class TestPydot:
13 def pydot_checks(self, G, prog):
14 """
15 Validate :mod:`pydot`-based usage of the passed NetworkX graph with the
16 passed basename of an external GraphViz command (e.g., `dot`, `neato`).
17 """
18
19 # Set the name of this graph to... "G". Failing to do so will
20 # subsequently trip an assertion expecting this name.
21 G.graph["name"] = "G"
22
23 # Add arbitrary nodes and edges to the passed empty graph.
24 G.add_edges_from([("A", "B"), ("A", "C"), ("B", "C"), ("A", "D")])
25 G.add_node("E")
26
27 # Validate layout of this graph with the passed GraphViz command.
28 graph_layout = nx.nx_pydot.pydot_layout(G, prog=prog)
29 assert isinstance(graph_layout, dict)
30
31 # Convert this graph into a "pydot.Dot" instance.
32 P = nx.nx_pydot.to_pydot(G)
33
34 # Convert this "pydot.Dot" instance back into a graph of the same type.
35 G2 = G.__class__(nx.nx_pydot.from_pydot(P))
36
37 # Validate the original and resulting graphs to be the same.
38 assert_graphs_equal(G, G2)
39
40 # Serialize this "pydot.Dot" instance to a temporary file in dot format
41 fname = tempfile.mktemp()
42 P.write_raw(fname)
43
44 # Deserialize a list of new "pydot.Dot" instances back from this file.
45 Pin_list = pydot.graph_from_dot_file(path=fname, encoding="utf-8")
46
47 # Validate this file to contain only one graph.
48 assert len(Pin_list) == 1
49
50 # The single "pydot.Dot" instance deserialized from this file.
51 Pin = Pin_list[0]
52
53 # Sorted list of all nodes in the original "pydot.Dot" instance.
54 n1 = sorted([p.get_name() for p in P.get_node_list()])
55
56 # Sorted list of all nodes in the deserialized "pydot.Dot" instance.
57 n2 = sorted([p.get_name() for p in Pin.get_node_list()])
58
59 # Validate these instances to contain the same nodes.
60 assert n1 == n2
61
62 # Sorted list of all edges in the original "pydot.Dot" instance.
63 e1 = sorted([(e.get_source(), e.get_destination()) for e in P.get_edge_list()])
64
65 # Sorted list of all edges in the original "pydot.Dot" instance.
66 e2 = sorted(
67 [(e.get_source(), e.get_destination()) for e in Pin.get_edge_list()]
68 )
69
70 # Validate these instances to contain the same edges.
71 assert e1 == e2
72
73 # Deserialize a new graph of the same type back from this file.
74 Hin = nx.nx_pydot.read_dot(fname)
75 Hin = G.__class__(Hin)
76
77 # Validate the original and resulting graphs to be the same.
78 assert_graphs_equal(G, Hin)
79
80 def test_undirected(self):
81 self.pydot_checks(nx.Graph(), prog="neato")
82
83 def test_directed(self):
84 self.pydot_checks(nx.DiGraph(), prog="dot")
85
86 def test_read_write(self):
87 G = nx.MultiGraph()
88 G.graph["name"] = "G"
89 G.add_edge("1", "2", key="0") # read assumes strings
90 fh = StringIO()
91 nx.nx_pydot.write_dot(G, fh)
92 fh.seek(0)
93 H = nx.nx_pydot.read_dot(fh)
94 assert_graphs_equal(G, H)