1 #!/usr/bin/env python 2 from nose.tools import * 3 import networkx 4 5 class BaseGraphTester(object): 6 """ Tests for data-structure independent graph class features.""" 7 def test_contains(self): 8 G=self.K3 9 assert(1 in G ) 10 assert(4 not in G ) 11 assert('b' not in G ) 12 assert([] not in G ) # no exception for nonhashable 13 assert({1:1} not in G) # no exception for nonhashable 14 15 def test_order(self): 16 G=self.K3 17 assert_equal(len(G),3) 18 assert_equal(G.order(),3) 19 assert_equal(G.number_of_nodes(),3) 20 21 def test_nodes_iter(self): 22 G=self.K3 23 assert_equal(sorted(G.nodes_iter()),self.k3nodes) 24 assert_equal(sorted(G.nodes_iter(data=True)),[(0,{}),(1,{}),(2,{})]) 25 26 def test_nodes(self): 27 G=self.K3 28 assert_equal(sorted(G.nodes()),self.k3nodes) 29 assert_equal(sorted(G.nodes(data=True)),[(0,{}),(1,{}),(2,{})]) 30 31 def test_has_node(self): 32 G=self.K3 33 assert(G.has_node(1)) 34 assert(not G.has_node(4)) 35 assert(not G.has_node([])) # no exception for nonhashable 36 assert(not G.has_node({1:1})) # no exception for nonhashable 37 38 def test_has_edge(self): 39 G=self.K3 40 assert_equal(G.has_edge(0,1),True) 41 assert_equal(G.has_edge(0,-1),False) 42 43 def test_neighbors(self): 44 G=self.K3 45 assert_equal(sorted(G.neighbors(0)),[1,2]) 46 assert_raises((KeyError,networkx.NetworkXError), G.neighbors,-1) 47 48 def test_neighbors_iter(self): 49 G=self.K3 50 assert_equal(sorted(G.neighbors_iter(0)),[1,2]) 51 assert_raises((KeyError,networkx.NetworkXError), G.neighbors_iter,-1) 52 53 def test_edges(self): 54 G=self.K3 55 assert_equal(sorted(G.edges()),[(0,1),(0,2),(1,2)]) 56 assert_equal(sorted(G.edges(0)),[(0,1),(0,2)]) 57 assert_raises((KeyError,networkx.NetworkXError), G.edges,-1) 58 59 def test_edges_iter(self): 60 G=self.K3 61 assert_equal(sorted(G.edges_iter()),[(0,1),(0,2),(1,2)]) 62 assert_equal(sorted(G.edges_iter(0)),[(0,1),(0,2)]) 63 f=lambda x:list(G.edges_iter(x)) 64 assert_raises((KeyError,networkx.NetworkXError), f, -1) 65 66 def test_adjacency_list(self): 67 G=self.K3 68 assert_equal(G.adjacency_list(),[[1,2],[0,2],[0,1]]) 69 70 def test_degree(self): 71 G=self.K3 72 assert_equal(list(G.degree().values()),[2,2,2]) 73 assert_equal(G.degree(),{0:2,1:2,2:2}) 74 assert_equal(G.degree(0),2) 75 assert_equal(G.degree([0]),{0:2}) 76 assert_raises((KeyError,networkx.NetworkXError), G.degree,-1) 77 78 def test_weighted_degree(self): 79 G=self.Graph() 80 G.add_edge(1,2,weight=2) 81 G.add_edge(2,3,weight=3) 82 assert_equal(list(G.degree(weight='weight').values()),[2,5,3]) 83 assert_equal(G.degree(weight='weight'),{1:2,2:5,3:3}) 84 assert_equal(G.degree(1,weight='weight'),2) 85 assert_equal(G.degree([1],weight='weight'),{1:2}) 86 87 def test_degree_iter(self): 88 G=self.K3 89 assert_equal(list(G.degree_iter()),[(0,2),(1,2),(2,2)]) 90 assert_equal(dict(G.degree_iter()),{0:2,1:2,2:2}) 91 assert_equal(list(G.degree_iter(0)),[(0,2)]) 92 93 def test_size(self): 94 G=self.K3 95 assert_equal(G.size(),3) 96 assert_equal(G.number_of_edges(),3) 97 98 def test_add_star(self): 99 G=self.K3.copy() 100 nlist=[12,13,14,15] 101 G.add_star(nlist) 102 assert_equal(sorted(G.edges(nlist)),[(12,13),(12,14),(12,15)]) 103 G=self.K3.copy() 104 G.add_star(nlist,weight=2.0) 105 assert_equal(sorted(G.edges(nlist,data=True)),\ 106 [(12,13,{'weight':2.}), 107 (12,14,{'weight':2.}), 108 (12,15,{'weight':2.})]) 109 110 def test_add_path(self): 111 G=self.K3.copy() 112 nlist=[12,13,14,15] 113 G.add_path(nlist) 114 assert_equal(sorted(G.edges(nlist)),[(12,13),(13,14),(14,15)]) 115 G=self.K3.copy() 116 G.add_path(nlist,weight=2.0) 117 assert_equal(sorted(G.edges(nlist,data=True)),\ 118 [(12,13,{'weight':2.}), 119 (13,14,{'weight':2.}), 120 (14,15,{'weight':2.})]) 121 122 def test_add_cycle(self): 123 G=self.K3.copy() 124 nlist=[12,13,14,15] 125 oklists=[ [(12,13),(12,15),(13,14),(14,15)], \ 126 [(12,13),(13,14),(14,15),(15,12)] ] 127 G.add_cycle(nlist) 128 assert_true(sorted(G.edges(nlist)) in oklists) 129 G=self.K3.copy() 130 oklists=[ [(12,13,{'weight':1.}),\ 131 (12,15,{'weight':1.}),\ 132 (13,14,{'weight':1.}),\ 133 (14,15,{'weight':1.})], \ 134 \ 135 [(12,13,{'weight':1.}),\ 136 (13,14,{'weight':1.}),\ 137 (14,15,{'weight':1.}),\ 138 (15,12,{'weight':1.})] \ 139 ] 140 141 G.add_cycle(nlist,weight=1.0) 142 assert_true(sorted(G.edges(nlist,data=True)) in oklists) 143 144 def test_nbunch_iter(self): 145 G=self.K3 146 assert_equal(list(G.nbunch_iter()),self.k3nodes) # all nodes 147 assert_equal(list(G.nbunch_iter(0)),[0]) # single node 148 assert_equal(list(G.nbunch_iter([0,1])),[0,1]) # sequence 149 # sequence with none in graph 150 assert_equal(list(G.nbunch_iter([-1])),[]) 151 # string sequence with none in graph 152 assert_equal(list(G.nbunch_iter("foo")),[]) 153 # node not in graph doesn't get caught upon creation of iterator 154 bunch=G.nbunch_iter(-1) 155 # but gets caught when iterator used 156 assert_raises(networkx.NetworkXError,list,bunch) 157 # unhashable doesn't get caught upon creation of iterator 158 bunch=G.nbunch_iter([0,1,2,{}]) 159 # but gets caught when iterator hits the unhashable 160 assert_raises(networkx.NetworkXError,list,bunch) 161 162 def test_selfloop_degree(self): 163 G=self.Graph() 164 G.add_edge(1,1) 165 assert_equal(list(G.degree().values()),[2]) 166 assert_equal(G.degree(),{1:2}) 167 assert_equal(G.degree(1),2) 168 assert_equal(G.degree([1]),{1:2}) 169 assert_equal(G.degree([1],weight='weight'),{1:2}) 170 171 def test_selfloops(self): 172 G=self.K3.copy() 173 G.add_edge(0,0) 174 assert_equal(G.nodes_with_selfloops(),[0]) 175 assert_equal(G.selfloop_edges(),[(0,0)]) 176 assert_equal(G.number_of_selfloops(),1) 177 G.remove_edge(0,0) 178 G.add_edge(0,0) 179 G.remove_edges_from([(0,0)]) 180 G.add_edge(1,1) 181 G.remove_node(1) 182 G.add_edge(0,0) 183 G.add_edge(1,1) 184 G.remove_nodes_from([0,1]) 185 186 187 class BaseAttrGraphTester(BaseGraphTester): 188 """ Tests of graph class attribute features.""" 189 def test_weighted_degree(self): 190 G=self.Graph() 191 G.add_edge(1,2,weight=2,other=3) 192 G.add_edge(2,3,weight=3,other=4) 193 assert_equal(list(G.degree(weight='weight').values()),[2,5,3]) 194 assert_equal(G.degree(weight='weight'),{1:2,2:5,3:3}) 195 assert_equal(G.degree(1,weight='weight'),2) 196 assert_equal(G.degree([1],weight='weight'),{1:2}) 197 198 assert_equal(list(G.degree(weight='other').values()),[3,7,4]) 199 assert_equal(G.degree(weight='other'),{1:3,2:7,3:4}) 200 assert_equal(G.degree(1,weight='other'),3) 201 assert_equal(G.degree([1],weight='other'),{1:3}) 202 203 def add_attributes(self,G): 204 G.graph['foo']=[] 205 G.node[0]['foo']=[] 206 G.remove_edge(1,2) 207 ll=[] 208 G.add_edge(1,2,foo=ll) 209 G.add_edge(2,1,foo=ll) 210 # attr_dict must be dict 211 assert_raises(networkx.NetworkXError,G.add_edge,0,1,attr_dict=[]) 212 213 def test_name(self): 214 G=self.Graph(name='') 215 assert_equal(G.name,"") 216 G=self.Graph(name='test') 217 assert_equal(G.__str__(),"test") 218 assert_equal(G.name,"test") 219 220 def test_copy(self): 221 G=self.K3 222 self.add_attributes(G) 223 H=G.copy() 224 self.is_deepcopy(H,G) 225 H=G.__class__(G) 226 self.is_shallow_copy(H,G) 227 228 def test_copy_attr(self): 229 G=self.Graph(foo=[]) 230 G.add_node(0,foo=[]) 231 G.add_edge(1,2,foo=[]) 232 H=G.copy() 233 self.is_deepcopy(H,G) 234 H=G.__class__(G) # just copy 235 self.is_shallow_copy(H,G) 236 237 def is_deepcopy(self,H,G): 238 self.graphs_equal(H,G) 239 self.different_attrdict(H,G) 240 self.deep_copy_attrdict(H,G) 241 242 def deep_copy_attrdict(self,H,G): 243 self.deepcopy_graph_attr(H,G) 244 self.deepcopy_node_attr(H,G) 245 self.deepcopy_edge_attr(H,G) 246 247 def deepcopy_graph_attr(self,H,G): 248 assert_equal(G.graph['foo'],H.graph['foo']) 249 G.graph['foo'].append(1) 250 assert_not_equal(G.graph['foo'],H.graph['foo']) 251 252 def deepcopy_node_attr(self,H,G): 253 assert_equal(G.node[0]['foo'],H.node[0]['foo']) 254 G.node[0]['foo'].append(1) 255 assert_not_equal(G.node[0]['foo'],H.node[0]['foo']) 256 257 def deepcopy_edge_attr(self,H,G): 258 assert_equal(G[1][2]['foo'],H[1][2]['foo']) 259 G[1][2]['foo'].append(1) 260 assert_not_equal(G[1][2]['foo'],H[1][2]['foo']) 261 262 def is_shallow_copy(self,H,G): 263 self.graphs_equal(H,G) 264 self.different_attrdict(H,G) 265 self.shallow_copy_attrdict(H,G) 266 267 def shallow_copy_attrdict(self,H,G): 268 self.shallow_copy_graph_attr(H,G) 269 self.shallow_copy_node_attr(H,G) 270 self.shallow_copy_edge_attr(H,G) 271 272 def shallow_copy_graph_attr(self,H,G): 273 assert_equal(G.graph['foo'],H.graph['foo']) 274 G.graph['foo'].append(1) 275 assert_equal(G.graph['foo'],H.graph['foo']) 276 277 def shallow_copy_node_attr(self,H,G): 278 assert_equal(G.node[0]['foo'],H.node[0]['foo']) 279 G.node[0]['foo'].append(1) 280 assert_equal(G.node[0]['foo'],H.node[0]['foo']) 281 282 def shallow_copy_edge_attr(self,H,G): 283 assert_equal(G[1][2]['foo'],H[1][2]['foo']) 284 G[1][2]['foo'].append(1) 285 assert_equal(G[1][2]['foo'],H[1][2]['foo']) 286 287 def same_attrdict(self, H, G): 288 old_foo=H[1][2]['foo'] 289 H.add_edge(1,2,foo='baz') 290 assert_equal(G.edge,H.edge) 291 H.add_edge(1,2,foo=old_foo) 292 assert_equal(G.edge,H.edge) 293 old_foo=H.node[0]['foo'] 294 H.node[0]['foo']='baz' 295 assert_equal(G.node,H.node) 296 H.node[0]['foo']=old_foo 297 assert_equal(G.node,H.node) 298 299 def different_attrdict(self, H, G): 300 old_foo=H[1][2]['foo'] 301 H.add_edge(1,2,foo='baz') 302 assert_not_equal(G.edge,H.edge) 303 H.add_edge(1,2,foo=old_foo) 304 assert_equal(G.edge,H.edge) 305 old_foo=H.node[0]['foo'] 306 H.node[0]['foo']='baz' 307 assert_not_equal(G.node,H.node) 308 H.node[0]['foo']=old_foo 309 assert_equal(G.node,H.node) 310 311 def graphs_equal(self,H,G): 312 assert_equal(G.adj,H.adj) 313 assert_equal(G.edge,H.edge) 314 assert_equal(G.node,H.node) 315 assert_equal(G.graph,H.graph) 316 assert_equal(G.name,H.name) 317 if not G.is_directed() and not H.is_directed(): 318 assert_true(H.adj[1][2] is H.adj[2][1]) 319 assert_true(G.adj[1][2] is G.adj[2][1]) 320 else: # at least one is directed 321 if not G.is_directed(): 322 G.pred=G.adj 323 G.succ=G.adj 324 if not H.is_directed(): 325 H.pred=H.adj 326 H.succ=H.adj 327 assert_equal(G.pred,H.pred) 328 assert_equal(G.succ,H.succ) 329 assert_true(H.succ[1][2] is H.pred[2][1]) 330 assert_true(G.succ[1][2] is G.pred[2][1]) 331 332 def test_graph_attr(self): 333 G=self.K3 334 G.graph['foo']='bar' 335 assert_equal(G.graph['foo'], 'bar') 336 del G.graph['foo'] 337 assert_equal(G.graph, {}) 338 H=self.Graph(foo='bar') 339 assert_equal(H.graph['foo'], 'bar') 340 341 def test_node_attr(self): 342 G=self.K3 343 G.add_node(1,foo='bar') 344 assert_equal(G.nodes(), [0,1,2]) 345 assert_equal(G.nodes(data=True), [(0,{}),(1,{'foo':'bar'}),(2,{})]) 346 G.node[1]['foo']='baz' 347 assert_equal(G.nodes(data=True), [(0,{}),(1,{'foo':'baz'}),(2,{})]) 348 349 def test_node_attr2(self): 350 G=self.K3 351 a={'foo':'bar'} 352 G.add_node(3,attr_dict=a) 353 assert_equal(G.nodes(), [0,1,2,3]) 354 assert_equal(G.nodes(data=True), 355 [(0,{}),(1,{}),(2,{}),(3,{'foo':'bar'})]) 356 357 def test_edge_attr(self): 358 G=self.Graph() 359 G.add_edge(1,2,foo='bar') 360 assert_equal(G.edges(data=True), [(1,2,{'foo':'bar'})]) 361 362 def test_edge_attr2(self): 363 G=self.Graph() 364 G.add_edges_from([(1,2),(3,4)],foo='foo') 365 assert_equal(sorted(G.edges(data=True)), 366 [(1,2,{'foo':'foo'}),(3,4,{'foo':'foo'})]) 367 368 def test_edge_attr3(self): 369 G=self.Graph() 370 G.add_edges_from([(1,2,{'weight':32}),(3,4,{'weight':64})],foo='foo') 371 assert_equal(G.edges(data=True), 372 [(1,2,{'foo':'foo','weight':32}),\ 373 (3,4,{'foo':'foo','weight':64})]) 374 375 G.remove_edges_from([(1,2),(3,4)]) 376 G.add_edge(1,2,data=7,spam='bar',bar='foo') 377 assert_equal(G.edges(data=True), 378 [(1,2,{'data':7,'spam':'bar','bar':'foo'})]) 379 380 def test_edge_attr4(self): 381 G=self.Graph() 382 G.add_edge(1,2,data=7,spam='bar',bar='foo') 383 assert_equal(G.edges(data=True), 384 [(1,2,{'data':7,'spam':'bar','bar':'foo'})]) 385 G[1][2]['data']=10 # OK to set data like this 386 assert_equal(G.edges(data=True), 387 [(1,2,{'data':10,'spam':'bar','bar':'foo'})]) 388 389 G.edge[1][2]['data']=20 # another spelling, "edge" 390 assert_equal(G.edges(data=True), 391 [(1,2,{'data':20,'spam':'bar','bar':'foo'})]) 392 G.edge[1][2]['listdata']=[20,200] 393 G.edge[1][2]['weight']=20 394 assert_equal(G.edges(data=True), 395 [(1,2,{'data':20,'spam':'bar', 396 'bar':'foo','listdata':[20,200],'weight':20})]) 397 398 def test_attr_dict_not_dict(self): 399 # attr_dict must be dict 400 G=self.Graph() 401 edges=[(1,2)] 402 assert_raises(networkx.NetworkXError,G.add_edges_from,edges, 403 attr_dict=[]) 404 405 def test_to_undirected(self): 406 G=self.K3 407 self.add_attributes(G) 408 H=networkx.Graph(G) 409 self.is_shallow_copy(H,G) 410 H=G.to_undirected() 411 self.is_deepcopy(H,G) 412 413 def test_to_directed(self): 414 G=self.K3 415 self.add_attributes(G) 416 H=networkx.DiGraph(G) 417 self.is_shallow_copy(H,G) 418 H=G.to_directed() 419 self.is_deepcopy(H,G) 420 421 def test_subgraph(self): 422 G=self.K3 423 self.add_attributes(G) 424 H=G.subgraph([0,1,2,5]) 425 # assert_equal(H.name, 'Subgraph of ('+G.name+')') 426 H.name=G.name 427 self.graphs_equal(H,G) 428 self.same_attrdict(H,G) 429 self.shallow_copy_attrdict(H,G) 430 431 H=G.subgraph(0) 432 assert_equal(H.adj,{0:{}}) 433 H=G.subgraph([]) 434 assert_equal(H.adj,{}) 435 assert_not_equal(G.adj,{}) 436 437 def test_selfloops_attr(self): 438 G=self.K3.copy() 439 G.add_edge(0,0) 440 G.add_edge(1,1,weight=2) 441 assert_equal(G.selfloop_edges(data=True), 442 [(0,0,{}),(1,1,{'weight':2})]) 443 444 445 class TestGraph(BaseAttrGraphTester): 446 """Tests specific to dict-of-dict-of-dict graph data structure""" 447 def setUp(self): 448 self.Graph=networkx.Graph 449 # build dict-of-dict-of-dict K3 450 ed1,ed2,ed3 = ({},{},{}) 451 self.k3adj={0: {1: ed1, 2: ed2}, 452 1: {0: ed1, 2: ed3}, 453 2: {0: ed2, 1: ed3}} 454 self.k3edges=[(0, 1), (0, 2), (1, 2)] 455 self.k3nodes=[0, 1, 2] 456 self.K3=self.Graph() 457 self.K3.adj=self.K3.edge=self.k3adj 458 self.K3.node={} 459 self.K3.node[0]={} 460 self.K3.node[1]={} 461 self.K3.node[2]={} 462 463 def test_data_input(self): 464 G=self.Graph(data={1:[2],2:[1]}, name="test") 465 assert_equal(G.name,"test") 466 assert_equal(sorted(G.adj.items()),[(1, {2: {}}), (2, {1: {}})]) 467 G=self.Graph({1:[2],2:[1]}, name="test") 468 assert_equal(G.name,"test") 469 assert_equal(sorted(G.adj.items()),[(1, {2: {}}), (2, {1: {}})]) 470 471 def test_adjacency_iter(self): 472 G=self.K3 473 assert_equal(dict(G.adjacency_iter()), 474 {0: {1: {}, 2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}}) 475 476 def test_getitem(self): 477 G=self.K3 478 assert_equal(G[0],{1: {}, 2: {}}) 479 assert_raises(KeyError, G.__getitem__, 'j') 480 assert_raises((TypeError,networkx.NetworkXError), G.__getitem__, ['A']) 481 482 def test_add_node(self): 483 G=self.Graph() 484 G.add_node(0) 485 assert_equal(G.adj,{0:{}}) 486 # test add attributes 487 G.add_node(1,c='red') 488 G.add_node(2,{'c':'blue'}) 489 G.add_node(3,{'c':'blue'},c='red') 490 assert_raises(networkx.NetworkXError, G.add_node, 4, []) 491 assert_raises(networkx.NetworkXError, G.add_node, 4, 4) 492 assert_equal(G.node[1]['c'],'red') 493 assert_equal(G.node[2]['c'],'blue') 494 assert_equal(G.node[3]['c'],'red') 495 # test updating attributes 496 G.add_node(1,c='blue') 497 G.add_node(2,{'c':'red'}) 498 G.add_node(3,{'c':'red'},c='blue') 499 assert_equal(G.node[1]['c'],'blue') 500 assert_equal(G.node[2]['c'],'red') 501 assert_equal(G.node[3]['c'],'blue') 502 503 def test_add_nodes_from(self): 504 G=self.Graph() 505 G.add_nodes_from([0,1,2]) 506 assert_equal(G.adj,{0:{},1:{},2:{}}) 507 # test add attributes 508 G.add_nodes_from([0,1,2],c='red') 509 assert_equal(G.node[0]['c'],'red') 510 assert_equal(G.node[2]['c'],'red') 511 # test that attribute dicts are not the same 512 assert(G.node[0] is not G.node[1]) 513 # test updating attributes 514 G.add_nodes_from([0,1,2],c='blue') 515 assert_equal(G.node[0]['c'],'blue') 516 assert_equal(G.node[2]['c'],'blue') 517 assert(G.node[0] is not G.node[1]) 518 # test tuple input 519 H=self.Graph() 520 H.add_nodes_from(G.nodes(data=True)) 521 assert_equal(H.node[0]['c'],'blue') 522 assert_equal(H.node[2]['c'],'blue') 523 assert(H.node[0] is not H.node[1]) 524 # specific overrides general 525 H.add_nodes_from([0,(1,{'c':'green'}),(3,{'c':'cyan'})],c='red') 526 assert_equal(H.node[0]['c'],'red') 527 assert_equal(H.node[1]['c'],'green') 528 assert_equal(H.node[2]['c'],'blue') 529 assert_equal(H.node[3]['c'],'cyan') 530 531 def test_remove_node(self): 532 G=self.K3 533 G.remove_node(0) 534 assert_equal(G.adj,{1:{2:{}},2:{1:{}}}) 535 assert_raises((KeyError,networkx.NetworkXError), G.remove_node,-1) 536 537 # generator here to implement list,set,string... 538 def test_remove_nodes_from(self): 539 G=self.K3 540 G.remove_nodes_from([0,1]) 541 assert_equal(G.adj,{2:{}}) 542 G.remove_nodes_from([-1]) # silent fail 543 544 def test_add_edge(self): 545 G=self.Graph() 546 G.add_edge(0,1) 547 assert_equal(G.adj,{0: {1: {}}, 1: {0: {}}}) 548 G=self.Graph() 549 G.add_edge(*(0,1)) 550 assert_equal(G.adj,{0: {1: {}}, 1: {0: {}}}) 551 552 def test_add_edges_from(self): 553 G=self.Graph() 554 G.add_edges_from([(0,1),(0,2,{'weight':3})]) 555 assert_equal(G.adj,{0: {1:{}, 2:{'weight':3}}, 1: {0:{}}, \ 556 2:{0:{'weight':3}}}) 557 G=self.Graph() 558 G.add_edges_from([(0,1),(0,2,{'weight':3}),(1,2,{'data':4})],data=2) 559 assert_equal(G.adj,{\ 560 0: {1:{'data':2}, 2:{'weight':3,'data':2}}, \ 561 1: {0:{'data':2}, 2:{'data':4}}, \ 562 2: {0:{'weight':3,'data':2}, 1:{'data':4}} \ 563 }) 564 565 assert_raises(networkx.NetworkXError, 566 G.add_edges_from,[(0,)]) # too few in tuple 567 assert_raises(networkx.NetworkXError, 568 G.add_edges_from,[(0,1,2,3)]) # too many in tuple 569 assert_raises(TypeError, G.add_edges_from,[0]) # not a tuple 570 571 572 def test_remove_edge(self): 573 G=self.K3 574 G.remove_edge(0,1) 575 assert_equal(G.adj,{0:{2:{}},1:{2:{}},2:{0:{},1:{}}}) 576 assert_raises((KeyError,networkx.NetworkXError), G.remove_edge,-1,0) 577 578 def test_remove_edges_from(self): 579 G=self.K3 580 G.remove_edges_from([(0,1)]) 581 assert_equal(G.adj,{0:{2:{}},1:{2:{}},2:{0:{},1:{}}}) 582 G.remove_edges_from([(0,0)]) # silent fail 583 584 def test_clear(self): 585 G=self.K3 586 G.clear() 587 assert_equal(G.adj,{}) 588 589 def test_edges_data(self): 590 G=self.K3 591 assert_equal(sorted(G.edges(data=True)),[(0,1,{}),(0,2,{}),(1,2,{})]) 592 assert_equal(sorted(G.edges(0,data=True)),[(0,1,{}),(0,2,{})]) 593 assert_raises((KeyError,networkx.NetworkXError), G.edges,-1) 594 595 596 def test_get_edge_data(self): 597 G=self.K3 598 assert_equal(G.get_edge_data(0,1),{}) 599 assert_equal(G[0][1],{}) 600 assert_equal(G.get_edge_data(10,20),None) 601 assert_equal(G.get_edge_data(-1,0),None) 602 assert_equal(G.get_edge_data(-1,0,default=1),1) 603