Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.07 KB)

1
#!/usr/bin/env python
2
from nose.tools import assert_equal
3
import os
4
import tempfile
5

    
6
import networkx as nx
7
from networkx.testing.utils import *
8

    
9

    
10
class TestGpickle(object):
11
    def setUp(self):
12
        G = nx.Graph(name="test")
13
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
14
        G.add_edges_from(e, width=10)
15
        G.add_node('g', color='green')
16
        G.graph['number'] = 1
17
        DG = nx.DiGraph(G)
18
        MG = nx.MultiGraph(G)
19
        MG.add_edge('a', 'a')
20
        MDG = nx.MultiDiGraph(G)
21
        MDG.add_edge('a', 'a')
22
        fG = G.copy()
23
        fDG = DG.copy()
24
        fMG = MG.copy()
25
        fMDG = MDG.copy()
26
        nx.freeze(fG)
27
        nx.freeze(fDG)
28
        nx.freeze(fMG)
29
        nx.freeze(fMDG)
30
        self.G = G
31
        self.DG = DG
32
        self.MG = MG
33
        self.MDG = MDG
34
        self.fG = fG
35
        self.fDG = fDG
36
        self.fMG = fMG
37
        self.fMDG = fMDG
38

    
39
    def test_gpickle(self):
40
        for G in [self.G, self.DG, self.MG, self.MDG,
41
                  self.fG, self.fDG, self.fMG, self.fMDG]:
42
            (fd, fname) = tempfile.mkstemp()
43
            nx.write_gpickle(G, fname)
44
            Gin = nx.read_gpickle(fname)
45
            assert_nodes_equal(list(G.nodes(data=True)),
46
                               list(Gin.nodes(data=True)))
47
            assert_edges_equal(list(G.edges(data=True)),
48
                               list(Gin.edges(data=True)))
49
            assert_graphs_equal(G, Gin)
50
            os.close(fd)
51
            os.unlink(fname)
52

    
53
    def test_protocol(self):
54
        for G in [self.G, self.DG, self.MG, self.MDG,
55
                  self.fG, self.fDG, self.fMG, self.fMDG]:
56
            with tempfile.TemporaryFile() as f:
57
                nx.write_gpickle(G, f, 0)
58
                f.seek(0)
59
                Gin = nx.read_gpickle(f)
60
                assert_nodes_equal(list(G.nodes(data=True)),
61
                                   list(Gin.nodes(data=True)))
62
                assert_edges_equal(list(G.edges(data=True)),
63
                                   list(Gin.edges(data=True)))
64
                assert_graphs_equal(G, Gin)