예제 #1
0
def test_search_no_results(kb: KB, apple):
    response = kb.search(q="invalid")
    assert [] == response.nodes

    response = kb.search(keys=["Apple, Inc.|COMPANY"], labels=["INVALID"])
    assert [] == response.nodes

    response = kb.search(labels=["INVALID"])
    assert [] == response.nodes

    response = kb.search(limit=0)
    assert [] == response.nodes
예제 #2
0
def test_search_with_results(kb: KB, apple, google):
    kb.save_node(apple)
    kb.save_node(google)
    kb.reindex()

    # default (all nodes, no filter, etc.)
    response = kb.search()
    assert [apple, google] == response.nodes

    # offset = 1, skips 1 node
    response = kb.search(offset=1)
    assert [google] == response.nodes

    # limit = 1
    response = kb.search(limit=1)
    assert [apple] == response.nodes

    # prefix
    response = kb.search(q="a")
    assert [apple] == response.nodes

    # keys
    response = kb.search(keys=["Apple, Inc.|COMPANY"])
    assert [apple] == response.nodes

    # keys
    response = kb.search(keys=[apple.key, apple.key, "junk"])
    assert [apple] == response.nodes

    # labels
    response = kb.search(labels=["COMPANY"])
    assert 2 == len(response.nodes)

    # keys + labels
    response = kb.search(keys=["Apple, Inc.|COMPANY"], labels=["COMPANY"])
    assert [apple] == response.nodes

    # dict
    assert response.dict() == {
        "nodes": [{
            "data": None,
            "headquarters": {
                "city": "Cupertino",
                "data": None,
                "key": "1",
                "label": "LOCATION",
            },
            "key": "Apple, Inc.|COMPANY",
            "label": "COMPANY",
            "name": "Apple, Inc.",
            "synonyms": ("Apple", "AAPL"),
        }],
        "trails": [{
            "end": "Apple, Inc.|COMPANY",
            "hops": [],
            "start": "Apple, Inc.|COMPANY",
        }],
    }
예제 #3
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
예제 #4
0
def test_search_with_just_text(kb: KB, apple, google):
    kb.save_node(apple)
    kb.save_node(google)
    kb.reindex()

    response = kb.search("ap")
    assert 1 == len(response)
    assert [apple] == response.nodes
    assert apple == response[0]
    assert [apple] == list(response)