File: test_reassign_id.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 (54 lines) | stat: -rw-r--r-- 1,275 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
from __future__ import annotations

from rdflib import BNode, Graph, Literal, Namespace, URIRef

DATA = """
{
  "@context": {
    "@version": 1.1,
    "ex": "https://example.com/",
    "@base": "https://example.com/res/",
    "id": "@id",
    "test": {
      "@id": "ex:test",
      "@context": {
        "id": "ex:id"
      }
    }
  },
  "id": "parent",
  "test": [
    { "id": "item1" },
    { "id": "item2" }
  ]
}
"""

DATA_OK = """
<https://example.com/res/parent> <https://example.com/test> _:b0 .
<https://example.com/res/parent> <https://example.com/test> _:b1 .
_:b0 <https://example.com/id> "item1" .
_:b1 <https://example.com/id> "item2" .
"""

EX = Namespace("https://example.com/")


def test_reassign_id():
    g = Graph().parse(data=DATA, format="json-ld")
    # g = Graph().parse(data=DATA_OK)

    parent = URIRef("https://example.com/res/parent")
    ex_id = EX.id
    ex_test = EX.test

    objects = list(g.objects(parent, ex_test))

    assert len(g) == 4
    assert len(objects) == 2
    for obj in objects:
        assert isinstance(obj, BNode)
        obj_pred_objects = list(g.predicate_objects(obj))
        assert len(obj_pred_objects) == 1
        assert obj_pred_objects[0][0] == ex_id
        assert isinstance(obj_pred_objects[0][1], Literal)