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