def test__edge__1(self): graph = graph_.Graph(['p', 'q', 'r'], { ('p', 'q'): '1', ('q', 'r'): '2' }) self.assertEqual(graph.edge(('p', 'q')), '1') self.assertEqual(graph.edge(('q', 'r')), '2')
def test__has_successors(self): graph = graph_.Graph(['p', 'q', 'r', 's', 't'], [('p', 'q'), ('q', 'r'), ('q', 's')]) self.assertTrue(graph.has_successors('p')) self.assertTrue(graph.has_successors('q')) self.assertFalse(graph.has_successors('r')) self.assertFalse(graph.has_successors('t'))
def test__neighbors__2(self): graph = graph_.Graph(['p', 'q', 'r', 's', 't'], [('p', 'q'), ('q', 'r'), ('s', 'r')]) self.assertEqual(graph.neighbors('p'), set(['q'])) self.assertEqual(graph.neighbors('q'), set(['p', 'r'])) self.assertEqual(graph.neighbors('r'), set(['q', 's'])) self.assertEqual(graph.neighbors('s'), set(['r'])) self.assertEqual(graph.neighbors('t'), set()) # isolated node, must not throw
def test__incident__2(self): graph = graph_.Graph(['p', 'q', 'r', 's', 't'], [('p', 'q'), ('p', 'r'), ('s', 'r')]) self.assertEqual(graph.incident('p'), set([('p', 'q'), ('p', 'r')])) self.assertEqual(graph.incident('q'), set([('p', 'q')])) self.assertEqual(graph.incident('r'), set([('p', 'r'), ('s', 'r')])) self.assertEqual(graph.incident('s'), set([('s', 'r')])) self.assertEqual(graph.incident('t'), set()) # isolated node, must not throw
def test__init__consistency_false(self): graph = graph_.Graph(['p', 'q'], [('p', 'q'), ('q', 'r')], consistency=False) self.assertIsInstance(graph.nodes, nodes_.Nodes) self.assertEqual(graph.nodes.data, {'p': None, 'q': None}) self.assertIsInstance(graph.edges, edges_.Edges) self.assertEqual(graph.edges.data, { ('p', 'q'): None, ('q', 'r'): None })
def test__has_edge__1(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) self.assertTrue(graph.has_edge(('p', 'q'))) self.assertTrue(graph.has_edge(('q', 'r')))
def test__discard_edge__2(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.discard_edge(('p', 'r')) # must not throw self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__del_edge(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.del_edge(('q', 'r')) self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertEqual(graph.edges.data, {('p', 'q'): '1'})
def test__discard_node__3(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.discard_node('x') self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__has_edge__3(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) self.assertFalse(graph.has_edge(('x', 'y'))) self.assertFalse(graph.has_edge(('x', ))) self.assertFalse(graph.has_edge(('x', 'y', 'z')))
def test__add_node__2(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.add_node('r', None) self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': None}) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__repeated_start_nodes_1(self): cb = Callbacks() graph = graph_.Graph(['p', 'q'], [('p', 'q')]) search = dfs_.Dfs(direction='outward', **cb.callbacks) trail = search(graph, ['p', 'p']) self.assertEqual(trail.nodes, ['p', 'q']) self.assertEqual(trail.edges, [('p', 'q')]) self.assertEqual(trail.backedges, []) self.assertEqual(cb.ingress_nodes, ['p', 'q']) self.assertEqual(cb.egress_nodes, ['q', 'p']) self.assertEqual(cb.ingress_edges, [('p', 'q')]) self.assertEqual(cb.egress_edges, [('p', 'q')]) self.assertEqual(cb.backedges, [])
def test__init__2(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) self.assertIsInstance(graph.nodes, nodes_.Nodes) self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertIsInstance(graph.edges, edges_.Edges) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__init__1(self): graph = graph_.Graph(['p', 'q', 'r', 's'], [('p', 'q'), ('q', 'r')]) self.assertIsInstance(graph.nodes, nodes_.Nodes) self.assertEqual(graph.nodes.data, { 'p': None, 'q': None, 'r': None, 's': None }) self.assertIsInstance(graph.edges, edges_.Edges) self.assertEqual(graph.edges.data, { ('p', 'q'): None, ('q', 'r'): None })
def test__del_edge__KeyError(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) with self.assertRaises(KeyError) as context: graph.del_edge(('p', 'r')) self.assertEqual(repr(('p', 'r')), str(context.exception)) self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__add_edge__2(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.add_edge(('p', 'r'), '3') self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'}) self.assertEqual(graph.edges.data, { ('p', 'q'): '1', ('q', 'r'): '2', ('p', 'r'): '3' })
def test__add_node__4(self): graph = graph_.Graph({ 'p': 'P', 'q': 'Q', 'r': 'R' }, { ('p', 'q'): '1', ('q', 'r'): '2' }) graph.add_node('s', 'S') self.assertEqual(graph.nodes.data, { 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S' }) self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
def test__repr__(self): graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')]) self.assertEqual( repr(graph), "Graph(%s, %s)" % (repr(graph.nodes), repr(graph.edges)))
def test__init__inconsistent_2(self): with self.assertRaises(KeyError) as context: graph_.Graph(['q', 'r'], [('p', 'q'), ('q', 'r')]) self.assertEqual(repr('p'), str(context.exception))
def test__leafs(self): graph = graph_.Graph(['p', 'q', 'r', 's', 't'], [('p', 'q'), ('p', 'r'), ('s', 'r')]) self.assertEqual(graph.leafs(), set(['q', 'r', 't']))
def test__isolated(self): graph = graph_.Graph(['p', 'q', 'r', 's', 't'], [('p', 'q'), ('p', 'r'), ('s', 'r')]) self.assertEqual(graph.isolated(), set(['t']))
def test__incident__KeyError(self): graph = graph_.Graph(['p', 'q'], [('p', 'q')]) with self.assertRaises(KeyError) as context: graph.incident('x') self.assertEqual(repr('x'), str(context.exception))
def test__edge__KeyError(self): graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')]) with self.assertRaises(KeyError) as context: graph.edge(('x', 'y')) self.assertEqual(repr(('x', 'y')), str(context.exception))
def test__add_edge__ValueError_2(self): graph = graph_.Graph({'p': 'P', 'q': 'Q'}, {('p', 'q'): '1'}) with self.assertRaises(ValueError) as context: graph.add_edge(('q', 'r', 's'))
def test__incident__1(self): graph = graph_.Graph(['p', 'q'], [('p', 'q')]) self.assertEqual(type(graph.incident('q')), set)
def test__edge__2(self): graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')]) self.assertIsNone(graph.edge(('p', 'q'))) self.assertIsNone(graph.edge(('q', 'r')))
def test__outward__1(self): graph = graph_.Graph(['p', 'q'], [('p', 'q')]) self.assertEqual(type(graph.outward('p')), set)
def test__neighbors__1(self): graph = graph_.Graph(['p', 'q'], [('p', 'q')]) self.assertEqual(type(graph.neighbors('p')), set)
def graph2(self): nodes = ['p', 'q', 'r', 's', 't', 'u', 'x'] edges = [('p', 'q'), ('q', 'r'), ('q', 's'), ('s', 'p'), ('s', 't'), ('r', 'u')] return graph_.Graph(nodes, edges)
def test__predecessors__1(self): graph = graph_.Graph(['p', 'q'], [('p', 'q')]) self.assertEqual(type(graph.predecessors('p')), set)