Ejemplo n.º 1
0
def test_blank_node_id() -> None:
    # Test that blank nodes are passed through and not considered
    # relative paths.  Blank nodes (also called anonymous ids) are
    # URIs starting with "_:".  They are randomly generated
    # placeholders mainly used internally where an id is needed but
    # was not given.

    ldr = Loader({})
    ctx = {"id": "@id"}  # type: ContextType
    ldr.add_context(ctx)

    ra, _ = ldr.resolve_all(cmap({"id": "_:foo"}), "http://example.com")
    assert {"id": "_:foo"} == ra
Ejemplo n.º 2
0
def test_secondaryFile_dsl_ref() -> None:
    ldr = Loader({})
    ldr.add_context({"secondaryFiles": {"secondaryFilesDSL": True}})

    ra, _ = ldr.resolve_all(cmap({"secondaryFiles": ".foo"}), "")
    assert {"secondaryFiles": {"pattern": ".foo", "required": None}} == ra

    ra, _ = ldr.resolve_all(cmap({"secondaryFiles": ".foo?"}), "")
    assert {"secondaryFiles": {"pattern": ".foo", "required": False}} == ra

    ra, _ = ldr.resolve_all(cmap({"secondaryFiles": [".foo"]}), "")
    assert {"secondaryFiles": [{"pattern": ".foo", "required": None}]} == ra

    ra, _ = ldr.resolve_all(cmap({"secondaryFiles": [".foo?"]}), "")
    assert {"secondaryFiles": [{"pattern": ".foo", "required": False}]} == ra
Ejemplo n.º 3
0
def test_subscoped_id() -> None:
    ldr = Loader({})
    ctx = {
        "id": "@id",
        "bar": {"subscope": "bar"},
    }  # type: ContextType
    ldr.add_context(ctx)

    ra, _ = ldr.resolve_all(
        cmap({"id": "foo", "bar": {"id": "baz"}}), "http://example.com"
    )
    assert {
        "id": "http://example.com/#foo",
        "bar": {"id": "http://example.com/#foo/bar/baz"},
    } == ra
Ejemplo n.º 4
0
def test_idmap() -> None:
    ldr = Loader({})
    ldr.add_context({
        "inputs": {
            "@id": "http://example.com/inputs",
            "mapSubject": "id",
            "mapPredicate": "a",
        },
        "outputs": {
            "@type": "@id",
            "identity": True
        },
        "id": "@id",
    })

    ra, _ = ldr.resolve_all(
        cmap({
            "id": "stuff",
            "inputs": {
                "zip": 1,
                "zing": 2
            },
            "outputs": ["out"],
            "other": {
                "n": 9
            },
        }),
        "http://example2.com/",
    )
    assert isinstance(ra, CommentedMap)

    assert "http://example2.com/#stuff" == ra["id"]
    for item in ra["inputs"]:
        if item["a"] == 2:
            assert "http://example2.com/#stuff/zing" == item["id"]
        else:
            assert "http://example2.com/#stuff/zip" == item["id"]
    assert ["http://example2.com/#stuff/out"] == ra["outputs"]
    assert {"n": 9} == ra["other"]
Ejemplo n.º 5
0
def test_typedsl_ref() -> None:
    ldr = Loader({})
    ldr.add_context(
        {
            "File": "http://example.com/File",
            "null": "http://example.com/null",
            "array": "http://example.com/array",
            "type": {"@type": "@vocab", "typeDSL": True},
        }
    )

    ra, _ = ldr.resolve_all(cmap({"type": "File"}), "")
    assert {"type": "File"} == ra

    ra, _ = ldr.resolve_all(cmap({"type": "File?"}), "")
    assert {"type": ["null", "File"]} == ra

    ra, _ = ldr.resolve_all(cmap({"type": "File[]"}), "")
    assert {"type": {"items": "File", "type": "array"}} == ra

    ra, _ = ldr.resolve_all(cmap({"type": "File[]?"}), "")
    assert {"type": ["null", {"items": "File", "type": "array"}]} == ra
Ejemplo n.º 6
0
def test_scoped_id() -> None:
    ldr = Loader({})
    ctx = {
        "id": "@id",
        "location": {
            "@id": "@id",
            "@type": "@id"
        },
        "bar": "http://example.com/bar",
        "ex": "http://example.com/",
    }  # type: ContextType
    ldr.add_context(ctx)

    ra, _ = ldr.resolve_all(cmap({
        "id": "foo",
        "bar": {
            "id": "baz"
        }
    }), "http://example.com")
    assert {
        "id": "http://example.com/#foo",
        "bar": {
            "id": "http://example.com/#foo/baz"
        },
    } == ra

    g = makerdf(None, ra, ctx)
    print(g.serialize(format="n3"))

    ra, _ = ldr.resolve_all(
        cmap({
            "location": "foo",
            "bar": {
                "location": "baz"
            }
        }),
        "http://example.com",
        checklinks=False,
    )
    assert {
        "location": "http://example.com/foo",
        "bar": {
            "location": "http://example.com/baz"
        },
    } == ra

    g = makerdf(None, ra, ctx)
    print(g.serialize(format="n3"))

    ra, _ = ldr.resolve_all(
        cmap({
            "id": "foo",
            "bar": {
                "location": "baz"
            }
        }),
        "http://example.com",
        checklinks=False,
    )
    assert {
        "id": "http://example.com/#foo",
        "bar": {
            "location": "http://example.com/baz"
        },
    } == ra

    g = makerdf(None, ra, ctx)
    print(g.serialize(format="n3"))

    ra, _ = ldr.resolve_all(
        cmap({
            "location": "foo",
            "bar": {
                "id": "baz"
            }
        }),
        "http://example.com",
        checklinks=False,
    )
    assert {
        "location": "http://example.com/foo",
        "bar": {
            "id": "http://example.com/#baz"
        },
    } == ra

    g = makerdf(None, ra, ctx)
    print(g.serialize(format="n3"))
Ejemplo n.º 7
0
def test_scoped_ref() -> None:
    ldr = Loader({})
    ldr.add_context({
        "scatter": {
            "@type": "@id",
            "refScope": 0
        },
        "source": {
            "@type": "@id",
            "refScope": 2
        },
        "in": {
            "mapSubject": "id",
            "mapPredicate": "source"
        },
        "out": {
            "@type": "@id",
            "identity": True
        },
        "inputs": {
            "mapSubject": "id",
            "mapPredicate": "type"
        },
        "outputs": {
            "mapSubject": "id"
        },
        "steps": {
            "mapSubject": "id"
        },
        "id": "@id",
    })

    ra, _ = ldr.resolve_all(
        cmap({
            "inputs": {
                "inp": "string",
                "inp2": "string"
            },
            "outputs": {
                "out": {
                    "type": "string",
                    "source": "step2/out"
                }
            },
            "steps": {
                "step1": {
                    "in": {
                        "inp": "inp",
                        "inp2": "#inp2",
                        "inp3": ["inp", "inp2"]
                    },
                    "out": ["out"],
                    "scatter": "inp",
                },
                "step2": {
                    "in": {
                        "inp": "step1/out"
                    },
                    "scatter": "inp",
                    "out": ["out"],
                },
            },
        }),
        "http://example2.com/",
    )

    assert {
        "inputs": [
            {
                "id": "http://example2.com/#inp",
                "type": "string"
            },
            {
                "id": "http://example2.com/#inp2",
                "type": "string"
            },
        ],
        "outputs": [{
            "id": "http://example2.com/#out",
            "type": "string",
            "source": "http://example2.com/#step2/out",
        }],
        "steps": [
            {
                "id":
                "http://example2.com/#step1",
                "scatter":
                "http://example2.com/#step1/inp",
                "in": [
                    {
                        "id": "http://example2.com/#step1/inp",
                        "source": "http://example2.com/#inp",
                    },
                    {
                        "id": "http://example2.com/#step1/inp2",
                        "source": "http://example2.com/#inp2",
                    },
                    {
                        "id":
                        "http://example2.com/#step1/inp3",
                        "source": [
                            "http://example2.com/#inp",
                            "http://example2.com/#inp2",
                        ],
                    },
                ],
                "out": ["http://example2.com/#step1/out"],
            },
            {
                "id":
                "http://example2.com/#step2",
                "scatter":
                "http://example2.com/#step2/inp",
                "in": [{
                    "id": "http://example2.com/#step2/inp",
                    "source": "http://example2.com/#step1/out",
                }],
                "out": ["http://example2.com/#step2/out"],
            },
        ],
    } == ra