Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.36 KB)

1
from nose.tools import assert_equal, assert_raises, assert_not_equal
2
import networkx as nx
3
import io
4
import tempfile
5
import os
6
from networkx.readwrite.p2g import *
7
from networkx.testing import *
8

    
9

    
10
class TestP2G:
11

    
12
    def setUp(self):
13
        self.G = nx.Graph(name="test")
14
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
15
        self.G.add_edges_from(e)
16
        self.G.add_node('g')
17
        self.DG = nx.DiGraph(self.G)
18

    
19
    def test_read_p2g(self):
20
        s = b"""\
21
name
22
3 4
23
a
24
1 2
25
b
26

27
c
28
0 2
29
"""
30
        bytesIO = io.BytesIO(s)
31
        G = read_p2g(bytesIO)
32
        assert_equal(G.name, 'name')
33
        assert_equal(sorted(G), ['a', 'b', 'c'])
34
        edges = [(str(u), str(v)) for u, v in G.edges()]
35
        assert_edges_equal(G.edges(), [('a', 'c'), ('a', 'b'), ('c', 'a'), ('c', 'c')])
36

    
37
    def test_write_p2g(self):
38
        s = b"""foo
39
3 2
40
1
41
1 
42
2
43
2 
44
3
45

46
"""
47
        fh = io.BytesIO()
48
        G = nx.OrderedDiGraph()
49
        G.name = 'foo'
50
        G.add_edges_from([(1, 2), (2, 3)])
51
        write_p2g(G, fh)
52
        fh.seek(0)
53
        r = fh.read()
54
        assert_equal(r, s)
55

    
56
    def test_write_read_p2g(self):
57
        fh = io.BytesIO()
58
        G = nx.DiGraph()
59
        G.name = 'foo'
60
        G.add_edges_from([('a', 'b'), ('b', 'c')])
61
        write_p2g(G, fh)
62
        fh.seek(0)
63
        H = read_p2g(fh)
64
        assert_edges_equal(G.edges(), H.edges())