def test_get_path(self): g = nx.Graph() g.add_nodes_from([1, 2, 3, 4]) g.add_edge(1, 3) g.add_edge(3, 4) expected = [1, 3, 4] self.assertEqual(gu.get_path(g, 1, 4), expected)
def path_graph_from_ids(source_num, target_num, unigraph): """ Takes the node ids for source and target and the graph and returns a graph for the path between source and target """ # Use the ids provided to make urls (which are the actual node ids used in the graph) source_id = "http://eprints.gla.ac.uk/view/author/" + source_num + ".html" target_id = "http://eprints.gla.ac.uk/view/author/" + target_num + ".html" # Get the set of nodes in the graph to check for the source and target uni_node_set = graph_utils.get_node_set(unigraph) # Check if source and target in graph, if not return error message if source_id not in uni_node_set and target_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, neither author was found"}) return HttpResponse(errorMessage, content_type='application/json') elif source_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, the source author was not found"}) return HttpResponse(errorMessage, content_type='application/json') elif target_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, the target author was not found"}) return HttpResponse(errorMessage, content_type='application/json') # If both were found, get the path betwen them s_path = graph_utils.get_path(unigraph, source_id, target_id) # If s_path is false, no path was found between the authors, return error message if not s_path: errorMessage = json.dumps({"error": "Sorry, no path was found between the authors"}) return HttpResponse(errorMessage, content_type='application/json') # Otherwise make a graph connecting the path between the authors and return path_graph = graph_utils.make_path_graph(s_path, unigraph) graphdata = graph_utils.json_from_graph(path_graph) newdata = json.dumps(graphdata) return HttpResponse(newdata, content_type='application/json')