File size: 6,242 Bytes
b200bda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Unit tests for pydot drawing functions."""
import os
import tempfile
from io import StringIO

import pytest

import networkx as nx
from networkx.utils import graphs_equal

pydot = pytest.importorskip("pydot")


@pytest.mark.xfail
class TestPydot:
    def pydot_checks(self, G, prog):
        """
        Validate :mod:`pydot`-based usage of the passed NetworkX graph with the
        passed basename of an external GraphViz command (e.g., `dot`, `neato`).
        """

        # Set the name of this graph to... "G". Failing to do so will
        # subsequently trip an assertion expecting this name.
        G.graph["name"] = "G"

        # Add arbitrary nodes and edges to the passed empty graph.
        G.add_edges_from([("A", "B"), ("A", "C"), ("B", "C"), ("A", "D")])
        G.add_node("E")

        # Validate layout of this graph with the passed GraphViz command.
        graph_layout = nx.nx_pydot.pydot_layout(G, prog=prog)
        assert isinstance(graph_layout, dict)

        # Convert this graph into a "pydot.Dot" instance.
        P = nx.nx_pydot.to_pydot(G)

        # Convert this "pydot.Dot" instance back into a graph of the same type.
        G2 = G.__class__(nx.nx_pydot.from_pydot(P))

        # Validate the original and resulting graphs to be the same.
        assert graphs_equal(G, G2)

        fd, fname = tempfile.mkstemp()

        # Serialize this "pydot.Dot" instance to a temporary file in dot format
        P.write_raw(fname)

        # Deserialize a list of new "pydot.Dot" instances back from this file.
        Pin_list = pydot.graph_from_dot_file(path=fname, encoding="utf-8")

        # Validate this file to contain only one graph.
        assert len(Pin_list) == 1

        # The single "pydot.Dot" instance deserialized from this file.
        Pin = Pin_list[0]

        # Sorted list of all nodes in the original "pydot.Dot" instance.
        n1 = sorted(p.get_name() for p in P.get_node_list())

        # Sorted list of all nodes in the deserialized "pydot.Dot" instance.
        n2 = sorted(p.get_name() for p in Pin.get_node_list())

        # Validate these instances to contain the same nodes.
        assert n1 == n2

        # Sorted list of all edges in the original "pydot.Dot" instance.
        e1 = sorted((e.get_source(), e.get_destination()) for e in P.get_edge_list())

        # Sorted list of all edges in the original "pydot.Dot" instance.
        e2 = sorted((e.get_source(), e.get_destination()) for e in Pin.get_edge_list())

        # Validate these instances to contain the same edges.
        assert e1 == e2

        # Deserialize a new graph of the same type back from this file.
        Hin = nx.nx_pydot.read_dot(fname)
        Hin = G.__class__(Hin)

        # Validate the original and resulting graphs to be the same.
        assert graphs_equal(G, Hin)

        os.close(fd)
        os.unlink(fname)

    def test_undirected(self):
        self.pydot_checks(nx.Graph(), prog="neato")

    def test_directed(self):
        self.pydot_checks(nx.DiGraph(), prog="dot")

    def test_read_write(self):
        G = nx.MultiGraph()
        G.graph["name"] = "G"
        G.add_edge("1", "2", key="0")  # read assumes strings
        fh = StringIO()
        nx.nx_pydot.write_dot(G, fh)
        fh.seek(0)
        H = nx.nx_pydot.read_dot(fh)
        assert graphs_equal(G, H)


def test_pydot_issue_258():
    G = nx.Graph([("Example:A", 1)])
    with pytest.raises(ValueError):
        nx.nx_pydot.to_pydot(G)
    with pytest.raises(ValueError):
        nx.nx_pydot.pydot_layout(G)

    G = nx.Graph()
    G.add_node("1.2", style="filled", fillcolor="red:yellow")
    with pytest.raises(ValueError):
        nx.nx_pydot.to_pydot(G)
    G.remove_node("1.2")
    G.add_node("1.2", style="filled", fillcolor='"red:yellow"')
    assert (
        G.nodes.data() == nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G)).nodes.data()
    )

    G = nx.DiGraph()
    G.add_edge("1", "2", foo="bar:1")
    with pytest.raises(ValueError):
        nx.nx_pydot.to_pydot(G)
    G = nx.DiGraph()
    G.add_edge("1", "2", foo='"bar:1"')
    assert G["1"]["2"] == nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G))["1"]["2"]

    G = nx.MultiGraph()
    G.add_edge("1", "2", foo="b:1")
    G.add_edge("1", "2", bar="foo:foo")
    with pytest.raises(ValueError):
        nx.nx_pydot.to_pydot(G)
    G = nx.MultiGraph()
    G.add_edge("1", "2", foo='"b:1"')
    G.add_edge("1", "2", bar='"foo:foo"')
    # Keys as integers aren't preserved in the conversion. They are read as strings.
    assert [attr for _, _, attr in G.edges.data()] == [
        attr
        for _, _, attr in nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G)).edges.data()
    ]

    G = nx.Graph()
    G.add_edge("1", "2")
    G["1"]["2"]["f:oo"] = "bar"
    with pytest.raises(ValueError):
        nx.nx_pydot.to_pydot(G)
    G = nx.Graph()
    G.add_edge("1", "2")
    G["1"]["2"]['"f:oo"'] = "bar"
    assert G["1"]["2"] == nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G))["1"]["2"]

    G = nx.Graph([('"Example:A"', 1)])
    layout = nx.nx_pydot.pydot_layout(G)
    assert isinstance(layout, dict)


@pytest.mark.parametrize(
    "graph_type", [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph]
)
def test_hashable_pydot(graph_type):
    # gh-5790
    G = graph_type()
    G.add_edge("5", frozenset([1]), t='"Example:A"', l=False)
    G.add_edge("1", 2, w=True, t=("node1",), l=frozenset(["node1"]))
    G.add_edge("node", (3, 3), w="string")

    assert [
        {"t": '"Example:A"', "l": "False"},
        {"w": "True", "t": "('node1',)", "l": "frozenset({'node1'})"},
        {"w": "string"},
    ] == [
        attr
        for _, _, attr in nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G)).edges.data()
    ]

    assert {str(i) for i in G.nodes()} == set(
        nx.nx_pydot.from_pydot(nx.nx_pydot.to_pydot(G)).nodes
    )


def test_pydot_numerical_name():
    G = nx.Graph()
    G.add_edges_from([("A", "B"), (0, 1)])
    graph_layout = nx.nx_pydot.pydot_layout(G, prog="dot")
    assert isinstance(graph_layout, dict)
    assert "0" not in graph_layout
    assert 0 in graph_layout
    assert "1" not in graph_layout
    assert 1 in graph_layout
    assert "A" in graph_layout
    assert "B" in graph_layout