Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.35 KB)

1 5cef0f13 tiamilani
"""
2
    Unit tests for yaml.
3
"""
4
5
import os
6
import tempfile
7
from nose import SkipTest
8
from nose.tools import assert_equal
9
10
import networkx as nx
11
from networkx.testing import assert_edges_equal, assert_nodes_equal
12
13
14
class TestYaml(object):
15
    @classmethod
16
    def setupClass(cls):
17
        global yaml
18
        try:
19
            import yaml
20
        except ImportError:
21
            raise SkipTest('yaml not available.')
22
23
    def setUp(self):
24
        self.build_graphs()
25
26
    def build_graphs(self):
27
        self.G = nx.Graph(name="test")
28
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
29
        self.G.add_edges_from(e)
30
        self.G.add_node('g')
31
32
        self.DG = nx.DiGraph(self.G)
33
34
        self.MG = nx.MultiGraph()
35
        self.MG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
36
37
    def assert_equal(self, G, data=False):
38
        (fd, fname) = tempfile.mkstemp()
39
        nx.write_yaml(G, fname)
40
        Gin = nx.read_yaml(fname)
41
42
        assert_nodes_equal(list(G), list(Gin))
43
        assert_edges_equal(G.edges(data=data), Gin.edges(data=data))
44
45
        os.close(fd)
46
        os.unlink(fname)
47
48
    def testUndirected(self):
49
        self.assert_equal(self.G, data=False)
50
51
    def testDirected(self):
52
        self.assert_equal(self.DG, data=False)
53
54
    def testMultiGraph(self):
55
        self.assert_equal(self.MG, data=True)