File: test_graph_items.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (64 lines) | stat: -rw-r--r-- 1,522 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from rdflib import RDF, Graph, Namespace

EX = Namespace("http://example.org/")


def test_items():
    g = Graph().parse(
        data="""
        @prefix : <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

        <> :value (
            <http://example.org/thing1>
            <http://example.org/thing2>
        ).

        <> :value (
            <http://example.org/thing3>
        ).

        <> :value ().

        <> :value (
            <http://example.org/thing4>
            <http://example.org/thing5>
            <http://example.org/thing6>
        ).
        """,
        format="turtle",
    )

    values = {tuple(g.items(v)) for v in g.objects(None, RDF.value)}
    assert values == {
        (EX.thing1, EX.thing2),
        (EX.thing3,),
        (),
        (EX.thing4, EX.thing5, EX.thing6),
    }


def test_recursive_list_detection():
    g = Graph().parse(
        data="""
        @prefix : <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

        <> :value _:a .
        _:a :first "turtles"; :rest _:a .

        <> :value [ :first "turtles"; :rest _:b ] .
        _:b :first "all the way down"; :rest _:b .

        <> :value [ :first "turtles"; :rest _:c ] .
        _:c :first "all the way down"; :rest _:a .

        """,
        format="turtle",
    )

    for v in g.objects(None, RDF.value):
        try:
            list(g.items(v))
        except ValueError as e:  # noqa: F841
            pass
        else:
            assert False, "Expected detection of recursive rdf:rest reference"