File: test_issue1484.py

package info (click to toggle)
rdflib 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,248 kB
  • sloc: python: 39,216; sh: 153; makefile: 110
file content (65 lines) | stat: -rw-r--r-- 1,670 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
55
56
57
58
59
60
61
62
63
64
65
import unittest
import io
import json
from rdflib import Graph, RDF, RDFS, Namespace


class TestIssue1484_json(unittest.TestCase):
    def test_issue_1484_json(self):
        """
        Test JSON-LD parsing of result from json.dump
        """
        n = Namespace("http://example.org/")
        jsondata = {"@id": n.s, "@type": [n.t], n.p: {"@id": n.o}}

        s = io.StringIO()
        json.dump(jsondata, s, indent=2, separators=(",", ": "))
        s.seek(0)

        DEBUG = False
        if DEBUG:
            print("S: ", s.read())
            s.seek(0)

        b = n.base
        g = Graph()
        g.bind("rdf", RDF)
        g.bind("rdfs", RDFS)
        g.parse(source=s, publicID=b, format="json-ld")

        assert (n.s, RDF.type, n.t) in g
        assert (n.s, n.p, n.o) in g


class TestIssue1484_str(unittest.TestCase):
    def test_issue_1484_str(self):
        """
        Test JSON-LD parsing of result from string (used by round tripping tests)

        (Previously passes, but broken by earlier fix for above.)
        """
        n = Namespace("http://example.org/")
        jsonstr = """
            {
              "@id": "http://example.org/s",
              "@type": [
                "http://example.org/t"
              ],
              "http://example.org/p": {
                "@id": "http://example.org/o"
              }
            }
        """

        b = n.base
        g = Graph()
        g.bind("rdf", RDF)
        g.bind("rdfs", RDFS)
        g.parse(data=jsonstr, publicID=b, format="json-ld")

        assert((n.s, RDF.type, n.t) in g)
        assert((n.s, n.p, n.o) in g)


if __name__ == "__main__":
    unittest.main()