예제 #1
0
async def test_prefix_not_specified():
    """
    Test that if we get a category with no preferred_prefixes
    we make no changes.
    """
    portal = KnowledgePortal()

    preferred_prefixes = {}

    query_graph = {
        "nodes": {
            "n0": {"ids": ["DOID:9352"]},
        },
        "edges": {},
    }

    msg = Message.parse_obj({"query_graph": query_graph})

    await portal.map_prefixes(
        msg,
        preferred_prefixes,
    )

    # n0 should be unchanged
    assert msg.dict()["query_graph"]["nodes"]["n0"]["ids"] == ["DOID:9352"]
예제 #2
0
async def test_unknown_prefix():
    """
    Test that if passed an unknown prefix we
    assume it doesn't need to be changed.
    """
    portal = KnowledgePortal()

    preferred_prefixes = {"biolink:Disease": ["MONDO"]}

    query_graph = {
        "nodes": {
            "n0": {"ids": ["UNKNOWN:000000"]},
        },
        "edges": {},
    }

    msg = Message.parse_obj({"query_graph": query_graph})

    await portal.map_prefixes(
        msg,
        preferred_prefixes,
    )

    # n0 should be unchanged
    assert msg.dict()["query_graph"]["nodes"]["n0"]["ids"] == ["UNKNOWN:000000"]
예제 #3
0
async def test_map_prefixes_small_example():
    """
    Test that prefixes are mapped properly and that already
    mapped prefixes are unchanged.
    """
    portal = KnowledgePortal()

    preferred_prefixes = {"biolink:Disease": ["MONDO"]}

    query_graph = {
        "nodes": {
            "n0": {"ids": ["DOID:9352"]},
            "n1": {"ids": ["MONDO:0005148"]},
        },
        "edges": {},
    }

    msg = Message.parse_obj({"query_graph": query_graph})

    await portal.map_prefixes(
        msg,
        preferred_prefixes,
    )
    msg = msg.dict()

    # n0 should be converted to the correct prefix
    assert msg["query_graph"]["nodes"]["n0"]["ids"] == ["MONDO:0005148"]
    # There should be no change to n1
    assert msg["query_graph"]["nodes"]["n1"]["ids"] == ["MONDO:0005148"]
예제 #4
0
async def test_normalizer_not_reachable(caplog):
    """
    Test that if the normalizer is completely unavailable we make
    no changes to the query graph and continue on while adding a
    a warning to the log
    """
    portal = KnowledgePortal()

    preferred_prefixes = {"biolink:Disease": ["MONDO"]}

    query_graph = {
        "nodes": {
            "n0": {"ids": ["DOID:9352"]},
        },
        "edges": {},
    }

    msg = Message.parse_obj({"query_graph": query_graph})

    await portal.map_prefixes(
        msg,
        preferred_prefixes,
    )

    # n0 should be unchanged
    assert msg.dict()["query_graph"]["nodes"]["n0"]["ids"] == ["DOID:9352"]

    assert "RequestError contacting normalizer" in caplog.text
예제 #5
0
async def test_normalizer_no_synonyms_available(caplog):
    """
    Test that if we send a node with no synonyms
    to the normalizer that we continue working and add
    a warning to the log
    """
    portal = KnowledgePortal()

    preferred_prefixes = {"biolink:Disease": ["MONDO"]}

    query_graph = {
        "nodes": {
            "n0": {"ids": ["DOID:9352"]},
        },
        "edges": {},
    }

    msg = Message.parse_obj({"query_graph": query_graph})

    await portal.map_prefixes(
        msg,
        preferred_prefixes,
    )

    # n0 should be unchanged
    assert msg.dict()["query_graph"]["nodes"]["n0"]["ids"] == ["DOID:9352"]

    # The error we recieved from the normalizer should be in the logs
    assert normalizer_error_no_matches in caplog.text
예제 #6
0
def generate_message(spec) -> Message:
    """
    Generate a message using a specification. Example for the specification format:

    {
        "random_seed" : 4,
        "knowledge_graph" : {
            "nodes" : {
                "count" : 100,
                "attributes" : {
                    "count" : 100,
                    "spec": {
                        "subattribute_count": 5,
                        "value_type": "list",
                        "value_count": 10
                    }
                },
                "categories_count" : 1
            },
            "edges" : {
                "count" : 100,
                "attributes" : {
                    "count" : 100,
                    "spec": {
                        "subattribute_count": 5,
                        "value_type": "dict",
                        "value_count": 10
                    }
                }
            }
        },
        "results" : {
            "count" : 100,
            "node_bindings" : {
                "count_per_node" : 1
            },
            "edge_bindings" : {
                "count_per_edge" : 1,
                "attributes" : {
                    "count" : 100,
                    "spec": {
                        "subattribute_count": 5,
                        "value_type": "string"
                    }
                }
            }
        }
    }
    """

    randomInstance = random.Random(spec.get("random_seed", None))
    get_random = lambda: "".join(
        randomInstance.choice(string.ascii_letters) for _ in range(10)
    )

    kg_node_ids = [
        f"biolink:{get_random()}"
        for _ in range(spec["knowledge_graph"]["nodes"]["count"])
    ]
    kg_edge_ids = [
        get_random() for _ in range(spec["knowledge_graph"]["nodes"]["count"])
    ]

    return Message(
        query_graph=QueryGraph(nodes={}, edges={}),
        knowledge_graph=KnowledgeGraph(
            nodes={
                kgnid: Node(
                    attributes=[
                        generate_attribute(
                            spec["knowledge_graph"]["nodes"]["attributes"]["spec"],
                            get_random,
                        )
                        for _ in range(
                            spec["knowledge_graph"]["nodes"]["attributes"]["count"]
                        )
                    ],
                    categories=[
                        f"biolink:Category{get_random()}"
                        for _ in range(
                            spec["knowledge_graph"]["nodes"]["categories_count"]
                        )
                    ],
                )
                for kgnid in kg_node_ids
            },
            edges={
                kgeid: Edge(
                    attributes=[
                        generate_attribute(
                            spec["knowledge_graph"]["edges"]["attributes"]["spec"],
                            get_random,
                        )
                        for _ in range(
                            spec["knowledge_graph"]["edges"]["attributes"]["count"]
                        )
                    ],
                    subject=random.choice(kg_node_ids),
                    predicate=f"biolink:{get_random().lower()}",
                    object=random.choice(kg_node_ids),
                )
                for kgeid in kg_edge_ids
            },
        ),
        results=[
            Result(
                node_bindings={
                    f"QGraphNode:{get_random()}": [
                        NodeBinding(id=kgnid)
                        for _ in range(
                            spec["results"]["node_bindings"]["count_per_node"]
                        )
                    ]
                    for kgnid in kg_node_ids
                },
                edge_bindings={
                    f"QGraphEdge:{get_random()}": [
                        EdgeBinding(
                            id=kgeid,
                            attributes=[
                                generate_attribute(
                                    spec["results"]["edge_bindings"]["attributes"][
                                        "spec"
                                    ],
                                    get_random,
                                )
                                for _ in range(
                                    spec["results"]["edge_bindings"]["attributes"][
                                        "count"
                                    ],
                                )
                            ],
                        )
                        for _ in range(
                            spec["results"]["edge_bindings"]["count_per_edge"]
                        )
                    ]
                    for kgeid in kg_edge_ids
                },
            )
            for _ in range(spec["results"]["count"])
        ],
    )