Adapter to use PyLD's json-ld parser to add triples to librdf.
[htsworkflow.git] / htsworkflow / util / test / test_rdfjsonld.py
1 from unittest2 import TestCase, TestSuite, defaultTestLoader, skip
2
3 from htsworkflow.util.rdfjsonld import load_into_model, to_node, triple_to_statement
4 from htsworkflow.util.rdfhelp import get_model
5
6 jstatement = {
7     'object': {'datatype': u'http://www.w3.org/2001/XMLSchema#dateTime',
8                 'type': 'literal',
9                 'value': '1940-10-09'},
10     'predicate': {'type': 'IRI',
11                     'value': u'http://schema.org/birthDate'},
12     'subject': {'type': 'blank node',
13                 'value': '_:a'}
14 }
15 doc = {
16   "@context": "http://json-ld.org/contexts/person.jsonld",
17   "@id": "http://dbpedia.org/resource/John_Lennon",
18   "name": "John Lennon",
19   "born": "1940-10-09",
20   "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
21 }
22
23 class TestJsonLD(TestCase):
24     def test_to_node(self):
25         obj = to_node(jstatement['object'])
26         self.assertTrue(obj.is_literal())
27         self.assertEqual(str(obj), '1940-10-09')
28         pred = to_node(jstatement['predicate'])
29         self.assertTrue(pred.is_resource())
30         self.assertEqual(str(pred.uri), jstatement['predicate']['value'])
31         subj = to_node(jstatement['subject'])
32         self.assertTrue(subj.is_blank())
33
34     def test_to_statement(self):
35         stmt = triple_to_statement(jstatement)
36         self.assertTrue(stmt.object.is_literal())
37         self.assertEqual(str(stmt.object), '1940-10-09')
38         self.assertTrue(stmt.predicate.is_resource())
39         self.assertEqual(str(stmt.predicate.uri), jstatement['predicate']['value'])
40         self.assertTrue(stmt.subject.is_blank())
41
42     def test_load_model(self):
43         model = get_model(use_contexts=False)
44         self.assertEqual(len(model), 0)
45         load_into_model(model, doc)
46         self.assertEqual(len(model), 3)
47
48 def suite():
49     suite = TestSuite()
50     suite.addTests(
51         defaultTestLoader.loadTestsFromTestCase(TestJsonLD))
52     return suite
53
54 if __name__ == "__main__":
55     from unittest2 import main
56     main(defaultTest='suite')