Exemplo n.º 1
0
async def test_search():
    anchor = SGNode(0)
    graph = LocalGraph(anchor)

    for key in range(5, 100, 5):
        result = await graph.insert(key)
        assert result.key == key

    validate_graph(graph)

    assert (await graph.search(0)).key == 0
    node_5 = await graph.search(5)
    assert node_5.key == 5

    with pytest.raises(NotFound):
        await graph.search(6)

    graph.cursor = node_5
    with pytest.raises(NotFound):
        await graph.search(6)
    with pytest.raises(NotFound):
        await graph.search(4)

    node_80 = await graph.search(80)
    assert node_80.key == 80
Exemplo n.º 2
0
async def do_test_skip_graph_search_fuzz(anchor_key, keys_to_insert,
                                         keys_to_search, random_module):
    anchor = SGNode(anchor_key)
    graph = LocalGraph(anchor)

    inserted = {anchor_key}

    for key in keys_to_insert:
        graph.cursor = graph.db.get(random.choice(tuple(inserted)))
        if key in inserted:
            with pytest.raises(AlreadyPresent):
                await graph.insert(key)
        else:
            node = await graph.insert(key)
            assert node.key == key
            inserted.add(key)

    validate_graph(graph)

    for key in keys_to_search:
        graph.cursor = graph.db._db[random.choice(tuple(inserted))]

        if key == anchor_key or key in keys_to_insert:
            node = await graph.search(key)
            assert node.key == key
        else:
            with pytest.raises(NotFound):
                await graph.search(key)
Exemplo n.º 3
0
async def test_insert_far_left():
    anchor = SGNode(1)
    graph = LocalGraph(anchor)

    node = await graph.insert(0)
    assert node.key == 0
    assert node.get_neighbor(0, RIGHT) == anchor.key
    assert node.get_neighbor(0, LEFT) is None

    validate_graph(graph)
Exemplo n.º 4
0
async def test_insert_far_right():
    anchor = SGNode(0)
    graph = LocalGraph(anchor)

    node = await graph.insert(1)
    assert node.key == 1
    assert node.get_neighbor(0, LEFT) == anchor.key
    assert node.get_neighbor(0, RIGHT) is None

    assert node.get_neighbor(node.max_level, LEFT) is None
    assert node.get_neighbor(node.max_level, RIGHT) is None

    validate_graph(graph)
Exemplo n.º 5
0
async def test_delete(key_order):
    anchor = SGNode(4)
    graph = LocalGraph(anchor)

    for key in sorted(key_order):
        await graph.insert(key)

    validate_graph(graph)

    assert all(key in graph.db._db for key in key_order)
    for key in key_order:
        await graph.search(key)
        await graph.delete(key)
        with pytest.raises(NotFound):
            await graph.search(key)

        validate_graph(graph)
Exemplo n.º 6
0
async def test_insert_sequential_to_the_correct_mixed_order():
    anchor = SGNode(0)
    graph = LocalGraph(anchor)

    node_3, node_1, node_2 = tuple(
        [await graph.insert(key) for key in (3, 1, 2)])

    validate_graph(graph)

    assert anchor.get_neighbor(0, LEFT) is None
    assert anchor.get_neighbor(0, RIGHT) == 1

    assert node_1.get_neighbor(0, LEFT) == 0
    assert node_1.get_neighbor(0, RIGHT) == 2

    assert node_2.get_neighbor(0, LEFT) == 1
    assert node_2.get_neighbor(0, RIGHT) == 3

    assert node_3.get_neighbor(0, LEFT) == 2
    assert node_3.get_neighbor(0, RIGHT) is None