File: test_nested_arrays.py

package info (click to toggle)
rdflib 7.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 77,580 kB
  • sloc: python: 58,671; sh: 153; makefile: 88; ruby: 74; xml: 45
file content (71 lines) | stat: -rw-r--r-- 1,582 bytes parent folder | download
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
65
66
67
68
69
70
71
from __future__ import annotations

from rdflib import Graph, Literal
from rdflib.collection import Collection
from test.utils.namespace import EGDC

prop = EGDC["props/a"]
res = EGDC["res"]

DATA_NO_CONTAINER = """
{
    "@context": {
        "egdc": "http://example.com/",
        "a": {
            "@id": "egdc:props/a"
        }
    },
    "a": [
        [[1, 2, 3], ["4", 5]],
        6,
        [7, { "@id": "egdc:res" }]
    ]
}
"""

DATA_LIST = """
{
    "@context": {
        "egdc": "http://example.com/",
        "a": {
            "@id": "egdc:props/a",
            "@container": "@list"
        }
    },
    "a": [
        [[1, 2, 3], ["4", 5]],
        6,
        [7, { "@id": "egdc:res" }]
    ]
}
"""


def test_container_list():
    g = Graph()
    g.parse(data=DATA_LIST, format="application/ld+json")

    outer = Collection(g, next(g.objects(predicate=prop)))
    assert len(outer) == 3
    inner1, inner2, inner3 = outer

    inner1 = Collection(g, inner1)
    inner1_1, inner1_2 = map(lambda l: Collection(g, l), inner1)  # noqa: E741
    assert list(inner1_1) == [Literal(x) for x in (1, 2, 3)]
    assert list(inner1_2) == [Literal(x) for x in ("4", 5)]

    assert inner2 == Literal(6)

    inner3 = Collection(g, inner3)
    assert list(inner3) == [Literal(7), res]


def test_no_container():
    g = Graph()
    g.parse(data=DATA_NO_CONTAINER, format="application/ld+json")

    assert len(g) == 8

    objects = set(g.objects(predicate=prop))
    assert len(objects) == 8
    assert objects == set([Literal(x) for x in (1, 2, 3, "4", 5, 6, 7)] + [res])