-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add
to_dict
method to the JSON-LD Context
class.
`Context.to_dict` is used in JSON-LD serialization, but it was not implemented. This change adds the method.
- Loading branch information
Showing
3 changed files
with
109 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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}"} |