예제 #1
0
def test_repr():
    g = Graph()
    g.add_node('a')
    g.add_node('b')

    actual = g
    expected = 'a : [],b : [] '
    assert str(actual) == expected
예제 #2
0
def test_get_nodes():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    expected = ['a', 'b']
    actual = []
    [actual.append(item.value) for item in g.get_nodes()]
    assert actual == expected
예제 #3
0
def test_graph_gets_neighbors():
    graph = Graph()
    node = graph.add_node('node')
    node_two = graph.add_node('node_two')
    graph.add_edge(node, node_two)
    neighbor = graph.get_neighbor(node)
    neighbor_two = neighbor[0]
    assert neighbor_two.vertex.value == 'node_two'
    assert len(neighbor) == 1
예제 #4
0
def test_add_edge_sunny():

    graph = Graph()

    start = graph.add_node("start")

    end = graph.add_node("end")

    graph.add_edge(start, end)
예제 #5
0
def test_graph_neighbors():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    c = graph.add_node("c")
    edge_ab = graph.add_edge(a, b)
    edge_ac = graph.add_edge(a, c)
    actual = graph.get_neighbors(a)
    expected = [edge_ab, edge_ac]
    assert actual == expected
예제 #6
0
def test_add_edge_with_weight():

    graph = Graph()

    start = graph.add_node("start")

    end = graph.add_node("end")

    weight = 10

    graph.add_edge(start, end, weight)
예제 #7
0
def test_size_one():

    graph = Graph()

    graph.add_node("spam")

    expected = 1

    actual = graph.size()

    assert actual == expected
예제 #8
0
def test_size_two():

    graph = Graph()

    graph.add_node("spam")

    graph.add_node("bacon")

    expected = 2

    actual = graph.size()

    assert actual == expected
예제 #9
0
def test_get_nodes():

    graph = Graph()

    graph.add_node("banana")

    graph.add_node("apple")

    expected = 2

    actual = len(graph.get_nodes())

    assert actual == expected
예제 #10
0
def test_get_neighbors_returns_edges_with_custom_weight():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana, 44)

    neighbors = graph.get_neighbors(apple)

    neighbor_edge = neighbors[0]

    assert neighbor_edge.weight == 44
예제 #11
0
def test_size():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    expected = 2
    actual = graph.size()
    assert actual == expected
예제 #12
0
def test_graph_neighbors_one_node_one_edge():
    graph = Graph()
    a = graph.add_node("a")
    edge_a = graph.add_edge(a, a)
    actual = graph.get_neighbors(a)
    expected = [edge_a]
    assert actual == expected
예제 #13
0
def test_add_node_returns_vertex():

    graph = Graph()

    vertex = graph.add_node("spam")

    assert isinstance(vertex, Vertex)
예제 #14
0
def test_graph_nodes_list():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    actual = graph.get_nodes()
    expected = [a, b]
    assert actual == expected
예제 #15
0
def test_add_edge_true():
    g = Graph()
    a = g.add_node('a')
    b = g.add_node('b')
    g.add_edge(a, b)

    assert True
예제 #16
0
def test_get_neighbors_returns_edges_with_default_weight():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana)

    neighbors = graph.get_neighbors(apple)

    actual = neighbors[0].weight

    expected = 0

    assert actual == expected
예제 #17
0
def test_get_neigh():
    g = Graph()
    a = g.add_node('a')
    b = g.add_node('b')
    g.add_edge(a, b)
    actual = g.get_neighbors(a)
    expected = [('b', 1)]
    assert actual == expected
예제 #18
0
def test_one_node_one_edge():
    g = Graph()
    a = g.add_node('a')
    g.add_edge(a, a)
    actual = g.get_neighbors(a)
    expected = [('a', 1)]

    assert actual == expected
예제 #19
0
def test_add_node_return_has_correct_value():

    graph = Graph()

    expected = "spam"  # a vertex's value that comes back

    actual = graph.add_node("spam").value

    assert actual == expected
예제 #20
0
def test_get_neighbors_none():

    graph = Graph()

    banana = graph.add_node("banana")

    neighbors = graph.get_neighbors(banana)

    assert len(neighbors) == 0
예제 #21
0
def test_get_neighbors_returns_edges():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana)

    neighbors = graph.get_neighbors(apple)

    assert len(neighbors) == 1

    neighbor = neighbors[0]

    assert isinstance(neighbor, Edge)

    assert neighbor.vertex.value == 'banana'
예제 #22
0
def test_add_edge_interloper_end():

    graph = Graph()

    end = Vertex("end")

    start = graph.add_node("start")

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
def flights():
    graph = Graph()
    pandora = graph.add_node("pandora")
    narnia = graph.add_node("narnia")
    arendelle = graph.add_node("arendelle")
    metroville = graph.add_node("metroville")
    monstropolis = graph.add_node("monstropolis")
    naboo = graph.add_node("naboo")
    graph.add_edge(pandora, arendelle, 150)
    graph.add_edge(pandora, metroville, 82)
    graph.add_edge(narnia, metroville, 37)
    graph.add_edge(narnia, naboo, 250)
    graph.add_edge(metroville, pandora, 82)
    graph.add_edge(metroville, arendelle, 99)
    graph.add_edge(metroville, monstropolis, 105)
    graph.add_edge(metroville, naboo, 26)
    graph.add_edge(metroville, narnia, 37)
    graph.add_edge(arendelle, pandora, 150)
    graph.add_edge(arendelle, metroville, 99)
    graph.add_edge(arendelle, monstropolis, 42)
    graph.add_edge(monstropolis, arendelle, 42)
    graph.add_edge(monstropolis, metroville, 105)
    graph.add_edge(monstropolis, naboo, 73)
    graph.add_edge(naboo, monstropolis, 73)
    graph.add_edge(naboo, metroville, 26)
    graph.add_edge(naboo, narnia, 250)
    return {
        "graph": graph,
        "pandora": pandora,
        "narnia": narnia,
        "arendelle": arendelle,
        "metroville": metroville,
        "monstropolis": monstropolis,
        "naboo": naboo,
    }
예제 #24
0
def test_breadth_first():

    g = Graph()

    pandora = g.add_node("Pandora")

    arendelle = g.add_node("Arendelle")

    metroville = g.add_node("Metroville")

    monstropolis = g.add_node("Monstropolis")

    narnia = g.add_node("Narnia")

    naboo = g.add_node("Naboo")

    g.add_edge(pandora, arendelle)
    g.add_edge(arendelle, pandora)

    g.add_edge(arendelle, metroville)
    g.add_edge(metroville, arendelle)

    g.add_edge(arendelle, monstropolis)
    g.add_edge(monstropolis, arendelle)

    g.add_edge(metroville, monstropolis)
    g.add_edge(monstropolis, metroville)

    g.add_edge(metroville, narnia)
    g.add_edge(narnia, metroville)

    g.add_edge(metroville, naboo)
    g.add_edge(naboo, metroville)

    g.add_edge(narnia, naboo)
    g.add_edge(naboo, narnia)

    values = []

    g.breadth_first(pandora, lambda v: values.append(v.value))

    assert values == [
        "Pandora", "Arendelle", "Metroville", "Monstropolis", "Narnia", "Naboo"
    ]
예제 #25
0
def test_add_node():
    graph = Graph()
    expected = 'a'
    actual = graph.add_node('a').value
    assert expected == actual
예제 #26
0
def test_get_all_nodes():
    graph = Graph()
    node = graph.add_node('node')
    node_two = graph.add_node('node_two')
    assert len(graph.get_nodes()) == 2
예제 #27
0
def test_add_vertex():
    g = Graph()
    expected = 'a'
    actual = g.add_node('a').value

    assert expected == actual
예제 #28
0
def test_graph_size_fail():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    graph.add_edge(a, b)
    assert len(graph._adjacency_list) != 3
예제 #29
0
def test_add_edge_true():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    graph.add_edge(a, b)
    assert True
예제 #30
0
def test_add_node():
    graph = Graph()
    expected = "a"
    actual = graph.add_node(expected).value
    assert expected == actual