def test_node_type_Node(self): """Test case: Check that node is a Node object """ g = Graph() with self.assertRaises(TypeError) as context: g.addNode('A') self.assertTrue('A is not of type Node' in context.exception)
def test_duplicate_node(self): """Test case: Each node is unique """ g = Graph() g.addNode(Node('A')) with self.assertRaises(ValueError) as context: g.addNode(Node('A')) self.assertTrue('Duplicate node' in context.exception)
def test_add_edge(self): g = Graph() n1 = Node('A') n2 = Node('B') g.addNode(n1) g.addNode(n2) g.addEdge(Edge(n1, n2)) # A --> B self.assertIn(n2, g.edges[n1]) self.assertEqual([], g.edges[n2])
def test_edge_added_from_to__non_existing_node(self): """Test case: Check when an edge is added from or to a non existing node """ g = Graph() g.addNode(Node('A')) g.addNode(Node('B')) with self.assertRaises(ValueError) as context: g.addEdge(Edge(Node('A'), Node('D'))) # A --> D g.addEdge(Edge(Node('D'), Node('A'))) # D --> A self.assertTrue('Node: A and Node: D must be in the graph' in context.exception)
def test_duplicate_edge(self): g = Graph() g.addNode(Node('A')) g.addNode(Node('B')) g.addEdge(Edge(Node('A'), Node('B'))) # A --> B with self.assertRaises(ValueError) as context: g.addEdge(Edge(Node('A'),Node('B'))) self.assertTrue('The edge (Node: A, Node: B) is already in the graph' in context.exception)
def test_method_getitem_of_Graph(self): """Test case: * Nodes A, B. C, D. Neighbors of A: B and D """ g = Graph() g.addNode(Node('A')) g.addNode(Node('B')) g.addNode(Node('C')) g.addNode(Node('D')) g.addEdge(Edge(Node('A'), Node('B'))) # A --> B g.addEdge(Edge(Node('A'), Node('D'))) # A --> D self.assertIn(Node('B'), g[Node('A')]) self.assertIn(Node('D'), g[Node('A')])
def test_method_has_key_of_Graph(self): """Test case: * check that node is a Node object * if the key is available in the dict, return True """ g = Graph() with self.assertRaises(TypeError) as context: g.has_key('A') self.assertTrue('A is not of type Node' in context.exception) n1 = Node('A') g.addNode(n1) self.assertTrue(g.has_key(n1))
def test_edge_type_Edge(self): """Test case: Check that edge is a Edge object """ g = Graph() with self.assertRaises(TypeError) as context: g.addEdge(('A','B'))
def test_add_node(self): """Test case: Check the node is in graph.nodes """ g = Graph() g.addNode(Node('A')) self.assertIn('A', g.nodes)