Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / readwrite / tests / test_adjlist.py @ 5cef0f13

History | View | Annotate | Download (9.16 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
    Unit tests for adjlist.
4
"""
5
import io
6
from nose.tools import assert_equal, assert_raises, assert_not_equal
7
import os
8
import tempfile
9
import networkx as nx
10
from networkx.testing import (assert_nodes_equal, assert_edges_equal,
11
                              assert_graphs_equal)
12

    
13

    
14
class TestAdjlist():
15

    
16
    def setUp(self):
17
        self.G = nx.Graph(name="test")
18
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
19
        self.G.add_edges_from(e)
20
        self.G.add_node('g')
21
        self.DG = nx.DiGraph(self.G)
22
        self.XG = nx.MultiGraph()
23
        self.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
24
        self. XDG = nx.MultiDiGraph(self.XG)
25

    
26
    def test_read_multiline_adjlist_1(self):
27
        # Unit test for https://networkx.lanl.gov/trac/ticket/252
28
        s = b"""# comment line
29
1 2
30
# comment line
31
2
32
3
33
"""
34
        bytesIO = io.BytesIO(s)
35
        G = nx.read_multiline_adjlist(bytesIO)
36
        adj = {'1': {'3': {}, '2': {}}, '3': {'1': {}}, '2': {'1': {}}}
37
        assert_graphs_equal(G, nx.Graph(adj))
38

    
39
    def test_unicode(self):
40
        G = nx.Graph()
41
        try:  # Python 3.x
42
            name1 = chr(2344) + chr(123) + chr(6543)
43
            name2 = chr(5543) + chr(1543) + chr(324)
44
        except ValueError:  # Python 2.6+
45
            name1 = unichr(2344) + unichr(123) + unichr(6543)
46
            name2 = unichr(5543) + unichr(1543) + unichr(324)
47
        G.add_edge(name1, 'Radiohead', **{name2: 3})
48
        fd, fname = tempfile.mkstemp()
49
        nx.write_multiline_adjlist(G, fname)
50
        H = nx.read_multiline_adjlist(fname)
51
        assert_graphs_equal(G, H)
52
        os.close(fd)
53
        os.unlink(fname)
54

    
55
    def test_latin1_err(self):
56
        G = nx.Graph()
57
        try:  # Python 3.x
58
            name1 = chr(2344) + chr(123) + chr(6543)
59
            name2 = chr(5543) + chr(1543) + chr(324)
60
        except ValueError:  # Python 2.6+
61
            name1 = unichr(2344) + unichr(123) + unichr(6543)
62
            name2 = unichr(5543) + unichr(1543) + unichr(324)
63
        G.add_edge(name1, 'Radiohead', **{name2: 3})
64
        fd, fname = tempfile.mkstemp()
65
        assert_raises(UnicodeEncodeError,
66
                      nx.write_multiline_adjlist,
67
                      G, fname, encoding='latin-1')
68
        os.close(fd)
69
        os.unlink(fname)
70

    
71
    def test_latin1(self):
72
        G = nx.Graph()
73
        try:  # Python 3.x
74
            blurb = chr(1245)  # just to trigger the exception
75
            name1 = 'Bj' + chr(246) + 'rk'
76
            name2 = chr(220) + 'ber'
77
        except ValueError:  # Python 2.6+
78
            name1 = 'Bj' + unichr(246) + 'rk'
79
            name2 = unichr(220) + 'ber'
80
        G.add_edge(name1, 'Radiohead', **{name2: 3})
81
        fd, fname = tempfile.mkstemp()
82
        nx.write_multiline_adjlist(G, fname, encoding='latin-1')
83
        H = nx.read_multiline_adjlist(fname, encoding='latin-1')
84
        assert_graphs_equal(G, H)
85
        os.close(fd)
86
        os.unlink(fname)
87

    
88
    def test_adjlist_graph(self):
89
        G = self.G
90
        (fd, fname) = tempfile.mkstemp()
91
        nx.write_adjlist(G, fname)
92
        H = nx.read_adjlist(fname)
93
        H2 = nx.read_adjlist(fname)
94
        assert_not_equal(H, H2)  # they should be different graphs
95
        assert_nodes_equal(list(H), list(G))
96
        assert_edges_equal(list(H.edges()), list(G.edges()))
97
        os.close(fd)
98
        os.unlink(fname)
99

    
100
    def test_adjlist_digraph(self):
101
        G = self.DG
102
        (fd, fname) = tempfile.mkstemp()
103
        nx.write_adjlist(G, fname)
104
        H = nx.read_adjlist(fname, create_using=nx.DiGraph())
105
        H2 = nx.read_adjlist(fname, create_using=nx.DiGraph())
106
        assert_not_equal(H, H2)  # they should be different graphs
107
        assert_nodes_equal(list(H), list(G))
108
        assert_edges_equal(list(H.edges()), list(G.edges()))
109
        os.close(fd)
110
        os.unlink(fname)
111

    
112
    def test_adjlist_integers(self):
113
        (fd, fname) = tempfile.mkstemp()
114
        G = nx.convert_node_labels_to_integers(self.G)
115
        nx.write_adjlist(G, fname)
116
        H = nx.read_adjlist(fname, nodetype=int)
117
        H2 = nx.read_adjlist(fname, nodetype=int)
118
        assert_nodes_equal(list(H), list(G))
119
        assert_edges_equal(list(H.edges()), list(G.edges()))
120
        os.close(fd)
121
        os.unlink(fname)
122

    
123
    def test_adjlist_multigraph(self):
124
        G = self.XG
125
        (fd, fname) = tempfile.mkstemp()
126
        nx.write_adjlist(G, fname)
127
        H = nx.read_adjlist(fname, nodetype=int,
128
                            create_using=nx.MultiGraph())
129
        H2 = nx.read_adjlist(fname, nodetype=int,
130
                             create_using=nx.MultiGraph())
131
        assert_not_equal(H, H2)  # they should be different graphs
132
        assert_nodes_equal(list(H), list(G))
133
        assert_edges_equal(list(H.edges()), list(G.edges()))
134
        os.close(fd)
135
        os.unlink(fname)
136

    
137
    def test_adjlist_multidigraph(self):
138
        G = self.XDG
139
        (fd, fname) = tempfile.mkstemp()
140
        nx.write_adjlist(G, fname)
141
        H = nx.read_adjlist(fname, nodetype=int,
142
                            create_using=nx.MultiDiGraph())
143
        H2 = nx.read_adjlist(fname, nodetype=int,
144
                             create_using=nx.MultiDiGraph())
145
        assert_not_equal(H, H2)  # they should be different graphs
146
        assert_nodes_equal(list(H), list(G))
147
        assert_edges_equal(list(H.edges()), list(G.edges()))
148
        os.close(fd)
149
        os.unlink(fname)
150

    
151
    def test_adjlist_delimiter(self):
152
        fh = io.BytesIO()
153
        G = nx.path_graph(3)
154
        nx.write_adjlist(G, fh, delimiter=':')
155
        fh.seek(0)
156
        H = nx.read_adjlist(fh, nodetype=int, delimiter=':')
157
        assert_nodes_equal(list(H), list(G))
158
        assert_edges_equal(list(H.edges()), list(G.edges()))
159

    
160

    
161
class TestMultilineAdjlist():
162

    
163
    def setUp(self):
164
        self.G = nx.Graph(name="test")
165
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
166
        self.G.add_edges_from(e)
167
        self.G.add_node('g')
168
        self.DG = nx.DiGraph(self.G)
169
        self.DG.remove_edge('b', 'a')
170
        self.DG.remove_edge('b', 'c')
171
        self.XG = nx.MultiGraph()
172
        self.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
173
        self. XDG = nx.MultiDiGraph(self.XG)
174

    
175
    def test_multiline_adjlist_graph(self):
176
        G = self.G
177
        (fd, fname) = tempfile.mkstemp()
178
        nx.write_multiline_adjlist(G, fname)
179
        H = nx.read_multiline_adjlist(fname)
180
        H2 = nx.read_multiline_adjlist(fname)
181
        assert_not_equal(H, H2)  # they should be different graphs
182
        assert_nodes_equal(list(H), list(G))
183
        assert_edges_equal(list(H.edges()), list(G.edges()))
184
        os.close(fd)
185
        os.unlink(fname)
186

    
187
    def test_multiline_adjlist_digraph(self):
188
        G = self.DG
189
        (fd, fname) = tempfile.mkstemp()
190
        nx.write_multiline_adjlist(G, fname)
191
        H = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
192
        H2 = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
193
        assert_not_equal(H, H2)  # they should be different graphs
194
        assert_nodes_equal(list(H), list(G))
195
        assert_edges_equal(list(H.edges()), list(G.edges()))
196
        os.close(fd)
197
        os.unlink(fname)
198

    
199
    def test_multiline_adjlist_integers(self):
200
        (fd, fname) = tempfile.mkstemp()
201
        G = nx.convert_node_labels_to_integers(self.G)
202
        nx.write_multiline_adjlist(G, fname)
203
        H = nx.read_multiline_adjlist(fname, nodetype=int)
204
        H2 = nx.read_multiline_adjlist(fname, nodetype=int)
205
        assert_nodes_equal(list(H), list(G))
206
        assert_edges_equal(list(H.edges()), list(G.edges()))
207
        os.close(fd)
208
        os.unlink(fname)
209

    
210
    def test_multiline_adjlist_multigraph(self):
211
        G = self.XG
212
        (fd, fname) = tempfile.mkstemp()
213
        nx.write_multiline_adjlist(G, fname)
214
        H = nx.read_multiline_adjlist(fname, nodetype=int,
215
                                      create_using=nx.MultiGraph())
216
        H2 = nx.read_multiline_adjlist(fname, nodetype=int,
217
                                       create_using=nx.MultiGraph())
218
        assert_not_equal(H, H2)  # they should be different graphs
219
        assert_nodes_equal(list(H), list(G))
220
        assert_edges_equal(list(H.edges()), list(G.edges()))
221
        os.close(fd)
222
        os.unlink(fname)
223

    
224
    def test_multiline_adjlist_multidigraph(self):
225
        G = self.XDG
226
        (fd, fname) = tempfile.mkstemp()
227
        nx.write_multiline_adjlist(G, fname)
228
        H = nx.read_multiline_adjlist(fname, nodetype=int,
229
                                      create_using=nx.MultiDiGraph())
230
        H2 = nx.read_multiline_adjlist(fname, nodetype=int,
231
                                       create_using=nx.MultiDiGraph())
232
        assert_not_equal(H, H2)  # they should be different graphs
233
        assert_nodes_equal(list(H), list(G))
234
        assert_edges_equal(list(H.edges()), list(G.edges()))
235
        os.close(fd)
236
        os.unlink(fname)
237

    
238
    def test_multiline_adjlist_delimiter(self):
239
        fh = io.BytesIO()
240
        G = nx.path_graph(3)
241
        nx.write_multiline_adjlist(G, fh, delimiter=':')
242
        fh.seek(0)
243
        H = nx.read_multiline_adjlist(fh, nodetype=int, delimiter=':')
244
        assert_nodes_equal(list(H), list(G))
245
        assert_edges_equal(list(H.edges()), list(G.edges()))