Exemplo n.º 1
0
def test_searcher(graph):
    searcher = DefaultSearcher(graph=graph, traversal=T(), starts=[apple])
    trails = list(searcher)
    assert 1 == len(trails)
    assert {apple.key} == ends(trails)

    searcher = DefaultSearcher(graph=graph, traversal=T(), starts=[apple.key])
    trails = list(searcher)
    assert 1 == len(trails)
    assert {apple.key} == ends(trails)
Exemplo n.º 2
0
def test_all_nodes_optional_attribute(graph):
    t = T().all_nodes(max_hops=None).include(F.price > 3.00)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert 1 == len(trails)
    assert {honeycrisp.key} == ends(trails)

    t = T().all_nodes(max_hops=None).exclude(F.price > 3.00)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert 21 == len(trails)
    assert honeycrisp.key not in ends(trails)
Exemplo n.º 3
0
def test_query_exclude_by_label(graph):
    t = T().in_nodes().exclude(F.label == "SAUCE")
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[dessert])
    trails = list(searcher)

    assert {pie.key} == {r.end for r in trails}
    assert {dessert.key} == {r.start for r in trails}

    t = T().in_nodes().exclude(F.label != "SAUCE")
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[dessert])
    trails = list(searcher)

    assert {apple_sauce.key} == {r.end for r in trails}
    assert {dessert.key} == {r.start for r in trails}
Exemplo n.º 4
0
def test_has_apple_include_pies(graph):
    t = T().in_nodes(Verb.HAS_A).include(Verb.is_a >> pie)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {apple_pie.key} == ends(trails)
    assert {apple.key} == {r.start for r in trails}
Exemplo n.º 5
0
def test_all_nodes_all_verbs_no_max(graph):
    t = T().all_nodes(max_hops=None)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {apple.key} == starts(trails)
    assert 22 == len(trails)
Exemplo n.º 6
0
def test_multi_result_hops(graph):
    t = T().out_nodes(Verb.IS_A, max_hops=4)
    searcher = DefaultSearcher(graph=graph,
                               traversal=t,
                               starts=(apple_pie, apple_sauce))
    trails = list(searcher)
    assert ends(trails).issuperset({dessert.key})
Exemplo n.º 7
0
def test_out_nodes(graph):
    t = T().out_nodes(Verb.IS_A)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {fruit.key} == ends(trails)
    assert {apple.key} == starts(trails)
Exemplo n.º 8
0
def test_in_nodes_with_passthru(graph):
    t = T().in_nodes(Verb.IS_A, passthru=True)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {apple.key, granny_smith.key, honeycrisp.key} == ends(trails)
    assert {apple.key} == starts(trails)
Exemplo n.º 9
0
def test_in_nodes(graph):
    t = T().in_nodes(Verb.IS_A)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {granny_smith.key, honeycrisp.key} == ends(trails)
    assert {apple.key} == {r.start for r in trails}
Exemplo n.º 10
0
def test_in_has_a_apple_out_is_a(graph):
    t = T().in_nodes(Verb.HAS_A).out_nodes(Verb.IS_A)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {dessert.key, pie.key} == ends(trails)
    assert {apple.key} == starts(trails)
Exemplo n.º 11
0
def test_start_all_goal_all(graph):
    searcher = DefaultSearcher(graph=graph, traversal=T(), starts=graph)
    trails = list(searcher)

    assert 9 == len(trails)
    for result in trails:
        assert 0 == len(result)
        assert result.start == result.end
Exemplo n.º 12
0
def test_save_for_entity_and_edge(kb: KB, apple, google):
    assert apple == kb.save(apple)
    assert google == kb.save(google)
    kb.reindex()

    assert 2 == len(kb)
    assert apple == kb.get_node(apple.key)

    kb.connect(start=apple, verb="IS_A", end=apple)
    kb.reindex()

    assert kb.info()["graph"] == {
        "nodes": 2,
        "edges": 1,
    }

    assert 2 == len(kb.get_edges(node_key=apple))
    assert 1 == len(kb.get_edges(node_key=apple, direction=Direction.incoming))
    assert 2 == len(kb.get_edges(node_key=apple, verb="IS_A"))
    assert 1 == len(kb.get_edges(node_key=apple, verb="IS_A", limit=1))
    assert 0 == len(kb.get_edges(node_key=apple, verb="IS_NOT"))

    assert apple.key == kb.get_neighbors(apple).neighbors[0].key
    assert ([] == kb.get_neighbors(apple,
                                   verb="IS_NOT",
                                   direction=Direction.outgoing).neighbors)

    kb.save(Edge(start=apple, verb="POINTS_NO_WHERE", end="INVALID|THING"))
    kb.save(Edge(start=apple, verb="POINTS_NO_WHERE", end=google))
    kb.reindex()

    assert kb.info()["graph"] == {
        "nodes": 2,
        "edges": 3,
    }

    t = T().all_nodes(passthru=True)
    response = kb.search(q="a", traversal=t)
    assert 3 == len(response.nodes)

    kb.remove_node(apple.key)
    kb.reindex()

    assert kb.info()["graph"] == {
        "nodes": 1,
        "edges": 3,
    }

    kb.clean_edges()

    assert kb.info()["graph"] == {
        "nodes": 1,
        "edges": 0,
    }

    data = response.dict()
    compare = SearchResponse(**data)
    assert compare.nodes == response.nodes
Exemplo n.º 13
0
def test_in_nodes_all_verbs(graph):
    t = T().in_nodes()
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)

    assert {
        granny_smith.key,
        honeycrisp.key,
        apple_sauce.key,
        apple_pie.key,
    } == ends(trails)
    assert {apple.key} == starts(trails)
Exemplo n.º 14
0
def test_in_nodes_with_max_hops(graph):
    t = T().in_nodes(Verb.IS_A, max_hops=2)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[food])
    trails = list(searcher)

    assert {
        fruit.key,
        apple.key,
        dessert.key,
        pie.key,
        apple_sauce.key,
    } == ends(trails)
    assert {food.key} == starts(trails)
Exemplo n.º 15
0
def test_roundtrip():
    request = SearchRequest(traversal=T().in_nodes().include(F.label == "A"))

    data = request.dict()
    assert data == {
        "q":
        None,
        "keys": [],
        "labels": [],
        "limit":
        100,
        "offset":
        0,
        "traversal": [
            {
                "directions": [Direction.incoming],
                "max_hops": 1,
                "passthru": False,
                "verbs": [],
            },
            {
                "all":
                False,
                "criteria": [{
                    "compare": Comparison.exact,
                    "field": "label",
                    "type": "field",
                    "value": "A",
                }],
                "exclude":
                False,
                "skip_limit":
                1000,
            },
        ],
    }

    assert SearchRequest(**data).dict() == data
Exemplo n.º 16
0
def test_exclude_is_a(graph):
    t = T().in_nodes(Verb.HAS_A).exclude(Verb.is_a >> pie)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {apple_sauce.key} == ends(trails)
Exemplo n.º 17
0
def test_include_adjacent_to_pie(graph):
    t = T().in_nodes(max_hops=3).include(Verb.is_a**pie)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[food])
    trails = list(searcher)
    assert {dessert.key, apple_pie.key} == ends(trails)
Exemplo n.º 18
0
def test_include_what_an_apple_is(graph):
    t = T().in_nodes(max_hops=3).include(Verb.is_a << apple)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[food])
    trails = list(searcher)
    assert {fruit.key} == ends(trails)
Exemplo n.º 19
0
def test_comparison_options(graph):
    t = T().in_nodes(Verb.IS_A).include(F.price < 3.00)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price <= 1.99)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price < 1.99)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert set() == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price > 3.00)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {honeycrisp.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price >= 3.99)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {honeycrisp.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price.is_in((3.99, 1.99)))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key, honeycrisp.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price > 3.99)
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert set() == ends(trails)

    t = (T().in_nodes(Verb.IS_A).include(F.price > 2.00,
                                         F.price < 3.00,
                                         all=True))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert set() == ends(trails)

    t = (T().in_nodes(Verb.IS_A).include(F.price > 2.00,
                                         F.price < 3.00,
                                         all=False))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {honeycrisp.key, granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.contains("Smith"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.contains("smith"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert set() == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.icontains("SMITH"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).exclude(F.name.iexact("honeycrisp"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).exclude(F.name.startswith("Hone"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).exclude(F.name.istartswith("hone"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.endswith("Smith"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.iendswith("SMITH"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.price.range(1.50, 5))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key, honeycrisp.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.iendswith("SMITH"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {granny_smith.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.regex("^[A-Za-z]*$"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {honeycrisp.key} == ends(trails)

    t = T().in_nodes(Verb.IS_A).include(F.name.iregex(r"^[\w\s]+$"))
    searcher = DefaultSearcher(graph=graph, traversal=t, starts=[apple])
    trails = list(searcher)
    assert {honeycrisp.key, granny_smith.key} == ends(trails)