|
| 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