summaryrefslogtreecommitdiff
path: root/test/test_serializers/test_serializer_jsonld.py
blob: aff0544e3fbdeaf9ad667847a9dd19b3782e6465 (plain)
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
import json
import logging
import pprint
from typing import Any, Dict, Union

import pytest

from rdflib import Graph
from rdflib.namespace import Namespace
from rdflib.plugins.shared.jsonld.context import Context

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


@pytest.mark.parametrize(
    ["input"],
    [
        (
            Context(
                {
                    "eg": f"{EG}",
                }
            ),
        ),
        ({"eg": f"{EG}"},),
    ],
)
def test_serialize_context(input: Union[Dict[str, Any], Context]) -> None:
    """
    The JSON-LD serializer accepts and correctly serializes the context argument to the output.
    """
    graph = Graph()
    graph.add((EG.subject, EG.predicate, EG.object0))
    graph.add((EG.subject, EG.predicate, EG.object1))
    context = Context(
        {
            "eg": f"{EG}",
        }
    )
    logging.debug("context = %s", pprint.pformat(vars(context)))
    data = graph.serialize(format="json-ld", context=context)
    logging.debug("data = %s", data)
    obj = json.loads(data)
    assert obj["@context"] == {"eg": f"{EG}"}