def test_add_node_matrix_out_of_order(): graph = AdjacencyMatrix({}) gold_position = random.randint(10, 100) graph._add_vertex = MagicMock(return_value=gold_position) label = rand_str() with pytest.raises(ValueError): graph.add_vertex(label) graph._add_vertex.assert_called_once_with(label)
def test_add_edge_greater_than_zero(): graph = AdjacencyMatrix( [rand_str() for _ in range(random.randint(10, 100))]) source = target = 0 while source == target: source = random.randint(0, graph.vertex_count - 1) target = random.randint(0, graph.vertex_count - 1) weight = (random.random() + 0.01) * -1 with pytest.raises(ValueError): graph.add_edge(source, target, weight)
def test_add_node_matrix(): verts = [rand_str() for _ in range(random.randint(1, 10))] graph = AdjacencyMatrix(verts) graph._adjacency_matrix = np.random.rand(*graph._adjacency_matrix.shape) gold_position = len(verts) graph._add_vertex = MagicMock(return_value=gold_position) gold_label = rand_str() idx = graph.add_vertex(gold_label) graph._add_vertex.assert_called_once_with(gold_label) assert idx == gold_position assert (graph.adjacency_matrix[:, idx] == 0).all() assert (graph.adjacency_matrix[idx, :] == 0).all()
def test_add_edge_matrix(): graph = AdjacencyMatrix( [rand_str() for _ in range(random.randint(10, 100))]) source = target = 0 while source == target: source = random.randint(0, graph.vertex_count - 1) target = random.randint(0, graph.vertex_count - 1) weight = random.random() source_req = random.choice([source, graph[source]]) target_req = random.choice([target, graph[target]]) graph.add_edge(source_req, target_req, weight) assert graph.adjacency_matrix[source, target] == weight
def test_edge_count_matrix(): graph = AdjacencyMatrix({}) gold = random.randint(10, 100) verts = random.randint(15, 30) edges = np.zeros((verts, verts)) for _ in range(gold): added = False while not added: src, tgt = np.random.randint(0, verts, size=(2, )) if src == tgt: continue if edges[src, tgt] == 0: edges[src, tgt] = 1 added = True graph._adjacency_matrix = edges assert graph.edge_count == gold
def test_text_rank_mining_massive_datasets(): """"This is testing with a worked example from here: http://infolab.stanford.edu/~ullman/mmds/ch5.pdf """ g = AdjacencyMatrix(list("ABCD")) g.add_edge("A", "B") g.add_edge("A", "C") g.add_edge("A", "D") g.add_edge("B", "A") g.add_edge("B", "D") g.add_edge("C", "A") g.add_edge("D", "B") g.add_edge("D", "C") gold = np.array([3 / 9, 2 / 9, 2 / 9, 2 / 9]) scores = [x[1] for x in text_rank(g, damping=1, convergence=0)] np.testing.assert_allclose(scores, gold)
def random_graphs(p=None, min_vert=10, max_vert=100): p = random.random() if p is None else p verts = [str(i) for i in range(random.randint(min_vert, max_vert))] graph = AdjacencyMatrix(verts) graph2 = AdjacencyList(verts) for src, tgt in combinations(verts, 2): if src == tgt: continue if random.random() <= p: graph.add_edge(src, tgt, 1) graph2.add_edge(src, tgt, 1) graph.add_edge(tgt, src, 1) graph2.add_edge(tgt, src, 1) return graph, graph2
def test_vertex_count_matrix(): graph = AdjacencyMatrix({}) verts = random.randint(15, 30) for vert in range(verts): graph.add_vertex(str(vert)) assert graph.vertex_count == verts