예제 #1
0
def test_labels():
    assert Node.get_default_label() == "NODE"
    assert Node.get_all_labels() == {"NODE"}

    assert Entity.get_default_label() == "ENTITY"
    assert Entity.get_all_labels() == {"ENTITY"}

    assert CustomNode.get_default_label() == "SOMETHING_ELSE"
    assert CustomNode.get_all_labels() == {"SOMETHING_ELSE"}
예제 #2
0
def test_edge():
    start = Node()
    end = Node()
    edge = Edge(start=start, end=end, verb="IS_A")
    assert edge.dict() == {
        "start": start.key,
        "verb": "IS_A",
        "end": end.key,
        "data": None,
    }

    two = start >> "IS_A" >> end
    assert two == edge
    assert two.dict() == edge.dict()

    three = end << "IS_A" << start
    assert three == edge
    assert three.dict() == edge.dict()

    edge.set_verb("HAS_A")
    assert edge.dict() == {
        "start": start.key,
        "verb": "HAS_A",
        "end": end.key,
        "data": None,
    }

    data = dict(a="b")
    edge.set_data(data)
    assert edge.dict() == {
        "start": start.key,
        "verb": "HAS_A",
        "end": end.key,
        "data": {
            "a": "b"
        },
    }
def test_node_create():
    registry = Registry()
    assert isinstance(registry.create(Node, Node()), Node)
    assert isinstance(registry.create(Node, {}), Node)
    assert isinstance(registry.create(Node), Node)
    assert isinstance(registry.create(Node, label="NODE"), Node)
    assert isinstance(registry.create(Entity, label="NODE"), Node)

    assert isinstance(
        registry.create(Entity, name="abc", label="ENTITY"), Entity
    )
    assert isinstance(registry.create(Entity, name="abc"), Entity)

    assert isinstance(registry.create(CustomNode, {}), CustomNode)
    assert isinstance(registry.create(Node, label="CUSTOM_NODE"), CustomNode)
예제 #4
0
def test_node_cache(root, normalizer):
    node = Node()
    index = NodeIndex(root, normalizer)

    index.save(node)
    assert node == index.get(node.key)
    assert node in index
    assert node.key in index
    assert 1 == len(index)

    index.remove(node)
    assert index.get(node.key) is None
    assert node not in index
    assert node.key not in index
    assert 0 == len(index)
예제 #5
0
def test_node():
    empty = Node()
    assert 36 == len(empty.key)
    assert empty.dict() == dict(key=empty.key, label="NODE", data=None)

    node = Node(key="ENTITY|LABEL", label="LABEL")
    assert node.dict() == dict(key="ENTITY|LABEL", label="LABEL", data=None)

    data = packb(node, default=pydantic_encoder)
    assert unpackb(data, object_hook=Node.create) == node
예제 #6
0
def c():
    return Node(key="c")
예제 #7
0
def b():
    return Node(key="b")
예제 #8
0
def a():
    return Node(key="a")
예제 #9
0
def test_create_node():
    assert isinstance(Node.create(), Node)
    assert isinstance(CustomNode.create(), CustomNode)
    assert Node.create(name="abc").key == "abc|ENTITY"