Skip to content

Commit 6012f90

Browse files
committed
Add test for possible future type review using JSON signature
No effects were observed on Make-managed files. PEP 287, Q&A Question 4, and the ReST Primer provided the syntax reminders on reSructuredText. References: * https://docutils.sourceforge.io/docs/user/rst/quickstart.html#text-styles * https://peps.python.org/pep-0287/#questions-answers Signed-off-by: Alex Nelson <[email protected]>
1 parent e655d83 commit 6012f90

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test_json_native_type.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
3+
# Portions of this file contributed by NIST are governed by the
4+
# following statement:
5+
#
6+
# This software was developed at the National Institute of Standards
7+
# and Technology by employees of the Federal Government in the course
8+
# of their official duties. Pursuant to Title 17 Section 105 of the
9+
# United States Code, this software is not subject to copyright
10+
# protection within the United States. NIST assumes no responsibility
11+
# whatsoever for its use by other parties, and makes no guarantees,
12+
# expressed or implied, about its quality, reliability, or any other
13+
# characteristic.
14+
#
15+
# We would appreciate acknowledgement if the software is used.
16+
17+
"""
18+
This module includes tests for JSON-LD RDF-type interpretations of native JSON types.
19+
"""
20+
21+
import json
22+
from typing import Union
23+
24+
from rdflib import XSD, Graph, Literal, Namespace
25+
26+
JSON = Union[dict[str, "JSON"], list["JSON"], str, int, bool, float, None]
27+
28+
29+
def test_json_float() -> None:
30+
"""
31+
This test demonstrates that JSON-LD has a default interpretation of a JSON float [#jsonld1.1-B.1.3]_, automatically typing as ``xsd:double``. This happens to differ from CASE's default of ``xsd:decimal``.
32+
33+
References
34+
==========
35+
36+
.. [#jsonld1.1-B.1.3] https://www.w3.org/TR/json-ld11/#conversion-of-native-data-types
37+
"""
38+
39+
expected_graph = Graph()
40+
computed_graph = Graph()
41+
42+
json_graph: JSON = {
43+
"@context": {
44+
"ex": "http://example.org/ontology/",
45+
"kb": "http://example.org/kb/",
46+
"xsd": "http://www.w3.org/2001/XMLSchema#",
47+
},
48+
"@graph": {
49+
"@id": "kb:Thing-1",
50+
"ex:property1": {"@type": "xsd:decimal", "@value": "1.234"},
51+
"ex:property2": {"@type": "xsd:double", "@value": "2.345"},
52+
"ex:property3": {"@type": "xsd:float", "@value": "3.456"},
53+
"ex:property4": 4.567,
54+
},
55+
}
56+
57+
computed_graph.parse(data=json.dumps(json_graph), format="json-ld")
58+
59+
ns_ex = Namespace("http://example.org/ontology/")
60+
ns_kb = Namespace("http://example.org/kb/")
61+
ns_xsd = XSD
62+
63+
expected_graph.add(
64+
(
65+
ns_kb["Thing-1"],
66+
ns_ex["property1"],
67+
Literal("1.234", datatype=ns_xsd.decimal),
68+
)
69+
)
70+
expected_graph.add(
71+
(ns_kb["Thing-1"], ns_ex["property2"], Literal("2.345", datatype=ns_xsd.double))
72+
)
73+
expected_graph.add(
74+
(ns_kb["Thing-1"], ns_ex["property3"], Literal("3.456", datatype=ns_xsd.float))
75+
)
76+
expected_graph.add(
77+
(ns_kb["Thing-1"], ns_ex["property4"], Literal("4.567", datatype=ns_xsd.double))
78+
)
79+
80+
expected_triples = [x for x in expected_graph.triples((None, None, None))]
81+
computed_triples = [x for x in computed_graph.triples((None, None, None))]
82+
83+
assert expected_triples == computed_triples

0 commit comments

Comments
 (0)