def test_remove_hub_link(self): g = GraphBackend().add_hub('foo', 'bar').link('foo', 'bar') g.remove_hub('bar') self.assertTrue(g.is_hub('foo')) self.assertFalse(g.is_hub('bar')) self.assertEqual(g.hubs(), set(['foo'])) self.assertEqual(g.links('foo'), set(['bar']))
def test_remove_hub_followed_by_others(self): g = GraphBackend().add_hub('h1', 'h2')\ .link('h1', 'h2').link('h2', 'h1') g.remove_hub('h1') self.assertFalse(g.is_hub('h1')) self.assertTrue(g.is_hub('h2')) self.assertEqual(g.links('h2'), set(['h1']))
def test_remove_hub_without_link(self): g = GraphBackend() with self.assertRaises(Exception) as exc: g.remove_hub('foo') self.assertEqual(exc.exception.message, "Hub 'foo' does not exist") g.add_hub('foo') g.remove_hub('foo') # ensure hub is deleted self.assertFalse(g.is_hub('foo')) self.assertEqual(g.hubs(), set()) # try to unlink a node from 'foo' with self.assertRaises(Exception) as exc: g.unlink('foo', 'bar') self.assertEqual(exc.exception.message, "Hub 'foo' does not exist")
def test_remove_node_link(self): g = GraphBackend() g.add_hub('foo') g.link('foo', 'bar') self.assertEqual(g.links('foo'), set(['bar'])) self.assertEqual(g.links('bar'), set(['foo'])) with self.assertRaises(Exception) as exc: g.remove_hub('foo') self.assertEqual(exc.exception.message, "Can't remove hub with connected nodes") g.unlink('foo', 'bar') self.assertTrue(g.is_hub('foo')) self.assertFalse(g.is_hub('bar')) self.assertEqual(g.hubs(), set(['foo'])) self.assertEqual(g.links('foo'), set())
def test_remove_node_link(self): g = GraphBackend() g.add_hub('foo') g.link('foo', 'bar') self.assertEqual(g.links('foo'), set(['bar'])) self.assertEqual(g.links('bar'), set(['foo'])) with self.assertRaises(Exception) as exc: g.remove_hub('foo') self.assertEqual( exc.exception.message, "Can't remove hub with connected nodes" ) g.unlink('foo', 'bar') self.assertTrue(g.is_hub('foo')) self.assertFalse(g.is_hub('bar')) self.assertEqual(g.hubs(), set(['foo'])) self.assertEqual(g.links('foo'), set())
def test_remove_hub_without_link(self): g = GraphBackend() with self.assertRaises(Exception) as exc: g.remove_hub('foo') self.assertEqual( exc.exception.message, "Hub 'foo' does not exist" ) g.add_hub('foo') g.remove_hub('foo') # ensure hub is deleted self.assertFalse(g.is_hub('foo')) self.assertEqual(g.hubs(), set()) # try to unlink a node from 'foo' with self.assertRaises(Exception) as exc: g.unlink('foo', 'bar') self.assertEqual( exc.exception.message, "Hub 'foo' does not exist" )
def test_remove_hub_with_linked_hubs(self): g = GraphBackend().add_hub('h1', 'h2', 'h3')\ .link('h1', 'h2').link('h1', 'h3').link('h2', 'h3') self.assertEqual(g.hub_links('h1'), set(['h2', 'h3'])) self.assertEqual(g.hub_links('h2'), set(['h3'])) self.assertEqual(g.hub_links('h3'), set()) g.remove_hub('h1') # remove node because nobody follows it self.assertFalse('h1' in g.hubs()) with self.assertRaises(Exception) as exc: g.links('h1') self.assertEqual(exc.exception.message, "Unknown node 'h1'") self.assertEqual(g.links('h2'), set(['h3'])) self.assertEqual(g.links('h3'), set()) with self.assertRaises(Exception) as exc: g.hub_links('h1') self.assertEqual(exc.exception.message, "Hub 'h1' does not exist") g.unlink('h2', 'h3') self.assertTrue('h2' in g.hubs()) self.assertTrue('h3' in g.hubs()) self.assertEqual(g.links('h2'), set()) self.assertEqual(g.links('h3'), set())