comparison env/lib/python3.9/site-packages/networkx/readwrite/tests/test_adjlist.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 """
2 Unit tests for adjlist.
3 """
4 import io
5 import pytest
6 import os
7 import tempfile
8 import networkx as nx
9 from networkx.testing import assert_nodes_equal, assert_edges_equal, assert_graphs_equal
10
11
12 class TestAdjlist:
13 @classmethod
14 def setup_class(cls):
15 cls.G = nx.Graph(name="test")
16 e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
17 cls.G.add_edges_from(e)
18 cls.G.add_node("g")
19 cls.DG = nx.DiGraph(cls.G)
20 cls.XG = nx.MultiGraph()
21 cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
22 cls.XDG = nx.MultiDiGraph(cls.XG)
23
24 def test_read_multiline_adjlist_1(self):
25 # Unit test for https://networkx.lanl.gov/trac/ticket/252
26 s = b"""# comment line
27 1 2
28 # comment line
29 2
30 3
31 """
32 bytesIO = io.BytesIO(s)
33 G = nx.read_multiline_adjlist(bytesIO)
34 adj = {"1": {"3": {}, "2": {}}, "3": {"1": {}}, "2": {"1": {}}}
35 assert_graphs_equal(G, nx.Graph(adj))
36
37 def test_unicode(self):
38 G = nx.Graph()
39 name1 = chr(2344) + chr(123) + chr(6543)
40 name2 = chr(5543) + chr(1543) + chr(324)
41 G.add_edge(name1, "Radiohead", **{name2: 3})
42 fd, fname = tempfile.mkstemp()
43 nx.write_multiline_adjlist(G, fname)
44 H = nx.read_multiline_adjlist(fname)
45 assert_graphs_equal(G, H)
46 os.close(fd)
47 os.unlink(fname)
48
49 def test_latin1_err(self):
50 G = nx.Graph()
51 name1 = chr(2344) + chr(123) + chr(6543)
52 name2 = chr(5543) + chr(1543) + chr(324)
53 G.add_edge(name1, "Radiohead", **{name2: 3})
54 fd, fname = tempfile.mkstemp()
55 pytest.raises(
56 UnicodeEncodeError, nx.write_multiline_adjlist, G, fname, encoding="latin-1"
57 )
58 os.close(fd)
59 os.unlink(fname)
60
61 def test_latin1(self):
62 G = nx.Graph()
63 name1 = "Bj" + chr(246) + "rk"
64 name2 = chr(220) + "ber"
65 G.add_edge(name1, "Radiohead", **{name2: 3})
66 fd, fname = tempfile.mkstemp()
67 nx.write_multiline_adjlist(G, fname, encoding="latin-1")
68 H = nx.read_multiline_adjlist(fname, encoding="latin-1")
69 assert_graphs_equal(G, H)
70 os.close(fd)
71 os.unlink(fname)
72
73 def test_parse_adjlist(self):
74 lines = ["1 2 5", "2 3 4", "3 5", "4", "5"]
75 nx.parse_adjlist(lines, nodetype=int) # smoke test
76 with pytest.raises(TypeError):
77 nx.parse_adjlist(lines, nodetype="int")
78 lines = ["1 2 5", "2 b", "c"]
79 with pytest.raises(TypeError):
80 nx.parse_adjlist(lines, nodetype=int)
81
82 def test_adjlist_graph(self):
83 G = self.G
84 (fd, fname) = tempfile.mkstemp()
85 nx.write_adjlist(G, fname)
86 H = nx.read_adjlist(fname)
87 H2 = nx.read_adjlist(fname)
88 assert H != H2 # they should be different graphs
89 assert_nodes_equal(list(H), list(G))
90 assert_edges_equal(list(H.edges()), list(G.edges()))
91 os.close(fd)
92 os.unlink(fname)
93
94 def test_adjlist_digraph(self):
95 G = self.DG
96 (fd, fname) = tempfile.mkstemp()
97 nx.write_adjlist(G, fname)
98 H = nx.read_adjlist(fname, create_using=nx.DiGraph())
99 H2 = nx.read_adjlist(fname, create_using=nx.DiGraph())
100 assert H != H2 # they should be different graphs
101 assert_nodes_equal(list(H), list(G))
102 assert_edges_equal(list(H.edges()), list(G.edges()))
103 os.close(fd)
104 os.unlink(fname)
105
106 def test_adjlist_integers(self):
107 (fd, fname) = tempfile.mkstemp()
108 G = nx.convert_node_labels_to_integers(self.G)
109 nx.write_adjlist(G, fname)
110 H = nx.read_adjlist(fname, nodetype=int)
111 H2 = nx.read_adjlist(fname, nodetype=int)
112 assert H != H2 # they should be different graphs
113 assert_nodes_equal(list(H), list(G))
114 assert_edges_equal(list(H.edges()), list(G.edges()))
115 os.close(fd)
116 os.unlink(fname)
117
118 def test_adjlist_multigraph(self):
119 G = self.XG
120 (fd, fname) = tempfile.mkstemp()
121 nx.write_adjlist(G, fname)
122 H = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
123 H2 = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
124 assert H != H2 # they should be different graphs
125 assert_nodes_equal(list(H), list(G))
126 assert_edges_equal(list(H.edges()), list(G.edges()))
127 os.close(fd)
128 os.unlink(fname)
129
130 def test_adjlist_multidigraph(self):
131 G = self.XDG
132 (fd, fname) = tempfile.mkstemp()
133 nx.write_adjlist(G, fname)
134 H = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiDiGraph())
135 H2 = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiDiGraph())
136 assert H != H2 # they should be different graphs
137 assert_nodes_equal(list(H), list(G))
138 assert_edges_equal(list(H.edges()), list(G.edges()))
139 os.close(fd)
140 os.unlink(fname)
141
142 def test_adjlist_delimiter(self):
143 fh = io.BytesIO()
144 G = nx.path_graph(3)
145 nx.write_adjlist(G, fh, delimiter=":")
146 fh.seek(0)
147 H = nx.read_adjlist(fh, nodetype=int, delimiter=":")
148 assert_nodes_equal(list(H), list(G))
149 assert_edges_equal(list(H.edges()), list(G.edges()))
150
151
152 class TestMultilineAdjlist:
153 @classmethod
154 def setup_class(cls):
155 cls.G = nx.Graph(name="test")
156 e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
157 cls.G.add_edges_from(e)
158 cls.G.add_node("g")
159 cls.DG = nx.DiGraph(cls.G)
160 cls.DG.remove_edge("b", "a")
161 cls.DG.remove_edge("b", "c")
162 cls.XG = nx.MultiGraph()
163 cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
164 cls.XDG = nx.MultiDiGraph(cls.XG)
165
166 def test_parse_multiline_adjlist(self):
167 lines = [
168 "1 2",
169 "b {'weight':3, 'name': 'Frodo'}",
170 "c {}",
171 "d 1",
172 "e {'weight':6, 'name': 'Saruman'}",
173 ]
174 nx.parse_multiline_adjlist(iter(lines)) # smoke test
175 with pytest.raises(TypeError):
176 nx.parse_multiline_adjlist(iter(lines), nodetype=int)
177 nx.parse_multiline_adjlist(iter(lines), edgetype=str) # smoke test
178 with pytest.raises(TypeError):
179 nx.parse_multiline_adjlist(iter(lines), nodetype=int)
180 lines = ["1 a"]
181 with pytest.raises(TypeError):
182 nx.parse_multiline_adjlist(iter(lines))
183 lines = ["a 2"]
184 with pytest.raises(TypeError):
185 nx.parse_multiline_adjlist(iter(lines), nodetype=int)
186 lines = ["1 2"]
187 with pytest.raises(TypeError):
188 nx.parse_multiline_adjlist(iter(lines))
189 lines = ["1 2", "2 {}"]
190 with pytest.raises(TypeError):
191 nx.parse_multiline_adjlist(iter(lines))
192
193 def test_multiline_adjlist_graph(self):
194 G = self.G
195 (fd, fname) = tempfile.mkstemp()
196 nx.write_multiline_adjlist(G, fname)
197 H = nx.read_multiline_adjlist(fname)
198 H2 = nx.read_multiline_adjlist(fname)
199 assert H != H2 # they should be different graphs
200 assert_nodes_equal(list(H), list(G))
201 assert_edges_equal(list(H.edges()), list(G.edges()))
202 os.close(fd)
203 os.unlink(fname)
204
205 def test_multiline_adjlist_digraph(self):
206 G = self.DG
207 (fd, fname) = tempfile.mkstemp()
208 nx.write_multiline_adjlist(G, fname)
209 H = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
210 H2 = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
211 assert H != H2 # they should be different graphs
212 assert_nodes_equal(list(H), list(G))
213 assert_edges_equal(list(H.edges()), list(G.edges()))
214 os.close(fd)
215 os.unlink(fname)
216
217 def test_multiline_adjlist_integers(self):
218 (fd, fname) = tempfile.mkstemp()
219 G = nx.convert_node_labels_to_integers(self.G)
220 nx.write_multiline_adjlist(G, fname)
221 H = nx.read_multiline_adjlist(fname, nodetype=int)
222 H2 = nx.read_multiline_adjlist(fname, nodetype=int)
223 assert H != H2 # they should be different graphs
224 assert_nodes_equal(list(H), list(G))
225 assert_edges_equal(list(H.edges()), list(G.edges()))
226 os.close(fd)
227 os.unlink(fname)
228
229 def test_multiline_adjlist_multigraph(self):
230 G = self.XG
231 (fd, fname) = tempfile.mkstemp()
232 nx.write_multiline_adjlist(G, fname)
233 H = nx.read_multiline_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
234 H2 = nx.read_multiline_adjlist(
235 fname, nodetype=int, create_using=nx.MultiGraph()
236 )
237 assert H != H2 # they should be different graphs
238 assert_nodes_equal(list(H), list(G))
239 assert_edges_equal(list(H.edges()), list(G.edges()))
240 os.close(fd)
241 os.unlink(fname)
242
243 def test_multiline_adjlist_multidigraph(self):
244 G = self.XDG
245 (fd, fname) = tempfile.mkstemp()
246 nx.write_multiline_adjlist(G, fname)
247 H = nx.read_multiline_adjlist(
248 fname, nodetype=int, create_using=nx.MultiDiGraph()
249 )
250 H2 = nx.read_multiline_adjlist(
251 fname, nodetype=int, create_using=nx.MultiDiGraph()
252 )
253 assert H != H2 # they should be different graphs
254 assert_nodes_equal(list(H), list(G))
255 assert_edges_equal(list(H.edges()), list(G.edges()))
256 os.close(fd)
257 os.unlink(fname)
258
259 def test_multiline_adjlist_delimiter(self):
260 fh = io.BytesIO()
261 G = nx.path_graph(3)
262 nx.write_multiline_adjlist(G, fh, delimiter=":")
263 fh.seek(0)
264 H = nx.read_multiline_adjlist(fh, nodetype=int, delimiter=":")
265 assert_nodes_equal(list(H), list(G))
266 assert_edges_equal(list(H.edges()), list(G.edges()))